{"version":3,"file":"clerkRuntimeError-DqAmLuLY.mjs","names":[],"sources":["../../src/errors/createErrorTypeGuard.ts","../../src/errors/clerkError.ts","../../src/errors/clerkRuntimeError.ts"],"sourcesContent":["type Value = unknown;\n\n/**\n * Creates a type guard function for any error class.\n * The returned function can be called as a standalone function or as a method on an error object.\n *\n * @example\n * ```typescript\n * class MyError extends Error {}\n * const isMyError = createErrorTypeGuard(MyError);\n *\n * // As a standalone function\n * if (isMyError(error)) { ... }\n *\n * // As a method (when attached to error object)\n * if (error.isMyError()) { ... }\n * ```\n */\nexport function createErrorTypeGuard<T extends new (...args: any[]) => Value>(\n  ErrorClass: T & { kind?: string },\n): {\n  (error: Value): error is InstanceType<T>;\n  (this: Value): this is InstanceType<T>;\n} {\n  function typeGuard(this: Value, error?: Value): error is InstanceType<T> {\n    const target = error ?? this;\n    if (!target) {\n      throw new TypeError(`${ErrorClass.kind || ErrorClass.name} type guard requires an error object`);\n    }\n    // Use duck-typing with 'kind' property to handle cross-bundle scenarios\n    // where instanceof fails due to different class instances\n    if (ErrorClass.kind && typeof target === 'object' && target !== null && 'constructor' in target) {\n      const targetConstructor = (target as { constructor?: { kind?: string } }).constructor;\n      if (targetConstructor?.kind === ErrorClass.kind) {\n        return true;\n      }\n    }\n    return target instanceof ErrorClass;\n  }\n\n  return typeGuard as {\n    (error: Value): error is InstanceType<T>;\n    (this: Value): this is InstanceType<T>;\n  };\n}\n","import { createErrorTypeGuard } from './createErrorTypeGuard';\n\nexport interface ClerkErrorParams {\n  /**\n   * A message that describes the error. This is typically intented to be showed to the developers.\n   * It should not be shown to the user or parsed directly as the message contents are not guaranteed\n   * to be stable - use the `code` property instead.\n   */\n  message: string;\n  /**\n   * A machine-stable code that identifies the error.\n   */\n  code: string;\n  /**\n   * A user-friendly message that describes the error and can be displayed to the user.\n   * This message defaults to English but can be usually translated to the user's language\n   * by matching the `code` property to a localized message.\n   */\n  longMessage?: string;\n  /**\n   * The cause of the error, typically an `Error` instance that was caught and wrapped by the Clerk error handler.\n   */\n  cause?: Error;\n  /**\n   * A URL to the documentation for the error.\n   */\n  docsUrl?: string;\n}\n\n/**\n * A temporary placeholder, this will eventually be replaced with a\n * build-time flag that will actually perform DCE.\n */\nconst __DEV__ = true;\n\nexport class ClerkError extends Error {\n  static kind = 'ClerkError';\n  readonly clerkError = true as const;\n  readonly code: string;\n  readonly longMessage: string | undefined;\n  readonly docsUrl: string | undefined;\n  readonly cause: Error | undefined;\n\n  get name() {\n    return this.constructor.name;\n  }\n\n  constructor(opts: ClerkErrorParams) {\n    super(new.target.formatMessage(new.target.kind, opts.message, opts.code, opts.docsUrl), { cause: opts.cause });\n    Object.setPrototypeOf(this, ClerkError.prototype);\n    this.code = opts.code;\n    this.docsUrl = opts.docsUrl;\n    this.longMessage = opts.longMessage;\n    this.cause = opts.cause;\n  }\n\n  public toString() {\n    return `[${this.name}]\\nMessage:${this.message}`;\n  }\n\n  protected static formatMessage(name: string, msg: string, code: string, docsUrl: string | undefined) {\n    // Keeping the Clerk prefix for backward compatibility\n    // msg = `${name}: ${msg.trim()}\\n\\n(code=\"${code}\")\\n\\n`;\n    // We can remove the Clerk prefix in the next major version\n    const prefix = 'Clerk:';\n    const regex = new RegExp(prefix.replace(' ', '\\\\s*'), 'i');\n    msg = msg.replace(regex, '');\n    msg = `${prefix} ${msg.trim()}\\n\\n(code=\"${code}\")\\n\\n`;\n    if (__DEV__ && docsUrl) {\n      msg += `\\n\\nDocs: ${docsUrl}`;\n    }\n    return msg;\n  }\n}\n\n/**\n * Type guard to check if a value is a ClerkError instance.\n */\nexport function isClerkError(val: unknown): val is ClerkError {\n  const typeguard = createErrorTypeGuard(ClerkError);\n  // Ths is the base error so we're being more defensive about the type guard\n  return typeguard(val) || (!!val && typeof val === 'object' && 'clerkError' in val && val.clerkError === true);\n}\n","import type { ClerkErrorParams } from './clerkError';\nimport { ClerkError } from './clerkError';\nimport { createErrorTypeGuard } from './createErrorTypeGuard';\n\ntype ClerkRuntimeErrorOptions = Omit<ClerkErrorParams, 'message'>;\n\n/**\n * Custom error class for representing Clerk runtime errors.\n *\n * @class ClerkRuntimeError\n *\n * @example\n *   throw new ClerkRuntimeError('An error occurred', { code: 'password_invalid' });\n */\nexport class ClerkRuntimeError extends ClerkError {\n  static kind = 'ClerkRuntimeError';\n  /**\n   * @deprecated Use `clerkError` property instead. This property is maintained for backward compatibility.\n   */\n  readonly clerkRuntimeError = true as const;\n\n  constructor(message: string, options: ClerkRuntimeErrorOptions) {\n    super({ ...options, message });\n    Object.setPrototypeOf(this, ClerkRuntimeError.prototype);\n  }\n}\n\n/**\n * Type guard to check if an error is a ClerkRuntimeError.\n * Can be called as a standalone function or as a method on an error object.\n *\n * @example\n * // As a standalone function\n * if (isClerkRuntimeError(error)) { ... }\n *\n * // As a method (when attached to error object)\n * if (error.isClerkRuntimeError()) { ... }\n */\nexport const isClerkRuntimeError = createErrorTypeGuard(ClerkRuntimeError);\n"],"mappings":";;;;;;;;;;;;;;;;;AAkBA,SAAgB,qBACd,YAIA;CACA,SAAS,UAAuB,OAAyC;EACvE,MAAM,SAAS,SAAS;AACxB,MAAI,CAAC,OACH,OAAM,IAAI,UAAU,GAAG,WAAW,QAAQ,WAAW,KAAK,sCAAsC;AAIlG,MAAI,WAAW,QAAQ,OAAO,WAAW,YAAY,WAAW,QAAQ,iBAAiB,QAEvF;OAD2B,OAA+C,aACnD,SAAS,WAAW,KACzC,QAAO;;AAGX,SAAO,kBAAkB;;AAG3B,QAAO;;;;;ACLT,IAAa,aAAb,MAAa,mBAAmB,MAAM;CACpC,OAAO,OAAO;CACd,AAAS,aAAa;CACtB,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CAET,IAAI,OAAO;AACT,SAAO,KAAK,YAAY;;CAG1B,YAAY,MAAwB;AAClC,QAAM,IAAI,OAAO,cAAc,IAAI,OAAO,MAAM,KAAK,SAAS,KAAK,MAAM,KAAK,QAAQ,EAAE,EAAE,OAAO,KAAK,OAAO,CAAC;AAC9G,SAA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