therepo/packages/conventional/src/lib/utils/error.ts
M. George Hansen 5f519f54f2
feat(@websnacksjs/conventional): initial implementation
`@websnacksjs/conventional` is a cli program which can be used with
`husky` & git hooks to enforce that commit messages adhere to the
conventional commits standard.
2025-08-19 18:31:34 +12:00

28 lines
691 B
TypeScript

export const normalizeError = (error: unknown): Error => {
if (!(error instanceof Error)) {
error = new Error(`non-Error type thrown: ${JSON.stringify(error)}`);
}
return error as Error;
};
const errorCauseChain = (error: Error): Error[] => {
const causeChain: Error[] = [];
let cause = error.cause;
while (cause !== undefined) {
causeChain.push(normalizeError(cause));
if (cause instanceof Error) {
cause = cause.cause;
}
}
return causeChain;
};
export const formatError = (error: Error): string => {
let message = error.message;
const causeChain = errorCauseChain(error);
for (const cause of causeChain) {
message += `: ${cause.message}`;
}
return message;
};