All files / src/internal request-error.ts

100% Statements 7/7
100% Branches 6/6
100% Functions 1/1
100% Lines 7/7

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                  3x 3x   1x     3x 3x 3x 3x            
import { AzureRequestError } from '../azure-request-error'
 
/** Builds an AzureRequestError from a JSON error body; `fields` picks the service's code and message. */
export async function jsonRequestError(
  operation: string,
  response: Response,
  fields: (body: unknown) => { code: unknown; message: unknown },
): Promise<AzureRequestError> {
  let body: unknown
  try {
    body = await response.json()
  } catch {
    body = undefined
  }
 
  const { code, message } = fields(body)
  const errorCode = typeof code === 'string' ? code : undefined
  const detail = typeof message === 'string' ? message : response.statusText
  return new AzureRequestError(
    `${operation} failed: ${response.status} ${detail}${errorCode === undefined ? '' : ` (${errorCode})`}`,
    response.status,
    errorCode,
  )
}