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AAO,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;IAC9D,CAAC;IAED,6EAA6E;IAC7E,yDAAyD;IACzD,OAAO;QACL,eAAe,CACb,uBAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,IAAA,2BAAe,EAAC,KAAK,CAAC,EACtB,IAAA,yBAAa,EAAC,KAAK,CAAC,CACrB;QACD,KAAK;KACN,CAAC;AACJ,CAAC;AAjED,kCAiEC;AAEY,QAAA,oBAAoB,GAA2B,IAAA,oBAAM,EAAC;IACjE,IAAI,EAAE,IAAA,oBAAM,GAAE;IACd,OAAO,EAAE,IAAA,oBAAM,GAAE;IACjB,KAAK,EAAE,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC;IACzB,KAAK,EAAE,IAAA,sBAAQ,EAAC,IAAA,kBAAI,EAAiB,GAAG,EAAE,CAAC,4BAAoB,CAAC,CAAC;CAClE,CAAC,CAAC","sourcesContent":["import {\n  errorCodes,\n  JsonRpcError as RpcError,\n  serializeCause,\n} from '@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type { TrackableError } from '@metamask/snaps-sdk';\nimport {\n  getErrorMessage,\n  getErrorStack,\n  isSerializedSnapError,\n  isSnapError,\n} from '@metamask/snaps-sdk';\nimport type { Struct } from '@metamask/superstruct';\nimport { lazy, nullable, object, string } from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\nimport { isObject, isJsonRpcError } from '@metamask/utils';\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';\n\nexport type SerializedSnapErrorWrapper = {\n  code: typeof SNAP_ERROR_WRAPPER_CODE;\n  message: typeof SNAP_ERROR_WRAPPER_MESSAGE;\n  data: {\n    cause: Json;\n  };\n};\n\nexport class WrappedSnapError extends Error {\n  readonly #error: unknown;\n\n  readonly #message: string;\n\n  readonly #stack?: string;\n\n  /**\n   * Create a new `WrappedSnapError`.\n   *\n   * @param error - The error to create the `WrappedSnapError` from.\n   */\n  constructor(error: unknown) {\n    const message = getErrorMessage(error);\n    super(message);\n\n    this.#error = error;\n    this.#message = message;\n    this.#stack = getErrorStack(error) ?? undefined;\n  }\n\n  /**\n   * The error name.\n   *\n   * @returns The error name.\n   */\n  get name() {\n    return 'WrappedSnapError';\n  }\n\n  /**\n   * The error message.\n   *\n   * @returns The error message.\n   */\n  get message() {\n    return this.#message;\n  }\n\n  /**\n   * The error stack.\n   *\n   * @returns The error stack.\n   */\n  get stack() {\n    return this.#stack;\n  }\n\n  /**\n   * Convert the error to a JSON object.\n   *\n   * @returns The JSON object.\n   */\n  toJSON(): SerializedSnapErrorWrapper {\n    const cause = isSnapError(this.#error)\n      ? this.#error.serialize()\n      : serializeCause(this.#error);\n\n    return {\n      code: SNAP_ERROR_WRAPPER_CODE,\n      message: SNAP_ERROR_WRAPPER_MESSAGE,\n      data: {\n        cause,\n      },\n    };\n  }\n\n  /**\n   * Serialize the error to a JSON object. This is called by\n   * `@metamask/rpc-errors` when serializing the error.\n   *\n   * @returns The JSON object.\n   */\n  serialize() {\n    return this.toJSON();\n  }\n}\n\n/**\n * Check if a JSON-RPC error is a `WrappedSnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `WrappedSnapError`.\n */\nexport function isWrappedSnapError(\n  error: unknown,\n): error is SerializedSnapErrorWrapper {\n  return (\n    isJsonRpcError(error) &&\n    error.code === SNAP_ERROR_WRAPPER_CODE &&\n    error.message === SNAP_ERROR_WRAPPER_MESSAGE\n  );\n}\n\n/**\n * Get a JSON-RPC error with the given code, message, stack, and data.\n *\n * @param code - The error code.\n * @param message - The error message.\n * @param stack - The error stack.\n * @param data - Additional data for the error.\n * @returns The JSON-RPC error.\n */\nfunction getJsonRpcError(\n  code: number,\n  message: string,\n  stack?: string | null,\n  data?: Json,\n) {\n  const error = new RpcError(code, message, data);\n  error.stack = stack ?? undefined;\n\n  return error;\n}\n\n/**\n * Attempt to unwrap an unknown error to a `JsonRpcError`. This function will\n * try to get the error code, message, and data from the error, and return a\n * `JsonRpcError` with those properties.\n *\n * @param error - The error to unwrap.\n * @returns A tuple containing the unwrapped error and a boolean indicating\n * whether the error was handled.\n */\nexport function unwrapError(\n  error: unknown,\n): [error: RpcError<DataWithOptionalCause>, isHandled: boolean] {\n  // This logic is a bit complicated, but it's necessary to handle all the\n  // different types of errors that can be thrown by a Snap.\n\n  // If the error is a wrapped Snap error, unwrap it.\n  if (isWrappedSnapError(error)) {\n    // The wrapped error can be a JSON-RPC error, or an unknown error. If it's\n    // a JSON-RPC error, we can unwrap it further.\n    if (isJsonRpcError(error.data.cause)) {\n      // If the JSON-RPC error is a wrapped Snap error, unwrap it further.\n      if (isSerializedSnapError(error.data.cause)) {\n        const { code, message, stack, data } = error.data.cause.data.cause;\n        return [getJsonRpcError(code, message, stack, data), true];\n      }\n\n      // If the JSON-RPC error is double wrapped, unwrap it further to provide the stack.\n      if (\n        isObject(error.data.cause.data) &&\n        isObject(error.data.cause.data.cause) &&\n        error.data.cause.message === error.data.cause.data.cause.message\n      ) {\n        const nestedCause = error.data.cause.data.cause;\n        const { code, message } = error.data.cause;\n        return [\n          getJsonRpcError(code, message, getErrorStack(nestedCause)),\n          false,\n        ];\n      }\n\n      // Otherwise, we use the original JSON-RPC error.\n      const { code, message, stack, data } = error.data.cause;\n      return [getJsonRpcError(code, message, stack, data), false];\n    }\n\n    // Otherwise, we throw an internal error with the wrapped error as the\n    // message.\n    return [\n      getJsonRpcError(\n        errorCodes.rpc.internal,\n        getErrorMessage(error.data.cause),\n        getErrorStack(error.data.cause),\n      ),\n      false,\n    ];\n  }\n\n  // The error can be a non-wrapped JSON-RPC error, in which case we can just\n  // re-throw it with the same code, message, and data.\n  if (isJsonRpcError(error)) {\n    const { code, message, stack, data } = error;\n    return [getJsonRpcError(code, message, stack, data), false];\n  }\n\n  // If the error is not a wrapped error, we don't know how to handle it, so we\n  // throw an internal error with the error as the message.\n  return [\n    getJsonRpcError(\n      errorCodes.rpc.internal,\n      getErrorMessage(error),\n      getErrorStack(error),\n    ),\n    false,\n  ];\n}\n\nexport const TrackableErrorStruct: Struct<TrackableError> = object({\n  name: string(),\n  message: string(),\n  stack: nullable(string()),\n  cause: nullable(lazy<TrackableError>(() => TrackableErrorStruct)),\n});\n\nexport { isSnapError, isSerializedSnapError };\n"]}