File size: 1,395 Bytes
a8fb296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, expect, it } from 'vitest'
import { ApiError, apiErrorMessage } from './apiErrorMessage'

describe('apiErrorMessage (#888)', () => {
  it("reads FastAPI's detail", () => {
    expect(apiErrorMessage({ detail: 'No Lighthouse audit found for this URL' }, 404)).toBe(
      'No Lighthouse audit found for this URL',
    )
  })

  it("reads this API's message when there is no detail", () => {
    const body = { error: 'Not Found', message: 'No Lighthouse audit found for this URL (run the scan).', path: '/api/x' }
    expect(apiErrorMessage(body, 404)).toBe('No Lighthouse audit found for this URL (run the scan).')
  })

  it('joins validation messages', () => {
    expect(apiErrorMessage({ detail: [{ msg: 'Field required' }, { msg: 'Too short' }] }, 422)).toBe(
      'Field required; Too short',
    )
  })

  it('falls back to the status code only when the body says nothing', () => {
    expect(apiErrorMessage(null, 404)).toBe('HTTP 404')
    expect(apiErrorMessage({}, 500)).toBe('HTTP 500')
    expect(apiErrorMessage({ detail: '   ' }, 502)).toBe('HTTP 502')
    expect(apiErrorMessage({ error: 'Bad Gateway' }, 502)).toBe('Bad Gateway')
  })

  it('keeps the status on the error', () => {
    const e = new ApiError('nothing stored', 404)
    expect(e.status).toBe(404)
    expect(e.message).toBe('nothing stored')
    expect(e instanceof Error).toBe(true)
  })
})