{"version":3,"sources":["../src/errors-api.ts"],"sourcesContent":["import type { TernSecureAPIError, TernSecureApiErrorJSON} from \"@tern-secure/types\";\n\nexport function isUnauthorizedError(e: any): boolean {\n  const status = e?.status;\n  const code = e?.errors?.[0]?.code;\n  return code === 'authentication_invalid' && status === 401;\n}\n\nexport function isCaptchaError(e: TernSecureAPIResponseError): boolean {\n  return ['captcha_invalid', 'captcha_not_enabled', 'captcha_missing_token'].includes(e.errors[0].code);\n}\n\nexport function is4xxError(e: any): boolean {\n  const status = e?.status;\n  return !!status && status >= 400 && status < 500;\n}\n\nexport function isNetworkError(e: any): boolean {\n  // TODO: revise during error handling epic\n  const message = (`${e.message}${e.name}` || '').toLowerCase().replace(/\\s+/g, '');\n  return message.includes('networkerror');\n}\n\nexport function parseErrors(data: TernSecureApiErrorJSON[] = []): TernSecureAPIError[] {\n  return data.length > 0 ? data.map(parseError) : [];\n}\n\n\nexport function parseError(error: TernSecureApiErrorJSON): TernSecureAPIError {\n  return {\n    code: error.code,\n    message: error.message,\n  };\n}\n\nexport function errorToJSON(error: TernSecureAPIError | null): TernSecureApiErrorJSON {\n  return {\n    code: error?.code || '',\n    message: error?.message || '',\n  };\n}\n\ninterface TernSecureAPIResponseOptions {\n  data: TernSecureApiErrorJSON[];\n  status: number;\n  retryAfter?: number;\n}\n\n\n\nexport class TernSecureAPIResponseError extends Error {\n  ternSecureError: true;\n\n  status: number;\n  message: string;\n  retryAfter?: number;\n\n  errors: TernSecureAPIError[];\n\n  constructor(message: string, { data, status, retryAfter }: TernSecureAPIResponseOptions) {\n    super(message);\n\n    Object.setPrototypeOf(this, TernSecureAPIResponseError.prototype);\n\n    this.status = status;\n    this.message = message;\n    this.retryAfter = retryAfter;\n    this.ternSecureError = true;\n    this.errors = parseErrors(data);\n  }\n\n  public toString = () => {\n    const message = `[${this.name}]\\nMessage:${this.message}\\nStatus:${this.status}\\nSerialized errors: ${this.errors.map(\n      e => JSON.stringify(e),\n    )}`;\n\n    return message;\n  };\n}\n\n\n\nexport class TernSecureRuntimeError extends Error {\n  ternSecureRuntimeError: true;\n\n  /**\n   * The error message.\n   *\n   * @type {string}\n   */\n  message: string;\n\n  /**\n   * A unique code identifying the error, can be used for localization.\n   *\n   * @type {string}\n   */\n  code: string;\n\n  constructor(message: string, { code }: { code: string }) {\n    const prefix = '🔒 TernSecure:';\n    const regex = new RegExp(prefix.replace(' ', '\\\\s*'), 'i');\n    const sanitized = message.replace(regex, '');\n    const _message = `${prefix} ${sanitized.trim()}\\n\\n(code=\"${code}\")\\n`;\n    super(_message);\n\n    Object.setPrototypeOf(this, TernSecureRuntimeError.prototype);\n\n    this.code = code;\n    this.message = _message;\n    this.ternSecureRuntimeError = true;\n    this.name = 'TernSecureRuntimeError';\n  }\n\n  /**\n   * Returns a string representation of the error.\n   *\n   * @returns {string} A formatted string with the error name and message.\n   */\n  public toString = () => {\n    return `[${this.name}]\\nMessage:${this.message}`;\n  };\n}"],"mappings":";AAEO,SAAS,oBAAoB,GAAiB;AAFrD;AAGE,QAAM,SAAS,uBAAG;AAClB,QAAM,QAAO,kCAAG,WAAH,mBAAY,OAAZ,mBAAgB;AAC7B,SAAO,SAAS,4BAA4B,WAAW;AACzD;AAEO,SAAS,eAAe,GAAwC;AACrE,SAAO,CAAC,mBAAmB,uBAAuB,uBAAuB,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,IAAI;AACtG;AAEO,SAAS,WAAW,GAAiB;AAC1C,QAAM,SAAS,uBAAG;AAClB,SAAO,CAAC,CAAC,UAAU,UAAU,OAAO,SAAS;AAC/C;AAEO,SAAS,eAAe,GAAiB;AAE9C,QAAM,WAAW,GAAG,EAAE,OAAO,GAAG,EAAE,IAAI,MAAM,IAAI,YAAY,EAAE,QAAQ,QAAQ,EAAE;AAChF,SAAO,QAAQ,SAAS,cAAc;AACxC;AAEO,SAAS,YAAY,OAAiC,CAAC,GAAyB;AACrF,SAAO,KAAK,SAAS,IAAI,KAAK,IAAI,UAAU,IAAI,CAAC;AACnD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