Files
automatisch/packages/backend/src/errors/base.ts
2022-11-07 22:51:30 +01:00

33 lines
822 B
TypeScript

import { IJSONObject } from '@automatisch/types';
export default class BaseError extends Error {
details = {};
constructor(error?: string | IJSONObject) {
let computedError: Record<string, unknown>;
try {
computedError = JSON.parse(error as string);
} catch {
computedError = typeof error === 'string' ? { error } : error;
}
let computedMessage: string;
try {
// challenge to input to see if it is stringified JSON
JSON.parse(error as string);
computedMessage = error as string;
} catch {
if (typeof error === 'string') {
computedMessage = error;
} else {
computedMessage = JSON.stringify(error, null, 2);
}
}
super(computedMessage);
this.details = computedError;
this.name = this.constructor.name;
}
}