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,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC;AAC9D,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,MAAM,KAAK,GAAG,iBAAiB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAC/C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;QACzD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,CAAC,KAAK,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,KAAc;IAC1C,OAAO,iBAAiB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AAC3C,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,MAAM,KAAK,GAAG,iBAAiB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAC/C,IAAI,KAAK,KAAK,IAAI,IAAI,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAClE,OAAO,KAA6B,CAAC;IACvC,CAAC;IAED,OAAO,EAAE,CAAC;AACZ,CAAC","sourcesContent":["import type { Json } from '@metamask/utils';\nimport { hasProperty, isObject, isValidJson } from '@metamask/utils';\n\nexport const SNAP_ERROR_CODE = -31002;\nexport const SNAP_ERROR_MESSAGE = 'Snap Error';\n\n/**\n * Get a property from an object, or return a fallback value if the property\n * does not exist.\n *\n * @param object - The object to get the property from.\n * @param property - The property to get from the object.\n * @param fallback - The fallback value to return if the property does not\n * exist.\n * @returns The value of the property if it exists, or the fallback value if\n * the property does not exist.\n */\nfunction getObjectProperty<Fallback = null>(\n  object: unknown,\n  property: string,\n  fallback: Fallback = null as Fallback,\n): unknown {\n  if (isObject(object) && hasProperty(object, property)) {\n    return object[property];\n  }\n\n  return fallback;\n}\n\n/**\n * Get a string property from an object, or convert the object to a string\n * if the property does not exist or is not a string.\n *\n * @param object - The object to get the property from.\n * @param property - The property to get from the object.\n * @param fallback - The fallback value to return if the property does not exist\n * or is not a string. Defaults to the string representation of the object.\n * @returns The value of the property if it exists and is a string, or the\n * fallback value if it does not exist or is not a string.\n */\nfunction getObjectStringProperty<Fallback = string>(\n  object: unknown,\n  property: string,\n  fallback: Fallback = String(object) as Fallback,\n): string | Fallback {\n  const value = getObjectProperty(object, property);\n  if (typeof value === 'string') {\n    return value;\n  }\n\n  return fallback;\n}\n\n/**\n * Get the error message from an unknown error type.\n *\n * - If the error is an object with a `message` property, return the message.\n * - Otherwise, return the error converted to a string.\n *\n * @param error - The error to get the message from.\n * @returns The error message.\n * @internal\n */\nexport function getErrorMessage(error: unknown) {\n  return getObjectStringProperty(error, 'message');\n}\n\n/**\n * Get the error stack from an unknown error type.\n *\n * @param error - The error to get the stack from.\n * @returns The error stack, or undefined if the error does not have a valid\n * stack.\n * @internal\n */\nexport function getErrorStack(error: unknown) {\n  return getObjectStringProperty(error, 'stack', null);\n}\n\n/**\n * Get the error name from an unknown error type.\n *\n * @param error - The error to get the name from.\n * @returns The error name, or `'Error'` if the error does not have a valid\n * name.\n */\nexport function getErrorName(error: unknown) {\n  const fallbackName = error instanceof Error ? error.name : 'Error';\n  return getObjectStringProperty(error, 'name', fallbackName);\n}\n\n/**\n * Get the error code from an unknown error type.\n *\n * @param error - The error to get the code from.\n * @returns The error code, or `-32603` if the error does not have a valid code.\n * @internal\n */\nexport function getErrorCode(error: unknown) {\n  const value = getObjectProperty(error, 'code');\n  if (typeof value === 'number' && Number.isInteger(value)) {\n    return value;\n  }\n\n  return -32603;\n}\n\n/**\n * Get the error cause from an unknown error type.\n *\n * @param error - The error to get the cause from.\n * @returns The error cause, or `null` if the error does not have a valid\n * cause.\n */\nexport function getErrorCause(error: unknown) {\n  return getObjectProperty(error, 'cause');\n}\n\n/**\n * Get the error data from an unknown error type.\n *\n * @param error - The error to get the data from.\n * @returns The error data, or an empty object if the error does not have valid\n * data.\n * @internal\n */\nexport function getErrorData(error: unknown) {\n  const value = getObjectProperty(error, 'data');\n  if (value !== null && isValidJson(value) && !Array.isArray(value)) {\n    return value as Record<string, Json>;\n  }\n\n  return {};\n}\n"]}