framework-do-dede 0.0.37 → 0.0.39

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.
@@ -1,5 +1,5 @@
1
1
  import { Registry } from "../di/registry";
2
- import { ServerError } from "../http";
2
+ import { InternalServerError } from "../http";
3
3
  export default class ControllerHandler {
4
4
  constructor(httpServer, port) {
5
5
  for (const { instance, instanceMethod, middlewares, method, route, statusCode, params, query, validation, offLogs, metricsHandlers } of this.registryControllers()) {
@@ -56,8 +56,9 @@ export default class ControllerHandler {
56
56
  catch (error) {
57
57
  if (!offLogs)
58
58
  console.log('\x1b[31m%s\x1b[0m', `❌ [LOG] Erro: "${logger.handler.instance}.${logger.handler.method}:" - error: ${error.message}`);
59
- requestMetrics.error = this.extractError(error);
60
- return error;
59
+ const capturedError = this.extractError(error);
60
+ requestMetrics.error = capturedError;
61
+ return capturedError;
61
62
  }
62
63
  finally {
63
64
  requestMetrics.endTime = performance.now();
@@ -122,16 +123,10 @@ export default class ControllerHandler {
122
123
  return filter;
123
124
  }
124
125
  extractError(error) {
125
- let statusCode;
126
- if (error instanceof ServerError)
127
- statusCode = error.getStatusCode();
128
- else
129
- statusCode = 500;
130
126
  return {
131
- stack: error.stack?.toString(),
132
- originalMessage: error.message,
133
127
  message: error.message,
134
- statusCode
128
+ statusCode: error.getStatusCode(),
129
+ unexpectedError: error instanceof InternalServerError ? error.getUnexpectedError() : undefined,
135
130
  };
136
131
  }
137
132
  }
@@ -19,10 +19,11 @@ type FrameworkWeb = {
19
19
  export default abstract class HttpServer {
20
20
  protected framework: FrameworkWeb;
21
21
  protected frameworkName: string;
22
- protected defaultMessageError: string;
22
+ private defaultMessageError;
23
23
  constructor(framework: FrameworkWeb, frameworkName: 'elysia' | 'express');
24
24
  use(middleware: CallableFunction): HttpServer;
25
25
  register(httpServerParams: HttpServerParams, handler: CallableFunction): void;
26
+ setDefaultMessageError(message: string): void;
26
27
  listen(port: number): void;
27
28
  private mountRoute;
28
29
  private elysia;
@@ -1,9 +1,9 @@
1
1
  import { FrameworkError } from "./FrameworkError";
2
- import { ServerError } from "./ServerError";
2
+ import { InternalServerError, ServerError } from "./ServerError";
3
3
  export default class HttpServer {
4
4
  framework;
5
5
  frameworkName;
6
- defaultMessageError = 'Ops, An unexpected error occurred';
6
+ defaultMessageError = '';
7
7
  constructor(framework, frameworkName) {
8
8
  if (frameworkName !== 'elysia' && frameworkName !== 'express')
9
9
  throw new FrameworkError('Framework not supported');
@@ -20,6 +20,9 @@ export default class HttpServer {
20
20
  return this.elysia(httpServerParams, route, handler);
21
21
  return this.express(httpServerParams, route, handler);
22
22
  }
23
+ setDefaultMessageError(message) {
24
+ this.defaultMessageError = message;
25
+ }
23
26
  listen(port) {
24
27
  this.framework.listen(port);
25
28
  }
@@ -50,16 +53,10 @@ export default class HttpServer {
50
53
  catch (error) {
51
54
  if (error instanceof ServerError) {
52
55
  set.status = error.getStatusCode();
53
- return {
54
- error: error.message,
55
- statusCode: error.getStatusCode()
56
- };
56
+ throw error;
57
57
  }
58
58
  set.status = 500;
59
- return {
60
- error: this.defaultMessageError,
61
- statusCode: 500
62
- };
59
+ throw new InternalServerError(error.message, this.defaultMessageError);
63
60
  }
64
61
  });
65
62
  }
@@ -78,15 +75,11 @@ export default class HttpServer {
78
75
  }
79
76
  catch (error) {
80
77
  if (error instanceof ServerError) {
81
- return res.status(error.getStatusCode()).json({
82
- error: error.message,
83
- statusCode: error.getStatusCode()
84
- });
78
+ res.status(error.getStatusCode());
79
+ throw error;
85
80
  }
86
- return res.status(500).json({
87
- error: this.defaultMessageError,
88
- statusCode: 500
89
- });
81
+ res.status(500);
82
+ throw new InternalServerError(error.message, this.defaultMessageError);
90
83
  }
91
84
  });
92
85
  }
@@ -21,3 +21,8 @@ export declare class Unauthorized extends ServerError {
21
21
  export declare class BadRequest extends ServerError {
22
22
  constructor(message: string);
23
23
  }
24
+ export declare class InternalServerError extends ServerError {
25
+ private unexpectedError;
26
+ constructor(unexpectedError: string, defaultMessage?: string);
27
+ getUnexpectedError(): string;
28
+ }
@@ -39,3 +39,13 @@ export class BadRequest extends ServerError {
39
39
  super(message, 400);
40
40
  }
41
41
  }
42
+ export class InternalServerError extends ServerError {
43
+ unexpectedError;
44
+ constructor(unexpectedError, defaultMessage = 'Ops, An unexpected error occurred') {
45
+ super(defaultMessage, 500);
46
+ this.unexpectedError = unexpectedError;
47
+ }
48
+ getUnexpectedError() {
49
+ return this.unexpectedError;
50
+ }
51
+ }
@@ -1,3 +1,3 @@
1
1
  import HttpServer from "./HttpServer";
2
- import { ServerError, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, BadRequest } from "./ServerError";
3
- export { ServerError, BadRequest, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, HttpServer };
2
+ import { ServerError, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, BadRequest, InternalServerError } from "./ServerError";
3
+ export { ServerError, BadRequest, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, HttpServer, InternalServerError };
@@ -1,3 +1,3 @@
1
1
  import HttpServer from "./HttpServer";
2
- import { ServerError, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, BadRequest } from "./ServerError";
3
- export { ServerError, BadRequest, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, HttpServer };
2
+ import { ServerError, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, BadRequest, InternalServerError } from "./ServerError";
3
+ export { ServerError, BadRequest, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, HttpServer, InternalServerError };
@@ -9,10 +9,9 @@ export type RequestMetrics = {
9
9
  ip: string;
10
10
  contentLength: string;
11
11
  error?: {
12
- stack: string;
13
- originalMessage: string;
14
12
  message: string;
15
13
  statusCode: number;
14
+ unexpectedError?: string;
16
15
  };
17
16
  handler: {
18
17
  instance: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "framework-do-dede",
3
- "version": "0.0.37",
3
+ "version": "0.0.39",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",