Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/presentation/http/http-api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { describe, test, expect } from 'vitest';

describe('HTTP API Error Handler', () => {
describe('JSON Parse Error Handler', () => {
test('Returns 400 when request body contains not complete JSON', async () => {
const response = await global.api?.fakeRequest({
method: 'POST',
url: '/join/test-hash1',
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'Content-Type': 'application/json',
},
body: '{invalid json',
});

expect(response?.statusCode).toBe(400);

const body = await response?.json();

expect(body).toStrictEqual({
message: 'Invalid JSON in request body',
});
});

test('Returns 400 when request body contains malformed JSON', async () => {
const response = await global.api?.fakeRequest({
method: 'POST',
url: '/join/test-hash1',
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'Content-Type': 'application/json',
},
body: '{"key": "value",}',
});

expect(response?.statusCode).toBe(400);

const body = await response?.json();

expect(body).toStrictEqual({
message: 'Invalid JSON in request body',
});
});

test('Returns 400 when JSON body is empty', async () => {
const response = await global.api?.fakeRequest({
method: 'POST',
url: '/join/test-hash1',
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'Content-Type': 'application/json',
},
body: '',
});

expect(response?.statusCode).toBe(400);

const body = await response?.json();

expect(body).toStrictEqual({
message: 'Invalid JSON in request body',
});
});

test('Does not return 400 for valid JSON', async () => {
const response = await global.api?.fakeRequest({
method: 'POST',
url: '/join/test-hash1',
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'Content-Type': 'application/json',
},
body: '{"key": "value"}',
});

expect(response?.statusCode).not.toBe(400);
});
});
});
28 changes: 24 additions & 4 deletions src/presentation/http/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ import UploadRouter from './router/upload.js';
import { ajvFilePlugin } from '@fastify/multipart';
import { UploadSchema } from './schema/Upload.js';
import { NoteHierarchySchema } from './schema/NoteHierarchy.js';
import { StatusCodes } from 'http-status-codes';

interface FastifyError extends Error {
code: string;
}

const appServerLogger = getLogger('appServer');

Expand Down Expand Up @@ -78,7 +83,7 @@ export default class HttpApi implements Api {
* └── your services
* @see https://fastify.dev/docs/latest/Guides/Getting-Started#loading-order-of-your-plugins
*/
this.domainErrorHandler();
this.globalErrorHandler();

await this.addCookies();
await this.addOpenapiDocs();
Expand Down Expand Up @@ -359,9 +364,9 @@ export default class HttpApi implements Api {
}

/**
* Domain error handler
* Global error handler
*/
private domainErrorHandler(): void {
private globalErrorHandler(): void {
this.server?.setErrorHandler(function (error, request, reply) {
/**
* If we have an error that occurs in the domain-level we reply it with special format
Expand All @@ -373,7 +378,22 @@ export default class HttpApi implements Api {
return;
}
/**
* If error is not a domain error, we route it to the default error handler
* JSON parse errors (invalid request body)
* Errors can be either SyntaxError or FastifyError.
*/
if ((error instanceof SyntaxError && error.message.includes('JSON'))
|| ((error as FastifyError).code?.startsWith('FST_ERR_CTP_') ?? false)) {
Comment thread
e11sy marked this conversation as resolved.
this.log.warn({ reqId: request.id }, 'Invalid JSON in request body');

return reply
.code(StatusCodes.BAD_REQUEST)
.type('application/json')
.send({
message: 'Invalid JSON in request body',
});
Comment thread
7eliassen marked this conversation as resolved.
}
/**
* If error is not a known type, we route it to the default error handler
*/
throw error;
});
Expand Down
Loading