{"version":3,"file":"TRPCClientError-Byo7P4QG.mjs","names":[],"sources":["../src/TRPCClientError.ts"],"sourcesContent":["import type {\n  inferClientTypes,\n  InferrableClientTypes,\n  Maybe,\n  TRPCErrorResponse,\n} from '@trpc/server/unstable-core-do-not-import';\nimport {\n  isObject,\n  type DefaultErrorShape,\n} from '@trpc/server/unstable-core-do-not-import';\n\ntype inferErrorShape<TInferrable extends InferrableClientTypes> =\n  inferClientTypes<TInferrable>['errorShape'];\n/** What a client error has, whatever its router. */\nexport interface TRPCClientErrorBase<TShape extends DefaultErrorShape> {\n  readonly message: string;\n  /** The error as the server's error formatter shaped it, if it came from the server. */\n  readonly shape: Maybe<TShape>;\n  /** The shape's `data`, such as the tRPC code and HTTP status. */\n  readonly data: Maybe<TShape['data']>;\n}\n/**\n * The shape of a client error for a router or procedure, without the\n * class. Use it to type errors you pass around, such as in React state.\n * @see https://trpc.io/docs/client/vanilla/infer-types#infer-trpcclienterror-types\n */\nexport type TRPCClientErrorLike<TInferrable extends InferrableClientTypes> =\n  TRPCClientErrorBase<inferErrorShape<TInferrable>>;\n\n/**\n * Whether a value is a `TRPCClientError`, typed for a router or procedure.\n * @see https://trpc.io/docs/client/vanilla/error-handling#catching-a-thrown-error\n */\nexport function isTRPCClientError<TInferrable extends InferrableClientTypes>(\n  cause: unknown,\n): cause is TRPCClientError<TInferrable> {\n  return cause instanceof TRPCClientError;\n}\n\nfunction isTRPCErrorResponse(obj: unknown): obj is TRPCErrorResponse<any> {\n  return (\n    isObject(obj) &&\n    isObject(obj['error']) &&\n    typeof obj['error']['code'] === 'number' &&\n    typeof obj['error']['message'] === 'string'\n  );\n}\n\nfunction getMessageFromUnknownError(err: unknown, fallback: string): string {\n  if (typeof err === 'string') {\n    return err;\n  }\n  if (isObject(err) && typeof err['message'] === 'string') {\n    return err['message'];\n  }\n  return fallback;\n}\n\n/**\n * The error a client call throws, whether the server sent an error or the\n * request failed.\n * @see https://trpc.io/docs/client/vanilla/error-handling\n */\nexport class TRPCClientError<TRouterOrProcedure extends InferrableClientTypes>\n  extends Error\n  implements TRPCClientErrorBase<inferErrorShape<TRouterOrProcedure>>\n{\n  // oxlint-disable-next-line typescript/ban-ts-comment\n  // @ts-ignore override doesn't work in all environments due to \"This member cannot have an 'override' modifier because it is not declared in the base class 'Error'\"\n  /** The error this one wraps, such as a network error. */\n  public override readonly cause;\n  /** The error as the server's error formatter shaped it, if it came from the server. */\n  public readonly shape: Maybe<inferErrorShape<TRouterOrProcedure>>;\n  /** The shape's `data`, such as the tRPC code and HTTP status. */\n  public readonly data: Maybe<inferErrorShape<TRouterOrProcedure>['data']>;\n\n  /**\n   * More about the error, if any. HTTP errors may have the `response` and\n   * its `responseJSON`; a failed fetch or unparsable response may have\n   * neither.\n   */\n  public meta;\n\n  constructor(\n    message: string,\n    opts?: {\n      result?: Maybe<TRPCErrorResponse<inferErrorShape<TRouterOrProcedure>>>;\n      cause?: Error;\n      meta?: Record<string, unknown>;\n    },\n  ) {\n    const cause = opts?.cause;\n\n    // oxlint-disable-next-line typescript/ban-ts-comment\n    // @ts-ignore https://github.com/tc39/proposal-error-cause\n    super(message, { cause });\n\n    this.meta = opts?.meta;\n\n    this.cause = cause;\n    this.shape = opts?.result?.error;\n    this.data = opts?.result?.error.data;\n    this.name = 'TRPCClientError';\n\n    Object.setPrototypeOf(this, TRPCClientError.prototype);\n  }\n\n  /**\n   * Turns an error response, an `Error` or another object into a\n   * `TRPCClientError`. A `TRPCClientError` is returned as it is.\n   */\n  public static from<TRouterOrProcedure extends InferrableClientTypes>(\n    _cause: Error | TRPCErrorResponse<any> | object,\n    opts: { meta?: Record<string, unknown>; cause?: Error } = {},\n  ): TRPCClientError<TRouterOrProcedure> {\n    const cause = _cause as unknown;\n\n    if (isTRPCClientError(cause)) {\n      if (opts.meta) {\n        // Decorate with meta error data\n        cause.meta = {\n          ...cause.meta,\n          ...opts.meta,\n        };\n      }\n      return cause;\n    }\n    if (isTRPCErrorResponse(cause)) {\n      return new TRPCClientError(cause.error.message, {\n        ...opts,\n        result: cause,\n        cause: opts.cause,\n      });\n    }\n    return new TRPCClientError(\n      getMessageFromUnknownError(cause, 'Unknown error'),\n      {\n        ...opts,\n        cause: cause as any,\n      },\n    );\n  }\n}\n"],"mappings":";;;;;;;AAiCA,SAAgB,kBACd,OACuC;CACvC,OAAO,iBAAiB;AAC1B;AAEA,SAAS,oBAAoB,KAA6C;CACxE,OACE,SAAS,GAAG,KACZ,SAAS,IAAI,QAAQ,KACrB,OAAO,IAAI,QAAQ,CAAC,YAAY,YAChC,OAAO,IAAI,QAAQ,CAAC,eAAe;AAEvC;AAEA,SAAS,2BAA2B,KAAc,UAA0B;CAC1E,IAAI,OAAO,QAAQ,UACjB,OAAO;CAET,IAAI,SAAS,GAAG,KAAK,OAAO,IAAI,eAAe,UAC7C,OAAO,IAAI;CAEb,OAAO;AACT;;;;;;AAOA,IAAa,kBAAb,MAAa,wBACH,MAEV;CAiBE,YACE,SACA,MAKA;;EACA,MAAM,QAAA,SAAA,QAAA,SAAA,KAAA,IAAA,KAAA,IAAQ,KAAM;EAIpB,MAAM,SAAS,EAAE,MAAM,CAAC;EAzBD,gBAAA,MAAA,SAAA,KAAA,CAAA;EAET,gBAAA,MAAA,SAAA,KAAA,CAAA;EAEA,gBAAA,MAAA,QAAA,KAAA,CAAA;EAOT,gBAAA,MAAA,QAAA,KAAA,CAAA;EAgBL,KAAK,OAAA,SAAA,QAAA,SAAA,KAAA,IAAA,KAAA,IAAO,KAAM;EAElB,KAAK,QAAQ;EACb,KAAK,QAAA,SAAA,QAAA,SAAA,KAAA,MAAA,eAAQ,KAAM,YAAA,QAAA,iBAAA,KAAA,IAAA,KAAA,IAAA,aAAQ;EAC3B,KAAK,OAAA,SAAA,QAAA,SAAA,KAAA,MAAA,gBAAO,KAAM,YAAA,QAAA,kBAAA,KAAA,IAAA,KAAA,IAAA,cAAQ,MAAM;EAChC,KAAK,OAAO;EAEZ,OAAO,eAAe,MAAM,gBAAgB,SAAS;CACvD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