All files / src unwrapError.web.ts

100% Statements 8/8
100% Branches 10/10
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35                      7x 1x     6x 4x     2x 1x     1x                 4x    
import { FingerprintError } from './errors'
import { isFingerprintError } from '@fingerprint/agent'
 
/**
 * Web implementation.
 *
 * `@fingerprint/agent` already throws its own `FingerprintError` (structurally identical to ours).
 * We re-wrap it in the SDK's {@link FingerprintError} so `instanceof` checks work against a single
 * class regardless of platform.
 */
export function unwrapError(error: unknown): FingerprintError {
  if (error instanceof FingerprintError) {
    return error
  }
 
  if (isFingerprintError(error)) {
    return new FingerprintError({ code: normalizeCode(error.code), message: error.message, event_id: error.event_id })
  }
 
  if (error instanceof Error) {
    return new FingerprintError({ code: 'unknown_error', message: error.message })
  }
 
  return new FingerprintError({ code: 'unknown_error', message: String(error) })
}
 
/**
 * The agent splits network failures into `network_connection` and `network_abort`; the native
 * clients report a single `network_error`. Collapse both into `network_error` so the code is the
 * same on every platform.
 */
function normalizeCode(code: string) {
  return code === 'network_connection' || code === 'network_abort' ? 'network_error' : code
}