@athenna/http 1.3.0 → 1.3.1
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/index.d.ts +2 -0
- package/index.js +2 -0
- package/package.json +1 -1
- package/src/Exceptions/Exception.d.ts +12 -0
- package/src/Exceptions/Exception.js +18 -0
- package/src/Handlers/HttpExceptionHandler.d.ts +27 -0
- package/src/Handlers/HttpExceptionHandler.js +71 -0
- package/src/Http.d.ts +4 -1
- package/src/Http.js +0 -7
- package/src/Kernels/HttpKernel.d.ts +6 -0
- package/src/Kernels/HttpKernel.js +41 -2
- package/src/Handlers/HttpErrorHandler.d.ts +0 -12
- package/src/Handlers/HttpErrorHandler.js +0 -46
package/index.d.ts
CHANGED
|
@@ -11,8 +11,10 @@ export * from './src/Facades/Server';
|
|
|
11
11
|
export * from './src/Http';
|
|
12
12
|
export * from './src/Router/Router';
|
|
13
13
|
export * from './src/Kernels/HttpKernel';
|
|
14
|
+
export * from './src/Exceptions/Exception';
|
|
14
15
|
export * from './src/Providers/HttpRouteProvider';
|
|
15
16
|
export * from './src/Providers/HttpServerProvider';
|
|
17
|
+
export * from './src/Handlers/HttpExceptionHandler';
|
|
16
18
|
export * from './src/Contracts/MiddlewareContract';
|
|
17
19
|
export * from './src/Contracts/Context/ContextContract';
|
|
18
20
|
export * from './src/Contracts/Context/HandlerContract';
|
package/index.js
CHANGED
|
@@ -23,8 +23,10 @@ __exportStar(require("./src/Facades/Server"), exports);
|
|
|
23
23
|
__exportStar(require("./src/Http"), exports);
|
|
24
24
|
__exportStar(require("./src/Router/Router"), exports);
|
|
25
25
|
__exportStar(require("./src/Kernels/HttpKernel"), exports);
|
|
26
|
+
__exportStar(require("./src/Exceptions/Exception"), exports);
|
|
26
27
|
__exportStar(require("./src/Providers/HttpRouteProvider"), exports);
|
|
27
28
|
__exportStar(require("./src/Providers/HttpServerProvider"), exports);
|
|
29
|
+
__exportStar(require("./src/Handlers/HttpExceptionHandler"), exports);
|
|
28
30
|
__exportStar(require("./src/Contracts/MiddlewareContract"), exports);
|
|
29
31
|
__exportStar(require("./src/Contracts/Context/ContextContract"), exports);
|
|
30
32
|
__exportStar(require("./src/Contracts/Context/HandlerContract"), exports);
|
package/package.json
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @athenna/http
|
|
3
|
+
*
|
|
4
|
+
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { Exception as SecException } from '@secjs/utils';
|
|
10
|
+
export declare class Exception extends SecException {
|
|
11
|
+
constructor(message?: string, statusCode?: number, code?: string, help?: string);
|
|
12
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @athenna/http
|
|
4
|
+
*
|
|
5
|
+
* (c) João Lenon <lenon@athenna.io>
|
|
6
|
+
*
|
|
7
|
+
* For the full copyright and license information, please view the LICENSE
|
|
8
|
+
* file that was distributed with this source code.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.Exception = void 0;
|
|
12
|
+
const utils_1 = require("@secjs/utils");
|
|
13
|
+
class Exception extends utils_1.Exception {
|
|
14
|
+
constructor(message = 'Internal server exception.', statusCode = 500, code = 'INTERNAL_SERVER_ERROR', help) {
|
|
15
|
+
super(message, statusCode, code, help);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.Exception = Exception;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ErrorContextContract } from '../Contracts/Context/Error/ErrorContextContract';
|
|
2
|
+
export declare class HttpExceptionHandler {
|
|
3
|
+
/**
|
|
4
|
+
* Set if error logs will come with stack.
|
|
5
|
+
*
|
|
6
|
+
* @protected
|
|
7
|
+
*/
|
|
8
|
+
protected addStack: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Error codes that should be ignored by Log.
|
|
11
|
+
*
|
|
12
|
+
* @protected
|
|
13
|
+
*/
|
|
14
|
+
protected ignoreCodes: string[];
|
|
15
|
+
/**
|
|
16
|
+
* Error statuses that should be ignored by Log.
|
|
17
|
+
*
|
|
18
|
+
* @protected
|
|
19
|
+
*/
|
|
20
|
+
protected ignoreStatuses: string[];
|
|
21
|
+
/**
|
|
22
|
+
* The global exception handler of all HTTP requests.
|
|
23
|
+
*
|
|
24
|
+
* @param ctx
|
|
25
|
+
*/
|
|
26
|
+
handle({ error, response }: ErrorContextContract): Promise<void>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpExceptionHandler = void 0;
|
|
4
|
+
const logger_1 = require("@athenna/logger");
|
|
5
|
+
const utils_1 = require("@secjs/utils");
|
|
6
|
+
const config_1 = require("@athenna/config");
|
|
7
|
+
class HttpExceptionHandler {
|
|
8
|
+
constructor() {
|
|
9
|
+
/**
|
|
10
|
+
* Set if error logs will come with stack.
|
|
11
|
+
*
|
|
12
|
+
* @protected
|
|
13
|
+
*/
|
|
14
|
+
this.addStack = false;
|
|
15
|
+
/**
|
|
16
|
+
* Error codes that should be ignored by Log.
|
|
17
|
+
*
|
|
18
|
+
* @protected
|
|
19
|
+
*/
|
|
20
|
+
this.ignoreCodes = [];
|
|
21
|
+
/**
|
|
22
|
+
* Error statuses that should be ignored by Log.
|
|
23
|
+
*
|
|
24
|
+
* @protected
|
|
25
|
+
*/
|
|
26
|
+
this.ignoreStatuses = [];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The global exception handler of all HTTP requests.
|
|
30
|
+
*
|
|
31
|
+
* @param ctx
|
|
32
|
+
*/
|
|
33
|
+
async handle({ error, response }) {
|
|
34
|
+
const code = error.code || error.name;
|
|
35
|
+
const statusCode = error.statusCode || error.status || 500;
|
|
36
|
+
const body = {
|
|
37
|
+
statusCode,
|
|
38
|
+
code: utils_1.String.toSnakeCase(`${code}`).toUpperCase(),
|
|
39
|
+
name: error.name,
|
|
40
|
+
message: error.message,
|
|
41
|
+
stack: error.stack,
|
|
42
|
+
};
|
|
43
|
+
if (error.help) {
|
|
44
|
+
body.help = error.help;
|
|
45
|
+
}
|
|
46
|
+
const isInternalServerError = statusCode === 500;
|
|
47
|
+
const isDebugMode = config_1.Config.get('app.debug');
|
|
48
|
+
if (isInternalServerError && !isDebugMode) {
|
|
49
|
+
body.name = 'Internal server error';
|
|
50
|
+
body.message = 'An internal server exception has occurred.';
|
|
51
|
+
delete body.stack;
|
|
52
|
+
}
|
|
53
|
+
if (this.ignoreCodes.includes(code) ||
|
|
54
|
+
this.ignoreStatuses.includes(statusCode)) {
|
|
55
|
+
return response.status(statusCode).send(body);
|
|
56
|
+
}
|
|
57
|
+
const logConfig = {
|
|
58
|
+
formatterConfig: {
|
|
59
|
+
context: 'ExceptionHandler',
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
if (this.addStack) {
|
|
63
|
+
logger_1.Log.error(`[${body.statusCode}] ${body.code}::${body.message}\nStack: ${body.stack}`, logConfig);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
logger_1.Log.error(`[${body.statusCode}] ${body.code}::${body.message}`, logConfig);
|
|
67
|
+
}
|
|
68
|
+
return response.status(statusCode).send(body);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.HttpExceptionHandler = HttpExceptionHandler;
|
package/src/Http.d.ts
CHANGED
|
@@ -70,7 +70,10 @@ export declare class Http {
|
|
|
70
70
|
* @param type {handle,intercept,terminate}
|
|
71
71
|
* @return void
|
|
72
72
|
*/
|
|
73
|
-
use(handler: HandleHandlerContract
|
|
73
|
+
use(handler: HandleHandlerContract): any;
|
|
74
|
+
use(handler: HandleHandlerContract, type: 'handle'): any;
|
|
75
|
+
use(handler: InterceptHandlerContract, type: 'intercept'): any;
|
|
76
|
+
use(handler: TerminateHandlerContract, type: 'terminate'): any;
|
|
74
77
|
request(): LightMyRequest.Chain;
|
|
75
78
|
request(options: InjectOptions | string): Promise<LightMyRequest.Response>;
|
|
76
79
|
/**
|
package/src/Http.js
CHANGED
|
@@ -71,13 +71,6 @@ class Http {
|
|
|
71
71
|
getRoutes(options) {
|
|
72
72
|
return this.server.printRoutes(options);
|
|
73
73
|
}
|
|
74
|
-
/**
|
|
75
|
-
* Print all routes registered.
|
|
76
|
-
*
|
|
77
|
-
* @param handler {HandleHandlerContract,InterceptHandlerContract,TerminateHandlerContract}
|
|
78
|
-
* @param type {handle,intercept,terminate}
|
|
79
|
-
* @return void
|
|
80
|
-
*/
|
|
81
74
|
use(handler, type = 'handle') {
|
|
82
75
|
let hookName = 'preHandler';
|
|
83
76
|
let handlerType = 'createDoneHandler';
|
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
2
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
22
|
exports.HttpKernel = void 0;
|
|
4
23
|
const logger_1 = require("@athenna/logger");
|
|
5
24
|
const config_1 = require("@athenna/config");
|
|
6
25
|
const Server_1 = require("../Facades/Server");
|
|
7
26
|
const utils_1 = require("@secjs/utils");
|
|
8
|
-
const HttpErrorHandler_1 = require("../Handlers/HttpErrorHandler");
|
|
9
27
|
class HttpKernel {
|
|
10
28
|
/**
|
|
11
29
|
* Register all global and named middlewares to the server.
|
|
@@ -76,7 +94,14 @@ class HttpKernel {
|
|
|
76
94
|
if (config_1.Config.get('http.noErrorHandler')) {
|
|
77
95
|
return;
|
|
78
96
|
}
|
|
79
|
-
|
|
97
|
+
let extension = 'js';
|
|
98
|
+
if (config_1.Env('NODE_TS') === 'true') {
|
|
99
|
+
extension = 'ts';
|
|
100
|
+
}
|
|
101
|
+
const path = utils_1.Path.app(`Http/Exceptions/Handler.${extension}`);
|
|
102
|
+
const Handler = utils_1.resolveModule(await Promise.resolve().then(() => __importStar(require(path))));
|
|
103
|
+
const handler = new Handler();
|
|
104
|
+
Server_1.Server.setErrorHandler(handler.handle.bind(handler));
|
|
80
105
|
}
|
|
81
106
|
/**
|
|
82
107
|
* Register log terminate middleware
|
|
@@ -92,5 +117,19 @@ class HttpKernel {
|
|
|
92
117
|
return ctx.next();
|
|
93
118
|
}, 'terminate');
|
|
94
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Register the requestId handle middleware.
|
|
122
|
+
*
|
|
123
|
+
* @return void
|
|
124
|
+
*/
|
|
125
|
+
async registerRequestIdMiddleware() {
|
|
126
|
+
if (config_1.Config.get('http.noRequestId')) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
Server_1.Server.use(async (ctx) => {
|
|
130
|
+
ctx.data.requestId = utils_1.Token.generate('ath');
|
|
131
|
+
return ctx.next();
|
|
132
|
+
}, 'handle');
|
|
133
|
+
}
|
|
95
134
|
}
|
|
96
135
|
exports.HttpKernel = HttpKernel;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/http
|
|
3
|
-
*
|
|
4
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import { ErrorContextContract } from '../Contracts/Context/Error/ErrorContextContract';
|
|
10
|
-
export declare class HttpErrorHandler {
|
|
11
|
-
static handler({ error, response }: ErrorContextContract): void | Promise<void>;
|
|
12
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @athenna/http
|
|
4
|
-
*
|
|
5
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
6
|
-
*
|
|
7
|
-
* For the full copyright and license information, please view the LICENSE
|
|
8
|
-
* file that was distributed with this source code.
|
|
9
|
-
*/
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.HttpErrorHandler = void 0;
|
|
12
|
-
const utils_1 = require("@secjs/utils");
|
|
13
|
-
const logger_1 = require("@athenna/logger");
|
|
14
|
-
const config_1 = require("@athenna/config");
|
|
15
|
-
class HttpErrorHandler {
|
|
16
|
-
static handler({ error, response }) {
|
|
17
|
-
const code = error.code || error.name;
|
|
18
|
-
const statusCode = error.statusCode || error.status || 500;
|
|
19
|
-
const body = {
|
|
20
|
-
statusCode,
|
|
21
|
-
code: utils_1.String.toSnakeCase(`${code}`).toUpperCase(),
|
|
22
|
-
name: error.name,
|
|
23
|
-
message: error.message,
|
|
24
|
-
stack: error.stack,
|
|
25
|
-
};
|
|
26
|
-
if (error.help) {
|
|
27
|
-
body.help = error.help;
|
|
28
|
-
}
|
|
29
|
-
const isInternalServerError = statusCode === 500;
|
|
30
|
-
const isDebugMode = config_1.Config.get('app.debug');
|
|
31
|
-
if (isInternalServerError && !isDebugMode) {
|
|
32
|
-
body.name = 'Internal server error';
|
|
33
|
-
body.message = 'An internal server exception has occurred.';
|
|
34
|
-
delete body.stack;
|
|
35
|
-
}
|
|
36
|
-
if (isDebugMode) {
|
|
37
|
-
logger_1.Log.error(`Error: ${JSON.stringify(body, null, 2)}`, {
|
|
38
|
-
formatterConfig: {
|
|
39
|
-
context: HttpErrorHandler.name,
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
return response.status(statusCode).send(body);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
exports.HttpErrorHandler = HttpErrorHandler;
|