All files / src/errors apiErrors.ts

91.22% Statements 52/57
68.18% Branches 30/44
79.16% Functions 19/24
94.33% Lines 50/53

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161                              8x     48x     48x 48x       8x                           40x 40x 40x 40x 40x               8x   4x       8x   2x       8x   2x       8x 2x     2x   2x       8x 5x     5x 5x       8x   3x       8x   7x       8x   5x       8x   2x       8x   2x       8x   4x       8x   2x       8x           8x       8x 8x 15x     8x 8x 19x     8x 8x         7x 7x    
import {
  CommonResponse429,
  DeleteVisit400Response,
  DeleteVisit403Response,
  DeleteVisit404Response,
  EventResponse403,
  EventResponse404,
  UpdateEventResponse400,
  UpdateEventResponse403,
  UpdateEventResponse404,
  UpdateEventResponse409,
  VisitorsResponse403,
  VisitorsResponse429,
} from '../types'
 
export class SdkError extends Error {
  constructor(
    message: string,
    readonly response?: Response,
    cause?: Error
  ) {
    super(message, { cause })
    this.name = this.constructor.name
  }
}
 
export class ApiError<Code extends number = number, Body = unknown> extends SdkError {
  // HTTP Status code
  readonly statusCode: Code
 
  // API error code
  readonly errorCode: string
 
  // API error response
  readonly responseBody: Body
 
  // Raw HTTP response
  override readonly response: Response
 
  constructor(message: string, body: Body, statusCode: Code, errorCode: string, response: Response) {
    super(message, response)
    this.responseBody = body
    this.response = response
    this.errorCode = errorCode
    this.statusCode = statusCode
  }
 
  static unknown(response: Response) {
    return new ApiError('Unknown error', undefined, response.status, response.statusText, response)
  }
}
 
export class DeleteVisit404Error extends ApiError<404, DeleteVisit404Response> {
  constructor(body: DeleteVisit404Response, response: Response) {
    super(body.error?.message ?? 'Visit not found', body, 404, body.error?.code ?? 'VisitorNotFound', response)
  }
}
 
export class DeleteVisit403Error extends ApiError<403, DeleteVisit403Response> {
  constructor(body: DeleteVisit403Response, response: Response) {
    super(body.error?.message ?? 'Forbidden', body, 403, body.error?.code ?? 'Forbidden', response)
  }
}
 
export class DeleteVisit400Error extends ApiError<400, DeleteVisit400Response> {
  constructor(body: DeleteVisit400Response, response: Response) {
    super(body.error?.message ?? 'Visit not found', body, 400, body.error?.code ?? 'RequestCannotBeParsed', response)
  }
}
 
export class CommonError429 extends ApiError<429, CommonResponse429> {
  readonly retryAfter: number = 0
 
  constructor(body: CommonResponse429, response: Response) {
    super(body.error?.message ?? 'Too many requests', body, 429, body.error?.code ?? 'TooManyRequests', response)
 
    this.retryAfter = getRetryAfter(response)
  }
}
 
export class VisitorsError429 extends ApiError<429, VisitorsResponse429> {
  readonly retryAfter: number = 0
 
  constructor(body: VisitorsResponse429, response: Response) {
    super(body.error, body, 429, 'TooManyRequests', response)
    this.retryAfter = getRetryAfter(response)
  }
}
 
export class VisitorsError403 extends ApiError<403, VisitorsResponse403> {
  constructor(body: VisitorsResponse403, response: Response) {
    super(body.error, body, 403, 'Forbidden', response)
  }
}
 
export class EventError403 extends ApiError<403, EventResponse403> {
  constructor(body: EventResponse403, response: Response) {
    super(body.error?.message ?? 'Forbidden', body, 403, body.error?.code ?? 'Forbidden', response)
  }
}
 
export class EventError404 extends ApiError<404, EventResponse404> {
  constructor(body: EventResponse404, response: Response) {
    super(body.error?.message ?? 'request id is not found', body, 404, body.error?.code ?? 'RequestNotFound', response)
  }
}
 
export class UpdateEventError400 extends ApiError<400, UpdateEventResponse400> {
  constructor(body: UpdateEventResponse400, response: Response) {
    super(body.error?.message ?? 'Bad request', body, 400, body.error?.code ?? 'BadRequest', response)
  }
}
 
export class UpdateEventError403 extends ApiError<403, UpdateEventResponse403> {
  constructor(body: UpdateEventResponse403, response: Response) {
    super(body.error?.message ?? 'Forbidden', body, 403, body.error?.code ?? 'Forbidden', response)
  }
}
 
export class UpdateEventError404 extends ApiError<404, UpdateEventResponse404> {
  constructor(body: UpdateEventResponse404, response: Response) {
    super(body.error?.message ?? 'Request id is not found', body, 404, body.error?.code ?? 'RequestNotFound', response)
  }
}
 
export class UpdateEventError409 extends ApiError<409, UpdateEventResponse409> {
  constructor(body: UpdateEventResponse409, response: Response) {
    super(body.error?.message ?? 'Conflict', body, 409, body.error?.code ?? 'Conflict', response)
  }
}
 
export const DELETE_VISITS_ERRORS = [
  DeleteVisit404Error,
  DeleteVisit403Error,
  DeleteVisit400Error,
  CommonError429,
] as const
export function isDeleteVisitorsError(error: unknown): error is (typeof DELETE_VISITS_ERRORS)[number]['prototype'] {
  return DELETE_VISITS_ERRORS.some((errorConstructor) => error instanceof errorConstructor)
}
 
export const VISITOR_ERRORS = [VisitorsError403, VisitorsError429] as const
export function isVisitorsError(error: unknown): error is (typeof VISITOR_ERRORS)[number]['prototype'] {
  return VISITOR_ERRORS.some((errorConstructor) => error instanceof errorConstructor)
}
 
export const EVENT_ERRORS = [EventError403, EventError404] as const
export function isEventError(error: unknown): error is (typeof EVENT_ERRORS)[number]['prototype'] {
  return EVENT_ERRORS.some((errorConstructor) => error instanceof errorConstructor)
}
 
export const UPDATE_EVENT_ERRORS = [UpdateEventError400, UpdateEventError403, UpdateEventError404, UpdateEventError409]
export function isUpdateEventError(error: unknown): error is (typeof UPDATE_EVENT_ERRORS)[number]['prototype'] {
  return UPDATE_EVENT_ERRORS.some((errorConstructor) => error instanceof errorConstructor)
}
 
function getRetryAfter(response: Response) {
  const retryAfter = parseInt(response.headers.get('retry-after') ?? '')
  return Number.isNaN(retryAfter) ? 0 : retryAfter
}