`@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.
28 lines
691 B
TypeScript
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;
|
|
};
|