All files / kernel-errors/src/marshal marshalError.ts

100% Statements 10/10
80% Branches 8/10
100% Functions 1/1
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                          50x         50x 8x           50x 50x     50x 4x 4x 4x       50x    
import { getSafeJson } from '@metamask/utils';
 
import { ErrorSentinel } from '../constants.ts';
import type { MarshaledError } from '../types.ts';
import { isOcapError } from '../utils/isOcapError.ts';
 
/**
 * Marshals an error into a {@link MarshaledError}.
 *
 * @param error - The error to marshal.
 * @returns The marshaled error.
 */
export function marshalError(error: Error): MarshaledError {
  const output: MarshaledError = {
    [ErrorSentinel]: true,
    message: error.message,
  };
 
  if (error.cause) {
    output.cause =
      error.cause instanceof Error
        ? marshalError(error.cause)
        : JSON.stringify(error.cause);
  }
 
  Eif (error.stack) {
    output.stack = error.stack;
  }
 
  if (isOcapError(error)) {
    output.code = error.code;
    Eif (error.data) {
      output.data = getSafeJson(error.data);
    }
  }
 
  return harden(output);
}