Skip to content

A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.

1.3 KB
Raw Blame
import { httpStatus, summarizeError } from '../error_summary'

function axiosError(status?: number, url = 'https://example.com/api') {
  return {
    isAxiosError: true,
    message: 'Request failed with status code 403',
    code: status ? undefined : 'ECONNABORTED',
    config: { url },
    response: status ? { status } : undefined
  }
}

describe('summarizeError', () => {
  it('reports the HTTP status and the url', () => {
    expect(summarizeError(axiosError(403))).toBe(
      'HTTP 403 (https://example.com/api)'
    )
  })

  it('reports the axios code when there is no response', () => {
    expect(summarizeError(axiosError())).toBe(
      'ECONNABORTED (https://example.com/api)'
    )
  })

  it('falls back to the message of a regular error', () => {
    expect(summarizeError(new Error('Failed'))).toBe('Failed')
  })

  it('never throws on unexpected values', () => {
    expect(summarizeError(undefined)).toBe('unknown error')
    expect(summarizeError('oops')).toBe('oops')
  })
})

describe('httpStatus', () => {
  it('reads the status of an axios error', () => {
    expect(httpStatus(axiosError(429))).toBe(429)
  })

  it('returns nothing for other values', () => {
    expect(httpStatus(new Error('Failed'))).toBeUndefined()
    expect(httpStatus(null)).toBeUndefined()
  })
})