framework-do-dede 0.0.38 → 0.0.40
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/dist/handlers/controller.handler.js +19 -13
- package/dist/http/HttpServer.d.ts +2 -1
- package/dist/http/HttpServer.js +21 -18
- package/dist/http/ServerError.d.ts +5 -0
- package/dist/http/ServerError.js +10 -0
- package/dist/http/index.d.ts +2 -2
- package/dist/http/index.js +2 -2
- package/dist/protocols/RequestMetrics.d.ts +1 -2
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Registry } from "../di/registry";
|
|
2
|
-
import {
|
|
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()) {
|
|
@@ -42,8 +42,12 @@ export default class ControllerHandler {
|
|
|
42
42
|
let middlewareData = {};
|
|
43
43
|
if (middlewares) {
|
|
44
44
|
for (const middleware of middlewares) {
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
try {
|
|
46
|
+
const middlewareResult = await middleware.execute({ headers: input.headers, ...mergedParams });
|
|
47
|
+
middlewareData = { ...middlewareResult, ...middlewareData };
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
}
|
|
47
51
|
}
|
|
48
52
|
}
|
|
49
53
|
const request = { headers: input.headers, data: mergedParams, middlewareData };
|
|
@@ -51,13 +55,15 @@ export default class ControllerHandler {
|
|
|
51
55
|
const response = await instance[instanceMethod](mergedParams, request);
|
|
52
56
|
if (!offLogs)
|
|
53
57
|
console.log('\x1b[32m%s\x1b[0m', `✅ [LOG] Finish: "${logger.handler.instance}.${logger.handler.method}:" - ${requestMetrics.elapsedTime}ms`);
|
|
58
|
+
console.log('response = ', response);
|
|
54
59
|
return response;
|
|
55
60
|
}
|
|
56
61
|
catch (error) {
|
|
57
62
|
if (!offLogs)
|
|
58
63
|
console.log('\x1b[31m%s\x1b[0m', `❌ [LOG] Erro: "${logger.handler.instance}.${logger.handler.method}:" - error: ${error.message}`);
|
|
59
|
-
|
|
60
|
-
|
|
64
|
+
const capturedError = this.extractError(error);
|
|
65
|
+
requestMetrics.error = capturedError;
|
|
66
|
+
return capturedError;
|
|
61
67
|
}
|
|
62
68
|
finally {
|
|
63
69
|
requestMetrics.endTime = performance.now();
|
|
@@ -70,6 +76,12 @@ export default class ControllerHandler {
|
|
|
70
76
|
}
|
|
71
77
|
httpServer.listen(port);
|
|
72
78
|
}
|
|
79
|
+
// if (error instanceof ServerError) {
|
|
80
|
+
// res.status(error.getStatusCode())
|
|
81
|
+
// throw error;
|
|
82
|
+
// }
|
|
83
|
+
// res.status(500)
|
|
84
|
+
// throw new InternalServerError(error.message, this.defaultMessageError)
|
|
73
85
|
registryControllers() {
|
|
74
86
|
const registryControllers = Registry.resolve('controllers');
|
|
75
87
|
const controllers = [];
|
|
@@ -122,16 +134,10 @@ export default class ControllerHandler {
|
|
|
122
134
|
return filter;
|
|
123
135
|
}
|
|
124
136
|
extractError(error) {
|
|
125
|
-
let statusCode;
|
|
126
|
-
if (error instanceof ServerError)
|
|
127
|
-
statusCode = error.getStatusCode();
|
|
128
|
-
else
|
|
129
|
-
statusCode = 500;
|
|
130
137
|
return {
|
|
131
|
-
stack: error.stack?.toString(),
|
|
132
|
-
originalMessage: error.message,
|
|
133
138
|
message: error.message,
|
|
134
|
-
statusCode
|
|
139
|
+
statusCode: error.getStatusCode(),
|
|
140
|
+
unexpectedError: error instanceof InternalServerError ? error.getUnexpectedError() : undefined,
|
|
135
141
|
};
|
|
136
142
|
}
|
|
137
143
|
}
|
|
@@ -19,10 +19,11 @@ type FrameworkWeb = {
|
|
|
19
19
|
export default abstract class HttpServer {
|
|
20
20
|
protected framework: FrameworkWeb;
|
|
21
21
|
protected frameworkName: string;
|
|
22
|
-
|
|
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;
|
package/dist/http/HttpServer.js
CHANGED
|
@@ -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 = '
|
|
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
|
-
|
|
54
|
-
error: error.message,
|
|
55
|
-
statusCode: error.getStatusCode()
|
|
56
|
-
};
|
|
56
|
+
throw error;
|
|
57
57
|
}
|
|
58
58
|
set.status = 500;
|
|
59
|
-
|
|
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,21 @@ export default class HttpServer {
|
|
|
78
75
|
}
|
|
79
76
|
catch (error) {
|
|
80
77
|
if (error instanceof ServerError) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
statusCode: error.getStatusCode()
|
|
84
|
-
});
|
|
78
|
+
res.status(error.getStatusCode());
|
|
79
|
+
throw error;
|
|
85
80
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
81
|
+
res.status(500);
|
|
82
|
+
throw new InternalServerError(error.message, this.defaultMessageError);
|
|
83
|
+
// if (error instanceof ServerError) {
|
|
84
|
+
// return res.status(error.getStatusCode()).json({
|
|
85
|
+
// error: error.message,
|
|
86
|
+
// statusCode: error.getStatusCode()
|
|
87
|
+
// })
|
|
88
|
+
// }
|
|
89
|
+
// return res.status(500).json({
|
|
90
|
+
// error: this.defaultMessageError,
|
|
91
|
+
// statusCode: 500
|
|
92
|
+
// })
|
|
90
93
|
}
|
|
91
94
|
});
|
|
92
95
|
}
|
|
@@ -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
|
+
}
|
package/dist/http/ServerError.js
CHANGED
|
@@ -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
|
+
}
|
package/dist/http/index.d.ts
CHANGED
|
@@ -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 };
|
package/dist/http/index.js
CHANGED
|
@@ -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 };
|