Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import {describe, expect, it} from '@jest/globals'
import {ProtocolError, isProtocolError, normalizeProtocolError} from '#common/errors'
describe('isProtocolError', () => { it('identifies protocol errors', () => { const protocolError = new ProtocolError('test', 400) const regularError = new Error('test')
expect(isProtocolError(protocolError, 400)).toBe(true) expect(isProtocolError(protocolError)).toBe(true) expect(isProtocolError(regularError, 400)).toBe(false) expect(isProtocolError(regularError)).toBe(false) })
it('checks specific status codes', () => { const error400 = new ProtocolError('bad request', 400) const error500 = new ProtocolError('server error', 500)
expect(isProtocolError(error400, 400)).toBe(true) expect(isProtocolError(error400, 500)).toBe(false) expect(isProtocolError(error500, 500)).toBe(true) })})
describe('normalizeProtocolError', () => { it('passes through existing protocol errors', () => { const original = new ProtocolError('test', 400) const normalized = normalizeProtocolError(original)
expect(normalized).toBe(original) })
it('converts regular errors to protocol errors', () => { const error = new Error('test error') const normalized = normalizeProtocolError(error)
expect(normalized).toBeInstanceOf(ProtocolError) expect(normalized.status).toBe(500) expect(normalized.cause).toBe(error) })
it('handles timeout errors', () => { const timeoutError = new DOMException('timeout', 'TimeoutError') const normalized = normalizeProtocolError(timeoutError)
expect(normalized.status).toBe(408) expect(normalized.message).toContain('operation timed out') })
it('handles abort errors', () => { const abortError = new DOMException('aborted', 'AbortError') const normalized = normalizeProtocolError(abortError)
expect(normalized.status).toBe(499) expect(normalized.message).toContain('operation was aborted') })})