All files / kernel-errors/src/errors EvaluatorError.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 3/3
100% Lines 8/8

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92                                                                                          5x         5x           134x           1x                               4x 4x     4x               134x  
import {
  assert,
  lazy,
  literal,
  object,
  optional,
  string,
  union,
} from '@metamask/superstruct';
 
import { BaseError } from '../BaseError.ts';
import {
  marshaledErrorSchema,
  ErrorCode,
  MarshaledErrorStruct,
} from '../constants.ts';
import type { ErrorOptionsWithStack, MarshaledOcapError } from '../types.ts';
 
/**
 * An error indicating a violation of evaluator infrastructure expectations.
 * These errors indicate internal failures that should exit the attempt,
 * such as $return, $catch, or $capture throwing inside the compartment.
 *
 * Note: This error should be impossible to throw in normal operation,
 * even if the compiler cannot detect this.
 */
export class EvaluatorError extends BaseError {
  /**
   * Creates a new EvaluatorError.
   *
   * @param message - A human-readable description of the evaluator error.
   * @param code - An internal code identifying the specific evaluator failure.
   * @param cause - The underlying error that caused this evaluator error.
   * @param options - Additional error options including stack.
   * @param options.data - Additional data about the error.
   * @param options.data.code - An internal code identifying the specific evaluator failure.
   * @param options.cause - The underlying error that caused this evaluator error.
   * @param options.stack - The stack trace of the error.
   */
  constructor(
    message: string,
    code: string,
    cause: Error,
    options?: ErrorOptionsWithStack,
  ) {
    super(ErrorCode.InternalError, message, {
      ...options,
      cause,
      data: { code },
    });
    harden(this);
  }
 
  /**
   * A superstruct struct for validating marshaled {@link EvaluatorError} instances.
   */
  public static struct = object({
    ...marshaledErrorSchema,
    code: literal(ErrorCode.InternalError),
    data: object({
      code: string(),
    }),
    cause: optional(union([string(), lazy(() => MarshaledErrorStruct)])),
  });
 
  /**
   * Unmarshals a {@link MarshaledError} into an {@link EvaluatorError}.
   *
   * @param marshaledError - The marshaled error to unmarshal.
   * @param unmarshalErrorOptions - The function to unmarshal the error options.
   * @returns The unmarshaled error.
   */
  public static unmarshal(
    marshaledError: MarshaledOcapError,
    unmarshalErrorOptions: (
      marshaledError: MarshaledOcapError,
    ) => ErrorOptionsWithStack,
  ): EvaluatorError {
    assert(marshaledError, this.struct);
    const cause = marshaledError.cause
      ? (unmarshalErrorOptions(marshaledError).cause as Error)
      : new Error('Unknown cause');
    return new EvaluatorError(
      marshaledError.message,
      marshaledError.data.code,
      cause,
      unmarshalErrorOptions(marshaledError),
    );
  }
}
harden(EvaluatorError);