@metamask/kernel-errors
Version:
1 lines • 2.52 kB
Source Map (JSON)
{"version":3,"file":"unmarshalError.mjs","sourceRoot":"","sources":["../../src/marshal/unmarshalError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,mCAAkC;AACjE,OAAO,EAAE,YAAY,EAAE,4BAA2B;AAOlD;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAC5B,cAA8B;IAE9B,IAAI,oBAAoB,CAAC,cAAc,CAAC,EAAE,CAAC;QACzC,OAAO,YAAY,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,SAAS,CAChD,cAAc,EACd,qBAAqB,CACtB,CAAC;IACJ,CAAC;IAED,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,qBAAqB,CAAC,cAAc,CAAC,CAAC;IAE/D,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,cAAc,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;IAE3D,IAAI,KAAK,EAAE,CAAC;QACV,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CACnC,cAA8B;IAE9B,MAAM,MAAM,GAA0B,EAAE,CAAC;IAEzC,IAAI,cAAc,CAAC,KAAK,EAAE,CAAC;QACzB,MAAM,CAAC,KAAK,GAAG,cAAc,CAAC,KAAK,CAAC;IACtC,CAAC;IAED,IAAI,cAAc,CAAC,KAAK,EAAE,CAAC;QACzB,MAAM,CAAC,KAAK;YACV,OAAO,cAAc,CAAC,KAAK,KAAK,QAAQ;gBACtC,CAAC,CAAC,cAAc,CAAC,KAAK;gBACtB,CAAC,CAAC,cAAc,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;IAC7C,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { isMarshaledOcapError } from './isMarshaledOcapError.ts';\nimport { errorClasses } from '../errors/index.ts';\nimport type {\n ErrorOptionsWithStack,\n MarshaledError,\n OcapError,\n} from '../types.ts';\n\n/**\n * Unmarshals a {@link MarshaledError} into an {@link Error}.\n *\n * @param marshaledError - The marshaled error to unmarshal.\n * @returns The unmarshaled error.\n */\nexport function unmarshalError(\n marshaledError: MarshaledError,\n): Error | OcapError {\n if (isMarshaledOcapError(marshaledError)) {\n return errorClasses[marshaledError.code].unmarshal(\n marshaledError,\n unmarshalErrorOptions,\n );\n }\n\n const { cause, stack } = unmarshalErrorOptions(marshaledError);\n\n const error = new Error(marshaledError.message, { cause });\n\n if (stack) {\n error.stack = stack;\n }\n\n return error;\n}\n\n/**\n * Gets the error options from a marshaled error.\n *\n * @param marshaledError - The marshaled error to get the options from.\n * @returns The error options.\n */\nexport function unmarshalErrorOptions(\n marshaledError: MarshaledError,\n): ErrorOptionsWithStack {\n const output: ErrorOptionsWithStack = {};\n\n if (marshaledError.stack) {\n output.stack = marshaledError.stack;\n }\n\n if (marshaledError.cause) {\n output.cause =\n typeof marshaledError.cause === 'string'\n ? marshaledError.cause\n : unmarshalError(marshaledError.cause);\n }\n\n return output;\n}\n"]}