@cloudpss/template
Version:
Lightweight string and object templating utilities with interpolation and formula support.
14 lines • 699 B
JavaScript
import { stringify } from './utils.js';
const KNOWN_ERRORS = [EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError];
/** 构建 Error */
export function buildError(err) {
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)}})`;
}
//# sourceMappingURL=builder.js.map