All files ApiError.ts

100% Statements 18/18
100% Branches 6/6
100% Functions 1/1
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                  1x 1x                     1x 10x   10x 10x 10x 10x 10x   10x   10x 10x 10x 10x 10x 10x 10x 1x  
import { HTTPError } from 'got'
 
export interface TransloaditErrorResponseBody {
  error?: string
  message?: string
  assembly_ssl_url?: string
  assembly_id?: string
}
 
export class ApiError extends Error {
  override name = 'ApiError'
 
  // there might not be an error code (or message) if the server didn't respond with any JSON response at all
  // e.g. if there was a 500 in the HTTP reverse proxy
  code?: string
  rawMessage?: string
  assemblySslUrl?: string
  assemblyId?: string
 
  override cause?: HTTPError | undefined
 
  constructor(params: { cause?: HTTPError; body: TransloaditErrorResponseBody | undefined }) {
    const { cause, body = {} } = params
 
    const parts = ['API error']
    if (cause?.response.statusCode) parts.push(`(HTTP ${cause.response.statusCode})`)
    if (body.error) parts.push(`${body.error}:`)
    if (body.message) parts.push(body.message)
    if (body.assembly_ssl_url) parts.push(body.assembly_ssl_url)
 
    const message = parts.join(' ')
 
    super(message)
    this.rawMessage = body.message
    this.assemblyId = body.assembly_id
    this.assemblySslUrl = body.assembly_ssl_url
    this.code = body.error
    this.cause = cause
  }
}