trainingpeaks-sdk
Version:
TypeScript SDK for TrainingPeaks API integration
43 lines (42 loc) • 1.33 kB
JavaScript
export class SDKError extends Error {
constructor(message, code, context, options) {
super(message);
this.code = code;
this.context = context;
this.name = 'SDKError';
this.cause = options?.cause;
}
toJSON() {
const result = {
name: this.name,
message: this.message,
code: this.code,
};
if (this.context !== undefined) {
result.context = this.context;
}
if (this.stack !== undefined && process.env.NODE_ENV !== 'production') {
result.stack = this.stack;
}
if (this.cause !== undefined) {
if (this.cause instanceof SDKError) {
result.cause = this.cause.toJSON();
}
else if (this.cause instanceof Error) {
const errorCause = {
name: this.cause.name,
message: this.cause.message,
};
if (this.cause.stack !== undefined &&
process.env.NODE_ENV !== 'production') {
errorCause.stack = this.cause.stack;
}
result.cause = errorCause;
}
else {
result.cause = this.cause;
}
}
return result;
}
}