@middy/http-error-handler 3.0.1 → 3.0.4
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/LICENSE +1 -1
- package/README.md +4 -3
- package/index.cjs +51 -1
- package/index.d.ts +1 -1
- package/index.js +45 -1
- package/package.json +4 -4
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
3
|
+
Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell) and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
<a href="https://packagephobia.com/result?p=@middy/http-error-handler">
|
|
10
10
|
<img src="https://packagephobia.com/badge?p=@middy/http-error-handler" alt="npm install size" style="max-width:100%;">
|
|
11
11
|
</a>
|
|
12
|
-
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
-
<img src="https://github.com/middyjs/middy/workflows/
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions/workflows/tests.yml">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/actions/workflows/tests.yml/badge.svg?branch=main&event=push" alt="GitHub Actions CI status badge" style="max-width:100%;">
|
|
14
14
|
</a>
|
|
15
15
|
<br/>
|
|
16
16
|
<a href="https://standardjs.com/">
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
34
34
|
</a>
|
|
35
35
|
</p>
|
|
36
|
+
<p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/http-error-handler">https://middy.js.org/docs/middlewares/http-error-handler</a></p>
|
|
36
37
|
</div>
|
|
37
38
|
|
|
38
39
|
Automatically handles uncaught errors that contain the properties `statusCode` (number) and `message` (string) and creates a proper HTTP response
|
|
@@ -92,7 +93,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
|
|
|
92
93
|
|
|
93
94
|
## License
|
|
94
95
|
|
|
95
|
-
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
96
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
96
97
|
|
|
97
98
|
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
98
99
|
<img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
|
package/index.cjs
CHANGED
|
@@ -1,3 +1,53 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
module.exports = void 0;
|
|
6
|
+
var _util = require("@middy/util");
|
|
7
|
+
const defaults = {
|
|
8
|
+
logger: console.error,
|
|
9
|
+
fallbackMessage: null
|
|
10
|
+
};
|
|
11
|
+
const httpErrorHandlerMiddleware = (opts = {})=>{
|
|
12
|
+
const options = {
|
|
13
|
+
...defaults,
|
|
14
|
+
...opts
|
|
15
|
+
};
|
|
16
|
+
const httpErrorHandlerMiddlewareOnError = async (request)=>{
|
|
17
|
+
if (request.response !== undefined) return;
|
|
18
|
+
if (typeof options.logger === 'function') {
|
|
19
|
+
options.logger(request.error);
|
|
20
|
+
}
|
|
21
|
+
if (request.error.statusCode && request.error.expose === undefined) {
|
|
22
|
+
request.error.expose = request.error.statusCode < 500;
|
|
23
|
+
}
|
|
24
|
+
if (options.fallbackMessage && (!request.error.statusCode || !request.error.expose)) {
|
|
25
|
+
request.error = {
|
|
26
|
+
statusCode: 500,
|
|
27
|
+
message: options.fallbackMessage,
|
|
28
|
+
expose: true
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
if (request.error.expose) {
|
|
32
|
+
(0, _util).normalizeHttpResponse(request);
|
|
33
|
+
const { statusCode , message } = request.error;
|
|
34
|
+
request.response = {
|
|
35
|
+
...request.response,
|
|
36
|
+
statusCode: statusCode,
|
|
37
|
+
body: message,
|
|
38
|
+
headers: {
|
|
39
|
+
...request.response.headers,
|
|
40
|
+
'Content-Type': typeof (0, _util).jsonSafeParse(message) === 'string' ? 'text/plain' : 'application/json'
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
onError: httpErrorHandlerMiddlewareOnError
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
var _default = httpErrorHandlerMiddleware;
|
|
50
|
+
module.exports = _default;
|
|
51
|
+
|
|
2
52
|
|
|
3
53
|
//# sourceMappingURL=index.cjs.map
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,3 +1,47 @@
|
|
|
1
|
-
import{jsonSafeParse,normalizeHttpResponse}from
|
|
1
|
+
import { jsonSafeParse, normalizeHttpResponse } from '@middy/util';
|
|
2
|
+
const defaults = {
|
|
3
|
+
logger: console.error,
|
|
4
|
+
fallbackMessage: null
|
|
5
|
+
};
|
|
6
|
+
const httpErrorHandlerMiddleware = (opts = {})=>{
|
|
7
|
+
const options = {
|
|
8
|
+
...defaults,
|
|
9
|
+
...opts
|
|
10
|
+
};
|
|
11
|
+
const httpErrorHandlerMiddlewareOnError = async (request)=>{
|
|
12
|
+
if (request.response !== undefined) return;
|
|
13
|
+
if (typeof options.logger === 'function') {
|
|
14
|
+
options.logger(request.error);
|
|
15
|
+
}
|
|
16
|
+
if (request.error.statusCode && request.error.expose === undefined) {
|
|
17
|
+
request.error.expose = request.error.statusCode < 500;
|
|
18
|
+
}
|
|
19
|
+
if (options.fallbackMessage && (!request.error.statusCode || !request.error.expose)) {
|
|
20
|
+
request.error = {
|
|
21
|
+
statusCode: 500,
|
|
22
|
+
message: options.fallbackMessage,
|
|
23
|
+
expose: true
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
if (request.error.expose) {
|
|
27
|
+
normalizeHttpResponse(request);
|
|
28
|
+
const { statusCode , message } = request.error;
|
|
29
|
+
request.response = {
|
|
30
|
+
...request.response,
|
|
31
|
+
statusCode: statusCode,
|
|
32
|
+
body: message,
|
|
33
|
+
headers: {
|
|
34
|
+
...request.response.headers,
|
|
35
|
+
'Content-Type': typeof jsonSafeParse(message) === 'string' ? 'text/plain' : 'application/json'
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
onError: httpErrorHandlerMiddlewareOnError
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
export default httpErrorHandlerMiddleware;
|
|
45
|
+
|
|
2
46
|
|
|
3
47
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-error-handler",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"description": "Http error handler middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -56,12 +56,12 @@
|
|
|
56
56
|
},
|
|
57
57
|
"homepage": "https://middy.js.org",
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@middy/util": "
|
|
59
|
+
"@middy/util": "3.0.4"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@middy/core": "
|
|
62
|
+
"@middy/core": "3.0.4",
|
|
63
63
|
"@types/http-errors": "^1.8.0",
|
|
64
64
|
"@types/node": "^17.0.0"
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "3e9bc83e791f943c71cd7003fc27f0a3692d83a1"
|
|
67
67
|
}
|