22 lines
642 B
JavaScript
22 lines
642 B
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.asError = void 0;
|
|
/**
|
|
* Converts an unknown thrown value into an Error instance.
|
|
* If `e` is already an Error, wraps it; if a string, uses it as the message;
|
|
* otherwise JSON-serializes it as the error message.
|
|
*
|
|
* @param e - The unknown value to convert
|
|
* @returns An Error instance
|
|
*/
|
|
const asError = (e) => {
|
|
if (e instanceof Error) {
|
|
return e;
|
|
}
|
|
if (typeof e === 'string') {
|
|
return new Error(`${e}`);
|
|
}
|
|
return new Error(`${JSON.stringify(e)}`);
|
|
};
|
|
exports.asError = asError;
|
|
//# sourceMappingURL=asError.js.map
|