@cloudpss/template
Version:
Lightweight string and object templating utilities with interpolation and formula support.
16 lines (13 loc) • 690 B
text/typescript
import { stringify } from './utils.js';
const KNOWN_ERRORS = [EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError] as const;
/** 构建 Error */
export function buildError(err: Error): string {
if (typeof DOMException == 'function' && err instanceof DOMException) {
return `new DOMException(${stringify(err.message)}, ${stringify(err.name)})`;
}
const constructor = KNOWN_ERRORS.find((type) => err instanceof type)?.name ?? 'Error';
if (err.name === constructor) {
return `new ${constructor}(${stringify(err.message)})`;
}
return `Object.assign(new ${constructor}(${stringify(err.message)}), {name: ${stringify(err.name)}})`;
}