@middy/http-error-handler 2.5.2 → 3.0.0-alpha.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/LICENSE +1 -1
- package/README.md +2 -2
- package/index.js +16 -23
- package/package.json +9 -9
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-
|
|
3
|
+
Copyright (c) 2017-2022 Luciano Mammino, will Farrell 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
|
@@ -44,7 +44,7 @@ npm install --save @middy/http-error-handler
|
|
|
44
44
|
|
|
45
45
|
## Options
|
|
46
46
|
|
|
47
|
-
- `logger` (defaults to `console.error`) - a logging function that is invoked with the current error as an argument. You can pass `false` if you don't want the logging to happen.
|
|
47
|
+
- `logger` function (defaults to `console.error`) - a logging function that is invoked with the current error as an argument. You can pass `false` if you don't want the logging to happen.
|
|
48
48
|
- `fallbackMessage` (default to null) - When non-http errors occur you can catch them by setting a fallback message to be used. These will be returned with a 500 status code.
|
|
49
49
|
|
|
50
50
|
## Sample usage
|
|
@@ -82,7 +82,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
|
|
|
82
82
|
|
|
83
83
|
## License
|
|
84
84
|
|
|
85
|
-
Licensed under [MIT License](LICENSE). Copyright (c) 2017-
|
|
85
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
86
86
|
|
|
87
87
|
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
88
88
|
<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.js
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const {
|
|
4
|
-
jsonSafeParse,
|
|
5
|
-
normalizeHttpResponse
|
|
6
|
-
} = require('@middy/util');
|
|
7
|
-
|
|
1
|
+
import { jsonSafeParse, normalizeHttpResponse } from '@middy/util';
|
|
8
2
|
const defaults = {
|
|
9
3
|
logger: console.error,
|
|
10
4
|
fallbackMessage: null
|
|
@@ -16,19 +10,17 @@ const httpErrorHandlerMiddleware = (opts = {}) => {
|
|
|
16
10
|
};
|
|
17
11
|
|
|
18
12
|
const httpErrorHandlerMiddlewareOnError = async request => {
|
|
19
|
-
|
|
13
|
+
if (request.response !== undefined) return;
|
|
20
14
|
|
|
21
15
|
if (typeof options.logger === 'function') {
|
|
22
16
|
options.logger(request.error);
|
|
23
|
-
}
|
|
24
|
-
|
|
17
|
+
}
|
|
25
18
|
|
|
26
|
-
if (
|
|
19
|
+
if (request.error.statusCode && request.error.expose === undefined) {
|
|
27
20
|
request.error.expose = request.error.statusCode < 500;
|
|
28
|
-
}
|
|
29
|
-
|
|
21
|
+
}
|
|
30
22
|
|
|
31
|
-
if (options.fallbackMessage && (!
|
|
23
|
+
if (options.fallbackMessage && (!request.error.statusCode || !request.error.expose)) {
|
|
32
24
|
request.error = {
|
|
33
25
|
statusCode: 500,
|
|
34
26
|
message: options.fallbackMessage,
|
|
@@ -36,14 +28,15 @@ const httpErrorHandlerMiddleware = (opts = {}) => {
|
|
|
36
28
|
};
|
|
37
29
|
}
|
|
38
30
|
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
31
|
+
if (request.error.expose) {
|
|
32
|
+
normalizeHttpResponse(request);
|
|
33
|
+
request.response = { ...request.response,
|
|
34
|
+
statusCode: request.error.statusCode,
|
|
35
|
+
body: request.error.message,
|
|
36
|
+
headers: { ...request.response.headers,
|
|
37
|
+
'Content-Type': typeof jsonSafeParse(request.error.message) === 'string' ? 'text/plain' : 'application/json'
|
|
38
|
+
}
|
|
39
|
+
};
|
|
47
40
|
}
|
|
48
41
|
};
|
|
49
42
|
|
|
@@ -52,4 +45,4 @@ const httpErrorHandlerMiddleware = (opts = {}) => {
|
|
|
52
45
|
};
|
|
53
46
|
};
|
|
54
47
|
|
|
55
|
-
|
|
48
|
+
export default httpErrorHandlerMiddleware;
|
package/package.json
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-error-handler",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-alpha.1",
|
|
4
4
|
"description": "Http error handler middleware for the middy framework",
|
|
5
|
-
"type": "
|
|
5
|
+
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=14"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"
|
|
13
|
+
"exports": "./index.js",
|
|
14
14
|
"types": "index.d.ts",
|
|
15
15
|
"files": [
|
|
16
|
+
"index.js",
|
|
16
17
|
"index.d.ts"
|
|
17
18
|
],
|
|
18
19
|
"scripts": {
|
|
@@ -47,13 +48,12 @@
|
|
|
47
48
|
},
|
|
48
49
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
49
50
|
"dependencies": {
|
|
50
|
-
"@middy/util": "^
|
|
51
|
+
"@middy/util": "^3.0.0-alpha.1"
|
|
51
52
|
},
|
|
52
53
|
"devDependencies": {
|
|
53
|
-
"@middy/core": "^
|
|
54
|
+
"@middy/core": "^3.0.0-alpha.1",
|
|
54
55
|
"@types/http-errors": "^1.8.0",
|
|
55
|
-
"@types/node": "^
|
|
56
|
-
"http-errors": "^1.8.0"
|
|
56
|
+
"@types/node": "^17.0.0"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "a14125c6b2e21b181824f9985a919a47f1e4711f"
|
|
59
59
|
}
|