@novo-learning/novo-sdk
Version:
SDK for the Novolanguage Speech Analysis API
37 lines (31 loc) • 1.01 kB
text/typescript
export const formatError = (error: unknown): string => {
if (!error || typeof error !== 'object') {
return `Unknown error: ${String(error)}`;
}
const baseError = error as Partial<Error> & { errors?: unknown };
const lines = [];
lines.push(`Name: ${baseError.name ?? 'Unknown'}`);
if (baseError.message) {
lines.push(`Message: ${baseError.message}`);
}
Object.entries(baseError)
.filter(([key, _]) => !['name', 'message', 'stack', 'errors'].includes(key))
.forEach(([key, value]) => {
try {
lines.push(`${key}: ${JSON.stringify(value)}`);
} catch {
lines.push(`${key}: unable to stringify`);
}
});
if (baseError.stack) {
lines.push('Stacktrace:');
lines.push(baseError.stack);
}
if (baseError.errors && Array.isArray(baseError.errors)) {
baseError.errors.forEach((subError, index) => {
lines.push(`-- Error ${index + 1} --`);
lines.push(formatError(subError));
});
}
return lines.join('\n');
};