@athenna/http 5.3.0 → 5.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@athenna/http",
3
- "version": "5.3.0",
3
+ "version": "5.5.0",
4
4
  "description": "The Athenna Http server. Built on top of fastify.",
5
5
  "license": "MIT",
6
6
  "author": "João Lenon <lenon@athenna.io>",
@@ -32,4 +32,8 @@ export declare class FastifyHandler {
32
32
  * Parse the fastify onError hook to an Athenna error handler.
33
33
  */
34
34
  static error(handler: ErrorHandler): (error: any, req: FastifyRequest, res: FastifyReply) => Promise<void>;
35
+ /**
36
+ * Parse the fastify not found route handler.
37
+ */
38
+ static notFoundError(handler: ErrorHandler): (req: FastifyRequest, res: FastifyReply) => Promise<void>;
35
39
  }
@@ -9,6 +9,7 @@
9
9
  import { Is } from '@athenna/common';
10
10
  import { Request } from '#src/context/Request';
11
11
  import { Response } from '#src/context/Response';
12
+ import { NotFoundException } from '#src/exceptions/NotFoundException';
12
13
  export class FastifyHandler {
13
14
  /**
14
15
  * Parse the fastify request handler and the preHandler hook to an Athenna
@@ -90,4 +91,20 @@ export class FastifyHandler {
90
91
  await handler(ctx);
91
92
  };
92
93
  }
94
+ /**
95
+ * Parse the fastify not found route handler.
96
+ */
97
+ static notFoundError(handler) {
98
+ return async (req, res) => {
99
+ if (!req.data) {
100
+ req.data = {};
101
+ }
102
+ const ctx = {};
103
+ ctx.data = req.data;
104
+ ctx.request = new Request(req);
105
+ ctx.response = new Response(res, ctx.request);
106
+ ctx.error = new NotFoundException(`Route ${req.method}:${req.url} not found`);
107
+ await handler(ctx);
108
+ };
109
+ }
93
110
  }
@@ -27,7 +27,7 @@ export class HttpExceptionHandler {
27
27
  async handle({ error, response }) {
28
28
  const body = {
29
29
  statusCode: Json.copy(error.statusCode) || Json.copy(error.status) || 500,
30
- code: String.toSnakeCase(error.code || error.name).toUpperCase(),
30
+ code: String.toSnakeCase(error.code || error.name || 'E_INTERNAL_SERVER').toUpperCase(),
31
31
  name: Json.copy(error.name),
32
32
  message: Json.copy(error.message),
33
33
  details: Json.copy(error.details),
@@ -69,6 +69,7 @@ export class ServerImpl {
69
69
  */
70
70
  setErrorHandler(handler) {
71
71
  this.fastify.setErrorHandler(FastifyHandler.error(handler));
72
+ this.fastify.setNotFoundHandler(FastifyHandler.notFoundError(handler));
72
73
  return this;
73
74
  }
74
75
  /**