@trpc/server
Version:
77 lines (73 loc) • 2.46 kB
JavaScript
;
var utils = require('../utils.js');
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
class UnknownCauseError extends Error {
}
function getCauseFromUnknown(cause) {
if (cause instanceof Error) {
return cause;
}
const type = typeof cause;
if (type === 'undefined' || type === 'function' || cause === null) {
return undefined;
}
// Primitive types just get wrapped in an error
if (type !== 'object') {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
return new Error(String(cause));
}
// If it's an object, we'll create a synthetic error
if (utils.isObject(cause)) {
return Object.assign(new UnknownCauseError(), cause);
}
return undefined;
}
function getTRPCErrorFromUnknown(cause) {
if (cause instanceof TRPCError) {
return cause;
}
if (cause instanceof Error && cause.name === 'TRPCError') {
// https://github.com/trpc/trpc/pull/4848
return cause;
}
const trpcError = new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
cause
});
// Inherit stack from error
if (cause instanceof Error && cause.stack) {
trpcError.stack = cause.stack;
}
return trpcError;
}
class TRPCError extends Error {
constructor(opts){
const cause = getCauseFromUnknown(opts.cause);
const message = opts.message ?? cause?.message ?? opts.code;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore https://github.com/tc39/proposal-error-cause
super(message, {
cause
}), // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore override doesn't work in all environments due to "This member cannot have an 'override' modifier because it is not declared in the base class 'Error'"
_define_property(this, "cause", void 0), _define_property(this, "code", void 0);
this.code = opts.code;
this.name = 'TRPCError';
this.cause ?? (this.cause = cause);
}
}
exports.TRPCError = TRPCError;
exports.getCauseFromUnknown = getCauseFromUnknown;
exports.getTRPCErrorFromUnknown = getTRPCErrorFromUnknown;