@telstra/core
Version:
Telstra SDK Core
38 lines (37 loc) • 1.1 kB
JavaScript
/**
* The base class for all errors in the SDK.
*
* @remarks
* This class provides a consistent structure for errors,
* including a `code` property for machine-readable error identification
* and a `formatError()` method for standardised error formatting.
*/
export class SDKError extends Error {
/**
* A machine-readable code for the error.
*/
code;
/**
* Creates a new `SDKError` instance.
*
* @param errorCode - The error code object containing the message and code.
*/
constructor(errorCode) {
super(errorCode.message);
this.name = this.constructor.name;
this.code = errorCode.code;
}
/**
* Formats the error for display.
*
* @returns A formatted string representation of the error.
*/
formatError() {
const stackTrace = new Error().stack?.split('\n');
if (stackTrace) {
stackTrace.shift();
stackTrace.shift();
}
return `${this.name}: ${this.message}\n${stackTrace?.join('\n')}\ncode: ${this.code}\nmessage: '${this.message}'`;
}
}