All files / src/errors apiErrors.ts

100% Statements 34/34
100% Branches 7/7
100% Functions 7/7
100% Lines 34/34

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    1x 1x 52x 52x 52x 52x 52x 52x 52x 1x   1x   1x     34x     34x     34x   1x 34x 34x 34x 34x 34x 34x   1x 8x 8x   1x 11x 11x 1x         1x 1x 3x 3x 1x  
import { ErrorResponse } 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 RequestError<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 RequestError('Unknown error', undefined, response.status, response.statusText, response)
  }
 
  static fromErrorResponse(body: ErrorResponse, response: Response) {
    return new RequestError(body.error.message, body, response.status, body.error.code, response)
  }
}
 
/**
 * Error that indicate that the request was throttled.
 * */
export class TooManyRequestsError extends RequestError<429, ErrorResponse> {
  constructor(body: ErrorResponse, response: Response) {
    super(body.error.message, body, 429, body.error.code, response)
  }
}