All files ApiError.ts

100% Statements 20/20
100% Branches 15/15
100% Functions 1/1
100% Lines 17/17

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                              46x                                         46x   46x 46x 27x 46x 46x 46x   46x   46x 46x 46x 46x 46x 46x 46x 46x      
import type { RequestError } from 'got'
 
import type { AssemblyStatusError } from './alphalib/types/assemblyStatus.ts'
 
import { HTTPError } from 'got'
 
export interface TransloaditErrorResponseBody {
  error?: string
  message?: string
  reason?: AssemblyStatusError['reason']
  assembly_ssl_url?: string | null
  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
 
  /** String reason when supplied by the API, preserving the longstanding SDK contract. */
  reason?: string
 
  /** Original API reason, including structured diagnostics, without coercion. */
  rawReason?: AssemblyStatusError['reason']
 
  assemblySslUrl?: string
 
  assemblyId?: string
 
  override cause?: RequestError | undefined
 
  constructor(params: { cause?: RequestError; body: TransloaditErrorResponseBody | undefined }) {
    const { cause, body = {} } = params
 
    const parts = ['API error']
    if (cause instanceof HTTPError && 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.reason = typeof body.reason === 'string' ? body.reason : undefined
    this.rawReason = body.reason
    this.assemblyId = body.assembly_id
    this.assemblySslUrl = body.assembly_ssl_url ?? undefined
    this.code = body.error
    this.cause = cause
  }
}