Baseline: stan przed refaktoryzacja struktury
This commit is contained in:
83
api/test/unit/errorHandler.test.ts
Normal file
83
api/test/unit/errorHandler.test.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { ZodError, z } from 'zod';
|
||||
import { errorHandler, notFoundHandler } from '../../src/middleware/errorHandler.js';
|
||||
import { AppError, notFound } from '../../src/lib/errors.js';
|
||||
|
||||
function callHandler(err: unknown) {
|
||||
const res = {
|
||||
statusCode: 200,
|
||||
body: undefined as unknown,
|
||||
status(code: number) {
|
||||
this.statusCode = code;
|
||||
return this;
|
||||
},
|
||||
json(body: unknown) {
|
||||
this.body = body;
|
||||
return this;
|
||||
},
|
||||
};
|
||||
const req = { path: '/test' } as never;
|
||||
errorHandler(err, req, res as never, vi.fn());
|
||||
return res;
|
||||
}
|
||||
|
||||
describe('errorHandler', () => {
|
||||
it('maps AppError to its status and code', () => {
|
||||
const res = callHandler(notFound('Plan not found'));
|
||||
expect(res.statusCode).toBe(404);
|
||||
expect(res.body).toEqual({
|
||||
error: { code: 'NOT_FOUND', message: 'Plan not found', details: undefined },
|
||||
});
|
||||
});
|
||||
|
||||
it('maps AppError with details', () => {
|
||||
const res = callHandler(new AppError(409, 'CONFLICT', 'Taken', { email: 'x' }));
|
||||
expect(res.statusCode).toBe(409);
|
||||
expect((res.body as { error: { details: unknown } }).error.details).toEqual({ email: 'x' });
|
||||
});
|
||||
|
||||
it('maps ZodError to 400 VALIDATION_ERROR', () => {
|
||||
const zodError = new ZodError([
|
||||
{
|
||||
code: z.ZodIssueCode.invalid_type,
|
||||
expected: 'string',
|
||||
received: 'number',
|
||||
path: ['name'],
|
||||
message: 'Expected string, received number',
|
||||
},
|
||||
]);
|
||||
const res = callHandler(zodError);
|
||||
expect(res.statusCode).toBe(400);
|
||||
const body = res.body as { error: { code: string; details: { fieldErrors: Record<string, string[]> } } };
|
||||
expect(body.error.code).toBe('VALIDATION_ERROR');
|
||||
expect(body.error.details.fieldErrors.name).toEqual(['Expected string, received number']);
|
||||
});
|
||||
|
||||
it('maps unknown errors to 500 INTERNAL_ERROR without leaking details', () => {
|
||||
const res = callHandler(new Error('secret stack info'));
|
||||
expect(res.statusCode).toBe(500);
|
||||
expect(res.body).toEqual({
|
||||
error: { code: 'INTERNAL_ERROR', message: 'Internal server error' },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('notFoundHandler', () => {
|
||||
it('returns 404 JSON for unknown routes', () => {
|
||||
const res = {
|
||||
statusCode: 200,
|
||||
body: undefined as unknown,
|
||||
status(code: number) {
|
||||
this.statusCode = code;
|
||||
return this;
|
||||
},
|
||||
json(body: unknown) {
|
||||
this.body = body;
|
||||
return this;
|
||||
},
|
||||
};
|
||||
notFoundHandler({} as never, res as never);
|
||||
expect(res.statusCode).toBe(404);
|
||||
expect(res.body).toEqual({ error: { code: 'NOT_FOUND', message: 'Route not found' } });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user