All files / apps / web/src / features / auth / lib errors.ts

100% Statements 9/9
100% Branches 8/8
100% Functions 1/1
100% Lines 6/6

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          2x       11x 7x 5x 2x     3x    
import { FirebaseError } from 'firebase/app'
import { ApiRequestError } from '@/lib/api'
import { SIGN_IN_ERROR_CODE } from '../constants'
 
// Firebase codes for a sign-in the person stopped themselves.
const CANCELLED_CODES = new Set(['auth/user-cancelled', 'auth/popup-closed-by-user'])
 
/** The code the sign-in screen explains: the API's own, or one for what Firebase reported. */
export function getSignInErrorCode(error: unknown): string {
  if (error instanceof ApiRequestError) return error.code
  if (error instanceof FirebaseError) {
    if (CANCELLED_CODES.has(error.code)) return SIGN_IN_ERROR_CODE.cancelled
    if (error.code === 'auth/network-request-failed') return 'network'
  }
 
  return SIGN_IN_ERROR_CODE.failed
}