All files / src unwrapError.ts

100% Statements 18/18
88.46% Branches 23/26
100% Functions 2/2
100% Lines 18/18

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 36 37 38 39 40 41 42 43 44 45 46 47                      7x 1x     6x 4x 4x 4x 4x       2x 1x     1x         4x 2x     2x 2x 1x 1x 1x       1x    
import { FingerprintError } from './errors'
 
/**
 * Native implementation.
 *
 * The native modules reject the promise with a structured error: React Native surfaces the rejection
 * `code` as `error.code`, the message as `error.message`, and any extra data (here, the server
 * `eventId`) under `error.userInfo`. We map that straight onto a {@link FingerprintError} — no string
 * encoding involved. Anything that doesn't carry a `code` falls back to `unknown_error`.
 */
export function unwrapError(error: unknown): FingerprintError {
  if (error instanceof FingerprintError) {
    return error
  }
 
  if (typeof error === 'object' && error !== null && 'code' in error) {
    const code = error.code
    Eif (typeof code === 'string' && code.length > 0) {
      const message = 'message' in error && typeof error.message === 'string' ? error.message : undefined
      return new FingerprintError({ code, message, event_id: readEventId(error) })
    }
  }
 
  if (error instanceof Error) {
    return new FingerprintError({ code: 'unknown_error', message: error.message })
  }
 
  return new FingerprintError({ code: 'unknown_error', message: String(error) })
}
 
/** Extracts the optional server event id React Native nests under `error.userInfo.eventId`. */
function readEventId(error: object): string | null {
  if (!('userInfo' in error)) {
    return null
  }
 
  const userInfo = error.userInfo
  if (typeof userInfo === 'object' && userInfo !== null && 'eventId' in userInfo) {
    const eventId = userInfo.eventId
    Eif (typeof eventId === 'string') {
      return eventId
    }
  }
 
  return null
}