@middy/http-error-handler 5.0.0-alpha.0 → 5.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/README.md +3 -2
- package/index.js +54 -43
- package/package.json +5 -11
- package/index.cjs +0 -54
package/README.md
CHANGED
|
@@ -19,8 +19,9 @@
|
|
|
19
19
|
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
20
20
|
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
|
|
21
21
|
</a>
|
|
22
|
-
<a href="https://
|
|
23
|
-
<img src="https://
|
|
22
|
+
<a href="https://github.com/middyjs/middy/actions/workflows/sast.yml">
|
|
23
|
+
<img src="https://github.com/middyjs/middy/actions/workflows/sast.yml/badge.svg
|
|
24
|
+
?branch=main&event=push" alt="CodeQL" style="max-width:100%;">
|
|
24
25
|
</a>
|
|
25
26
|
<a href="https://bestpractices.coreinfrastructure.org/projects/5280">
|
|
26
27
|
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
|
package/index.js
CHANGED
|
@@ -1,46 +1,57 @@
|
|
|
1
|
-
import { jsonSafeParse, normalizeHttpResponse } from '@middy/util'
|
|
1
|
+
import { jsonSafeParse, normalizeHttpResponse } from '@middy/util'
|
|
2
|
+
|
|
2
3
|
const defaults = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
4
|
+
logger: console.error,
|
|
5
|
+
fallbackMessage: null
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const httpErrorHandlerMiddleware = (opts = {}) => {
|
|
9
|
+
const options = { ...defaults, ...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
|
+
|
|
17
|
+
// Set default expose value, only passes in when there is an override
|
|
18
|
+
if (request.error.statusCode && request.error.expose === undefined) {
|
|
19
|
+
request.error.expose = request.error.statusCode < 500
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Non-http error OR expose set to false
|
|
23
|
+
if (
|
|
24
|
+
options.fallbackMessage &&
|
|
25
|
+
(!request.error.statusCode || !request.error.expose)
|
|
26
|
+
) {
|
|
27
|
+
request.error = {
|
|
28
|
+
statusCode: 500,
|
|
29
|
+
message: options.fallbackMessage,
|
|
30
|
+
expose: true
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (request.error.expose) {
|
|
35
|
+
normalizeHttpResponse(request)
|
|
36
|
+
const { statusCode, message, headers } = request.error
|
|
37
|
+
request.response = {
|
|
38
|
+
...request.response,
|
|
39
|
+
statusCode,
|
|
40
|
+
body: message,
|
|
41
|
+
headers: {
|
|
42
|
+
...headers,
|
|
43
|
+
...request.response.headers,
|
|
44
|
+
'Content-Type':
|
|
45
|
+
typeof jsonSafeParse(message) === 'string'
|
|
46
|
+
? 'text/plain'
|
|
47
|
+
: 'application/json'
|
|
39
48
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
};
|
|
44
|
-
};
|
|
45
|
-
export default httpErrorHandlerMiddleware;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
46
52
|
|
|
53
|
+
return {
|
|
54
|
+
onError: httpErrorHandlerMiddlewareOnError
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export default httpErrorHandlerMiddleware
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-error-handler",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.1",
|
|
4
4
|
"description": "Http error handler middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,24 +10,18 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"main": "./index.cjs",
|
|
14
13
|
"module": "./index.js",
|
|
15
14
|
"exports": {
|
|
16
15
|
".": {
|
|
17
16
|
"import": {
|
|
18
17
|
"types": "./index.d.ts",
|
|
19
18
|
"default": "./index.js"
|
|
20
|
-
},
|
|
21
|
-
"require": {
|
|
22
|
-
"types": "./index.d.ts",
|
|
23
|
-
"default": "./index.cjs"
|
|
24
19
|
}
|
|
25
20
|
}
|
|
26
21
|
},
|
|
27
22
|
"types": "index.d.ts",
|
|
28
23
|
"files": [
|
|
29
24
|
"index.js",
|
|
30
|
-
"index.cjs",
|
|
31
25
|
"index.d.ts"
|
|
32
26
|
],
|
|
33
27
|
"scripts": {
|
|
@@ -67,12 +61,12 @@
|
|
|
67
61
|
"url": "https://github.com/sponsors/willfarrell"
|
|
68
62
|
},
|
|
69
63
|
"dependencies": {
|
|
70
|
-
"@middy/util": "5.0.0-alpha.
|
|
64
|
+
"@middy/util": "5.0.0-alpha.1"
|
|
71
65
|
},
|
|
72
66
|
"devDependencies": {
|
|
73
|
-
"@middy/core": "5.0.0-alpha.
|
|
67
|
+
"@middy/core": "5.0.0-alpha.1",
|
|
74
68
|
"@types/http-errors": "^2.0.0",
|
|
75
|
-
"@types/node": "^
|
|
69
|
+
"@types/node": "^20.0.0"
|
|
76
70
|
},
|
|
77
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
|
|
78
72
|
}
|
package/index.cjs
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(module, "exports", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: ()=>_default
|
|
8
|
-
});
|
|
9
|
-
const _util = require("@middy/util");
|
|
10
|
-
const defaults = {
|
|
11
|
-
logger: console.error,
|
|
12
|
-
fallbackMessage: null
|
|
13
|
-
};
|
|
14
|
-
const httpErrorHandlerMiddleware = (opts = {})=>{
|
|
15
|
-
const options = {
|
|
16
|
-
...defaults,
|
|
17
|
-
...opts
|
|
18
|
-
};
|
|
19
|
-
const httpErrorHandlerMiddlewareOnError = async (request)=>{
|
|
20
|
-
if (request.response !== undefined) return;
|
|
21
|
-
if (typeof options.logger === 'function') {
|
|
22
|
-
options.logger(request.error);
|
|
23
|
-
}
|
|
24
|
-
if (request.error.statusCode && request.error.expose === undefined) {
|
|
25
|
-
request.error.expose = request.error.statusCode < 500;
|
|
26
|
-
}
|
|
27
|
-
if (options.fallbackMessage && (!request.error.statusCode || !request.error.expose)) {
|
|
28
|
-
request.error = {
|
|
29
|
-
statusCode: 500,
|
|
30
|
-
message: options.fallbackMessage,
|
|
31
|
-
expose: true
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
if (request.error.expose) {
|
|
35
|
-
(0, _util.normalizeHttpResponse)(request);
|
|
36
|
-
const { statusCode , message , headers } = request.error;
|
|
37
|
-
request.response = {
|
|
38
|
-
...request.response,
|
|
39
|
-
statusCode,
|
|
40
|
-
body: message,
|
|
41
|
-
headers: {
|
|
42
|
-
...headers,
|
|
43
|
-
...request.response.headers,
|
|
44
|
-
'Content-Type': typeof (0, _util.jsonSafeParse)(message) === 'string' ? 'text/plain' : 'application/json'
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
return {
|
|
50
|
-
onError: httpErrorHandlerMiddlewareOnError
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
const _default = httpErrorHandlerMiddleware;
|
|
54
|
-
|