diff --git a/src/entities/Font/lib/errors/errors.test.ts b/src/entities/Font/lib/errors/errors.test.ts new file mode 100644 index 0000000..1a1e22e --- /dev/null +++ b/src/entities/Font/lib/errors/errors.test.ts @@ -0,0 +1,51 @@ +import { + FontNetworkError, + FontResponseError, +} from './index'; + +describe('FontNetworkError', () => { + it('has correct name', () => { + const err = new FontNetworkError(); + expect(err.name).toBe('FontNetworkError'); + }); + + it('is instance of Error', () => { + expect(new FontNetworkError()).toBeInstanceOf(Error); + }); + + it('stores cause', () => { + const cause = new Error('network down'); + const err = new FontNetworkError(cause); + expect(err.cause).toBe(cause); + }); + + it('has default message', () => { + expect(new FontNetworkError().message).toBe('Failed to fetch fonts from proxy API'); + }); +}); + +describe('FontResponseError', () => { + it('has correct name', () => { + const err = new FontResponseError('response', undefined); + expect(err.name).toBe('FontResponseError'); + }); + + it('is instance of Error', () => { + expect(new FontResponseError('response.fonts', null)).toBeInstanceOf(Error); + }); + + it('stores field', () => { + const err = new FontResponseError('response.fonts', 42); + expect(err.field).toBe('response.fonts'); + }); + + it('stores received value', () => { + const err = new FontResponseError('response.fonts', 42); + expect(err.received).toBe(42); + }); + + it('message includes field name', () => { + const err = new FontResponseError('response.fonts', null); + expect(err.message).toContain('response.fonts'); + }); +}); diff --git a/src/entities/Font/lib/errors/index.ts b/src/entities/Font/lib/errors/index.ts new file mode 100644 index 0000000..4a49f96 --- /dev/null +++ b/src/entities/Font/lib/errors/index.ts @@ -0,0 +1,28 @@ +/** + * Thrown when the network request to the proxy API fails. + * Wraps the underlying fetch error (timeout, DNS failure, connection refused, etc.). + */ +export class FontNetworkError extends Error { + readonly name = 'FontNetworkError'; + + constructor(public readonly cause?: unknown) { + super('Failed to fetch fonts from proxy API'); + } +} + +/** + * Thrown when the proxy API returns a response with an unexpected shape. + * + * @property field - The name of the field that failed validation (e.g. `'response'`, `'response.fonts'`). + * @property received - The actual value received at that field, for debugging. + */ +export class FontResponseError extends Error { + readonly name = 'FontResponseError'; + + constructor( + public readonly field: string, + public readonly received: unknown, + ) { + super(`Invalid proxy API response: ${field}`); + } +}