Skip to content

Commit

Permalink
Added a decorator for custom errors and a domain-level error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
elizachi committed Jan 11, 2024
1 parent 000541b commit 1d7a1de
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 8 deletions.
24 changes: 24 additions & 0 deletions src/presentation/http/decorators/domainError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { StatusCodes } from 'http-status-codes';
import type { FastifyReply } from 'fastify';

/**
* Custom method for sending 500 error
*
* Send this error when a domain-level error is thrown
*
* @example
*
* if (note.creatorId !== userId) {
* return reply.domainError('Note with id ${id} was not updated');
* }
*
* @param message - custom message
*/
export default async function domainError(this: FastifyReply, message = 'Domain level error'): Promise<void> {
await this
.code(StatusCodes.INTERNAL_SERVER_ERROR)
.type('application/json')
.send({
message,
});
}
4 changes: 3 additions & 1 deletion src/presentation/http/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import notFound from './notFound.js';
import forbidden from './forbidden.js';
import unauthorized from './unauthorized.js';
import notAcceptable from './notAcceptable.js';
import domainError from './domainError.js';

export {
notFound,
forbidden,
unauthorized,
notAcceptable
notAcceptable,
domainError
};
15 changes: 15 additions & 0 deletions src/presentation/http/fastify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,20 @@ declare module 'fastify' {
* @param message - Optional message to send. If not specified, default message will be sent
*/
notAcceptable: (message?: string) => Promise<void>;

/**
* Custom method for sending 500 error
*
* Send this error when a domain-level error is thrown
*
* @example
*
* if (note.creatorId !== userId) {
* return reply.domainError('Note with id ${id} was not updated');
* }
*
* @param message - Optional message to send. If not specified, default message will be sent
*/
domainError: (message?: string) => Promise<void>;
}
}
20 changes: 19 additions & 1 deletion src/presentation/http/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUI from '@fastify/swagger-ui';
import addUserIdResolver from '@presentation/http/middlewares/common/userIdResolver.js';
import cookie from '@fastify/cookie';
import { notFound, forbidden, unauthorized, notAcceptable } from './decorators/index.js';
import { notFound, forbidden, unauthorized, notAcceptable, domainError } from './decorators/index.js';
import NoteRouter from '@presentation/http/router/note.js';
import OauthRouter from '@presentation/http/router/oauth.js';
import AuthRouter from '@presentation/http/router/auth.js';
Expand Down Expand Up @@ -79,6 +79,8 @@ export default class HttpApi implements Api {
this.addPoliciesCheckHook();

await this.addApiRoutes(domainServices);

this.domainErrorHandler();
}


Expand Down Expand Up @@ -278,6 +280,7 @@ export default class HttpApi implements Api {
this.server?.decorateReply('forbidden', forbidden);
this.server?.decorateReply('unauthorized', unauthorized);
this.server?.decorateReply('notAcceptable', notAcceptable);
this.server?.decorateReply('domainError', domainError);
}

/**
Expand Down Expand Up @@ -320,5 +323,20 @@ export default class HttpApi implements Api {
});
});
}

/**
* Domain error handler
*/
private domainErrorHandler(): void {
this.server?.setErrorHandler(function (error, request, reply) {
const statusCode = error.statusCode;

// eslint-disable-next-line @typescript-eslint/no-magic-numbers
if (statusCode === 500) {
this.log.error(error);
void reply.domainError(error.message);
}
});
}
}

6 changes: 0 additions & 6 deletions src/presentation/http/router/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,6 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
});
});

fastify.setErrorHandler(function (error, request, reply) {
this.log.error(error);
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
void reply.status(409).send({ ok: false });
});

done();
};

Expand Down

0 comments on commit 1d7a1de

Please sign in to comment.