at main 58 lines 2.0 kB view raw
1import {describe, expect, it} from '@jest/globals' 2 3import {ProtocolError, isProtocolError, normalizeProtocolError} from '#common/errors' 4 5describe('isProtocolError', () => { 6 it('identifies protocol errors', () => { 7 const protocolError = new ProtocolError('test', 400) 8 const regularError = new Error('test') 9 10 expect(isProtocolError(protocolError, 400)).toBe(true) 11 expect(isProtocolError(protocolError)).toBe(true) 12 expect(isProtocolError(regularError, 400)).toBe(false) 13 expect(isProtocolError(regularError)).toBe(false) 14 }) 15 16 it('checks specific status codes', () => { 17 const error400 = new ProtocolError('bad request', 400) 18 const error500 = new ProtocolError('server error', 500) 19 20 expect(isProtocolError(error400, 400)).toBe(true) 21 expect(isProtocolError(error400, 500)).toBe(false) 22 expect(isProtocolError(error500, 500)).toBe(true) 23 }) 24}) 25 26describe('normalizeProtocolError', () => { 27 it('passes through existing protocol errors', () => { 28 const original = new ProtocolError('test', 400) 29 const normalized = normalizeProtocolError(original) 30 31 expect(normalized).toBe(original) 32 }) 33 34 it('converts regular errors to protocol errors', () => { 35 const error = new Error('test error') 36 const normalized = normalizeProtocolError(error) 37 38 expect(normalized).toBeInstanceOf(ProtocolError) 39 expect(normalized.status).toBe(500) 40 expect(normalized.cause).toBe(error) 41 }) 42 43 it('handles timeout errors', () => { 44 const timeoutError = new DOMException('timeout', 'TimeoutError') 45 const normalized = normalizeProtocolError(timeoutError) 46 47 expect(normalized.status).toBe(408) 48 expect(normalized.message).toContain('operation timed out') 49 }) 50 51 it('handles abort errors', () => { 52 const abortError = new DOMException('aborted', 'AbortError') 53 const normalized = normalizeProtocolError(abortError) 54 55 expect(normalized.status).toBe(499) 56 expect(normalized.message).toContain('operation was aborted') 57 }) 58})