@middy/http-error-handler 6.1.6 → 6.2.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 +5 -5
- package/index.js +55 -55
- package/package.json +71 -74
package/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import middy from
|
|
1
|
+
import type middy from "@middy/core";
|
|
2
2
|
|
|
3
3
|
interface Options {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
logger?: ((error: any) => undefined) | boolean;
|
|
5
|
+
fallbackMessage?: string;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
declare function httpErrorHandler
|
|
8
|
+
declare function httpErrorHandler(options?: Options): middy.MiddlewareObj;
|
|
9
9
|
|
|
10
|
-
export default httpErrorHandler
|
|
10
|
+
export default httpErrorHandler;
|
package/index.js
CHANGED
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
import { jsonSafeParse, normalizeHttpResponse } from
|
|
1
|
+
import { jsonSafeParse, normalizeHttpResponse } from "@middy/util";
|
|
2
2
|
|
|
3
3
|
const defaults = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
4
|
+
logger: console.error,
|
|
5
|
+
fallbackMessage: undefined,
|
|
6
|
+
};
|
|
7
7
|
|
|
8
8
|
const httpErrorHandlerMiddleware = (opts = {}) => {
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
export default httpErrorHandlerMiddleware
|
|
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 (!request.error.expose || !request.error.statusCode) {
|
|
24
|
+
request.error = {
|
|
25
|
+
statusCode: 500,
|
|
26
|
+
message: options.fallbackMessage,
|
|
27
|
+
expose: true,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (request.error.expose) {
|
|
32
|
+
normalizeHttpResponse(request);
|
|
33
|
+
const { statusCode, message, headers } = request.error;
|
|
34
|
+
|
|
35
|
+
request.response = {
|
|
36
|
+
...request.response,
|
|
37
|
+
statusCode,
|
|
38
|
+
headers: {
|
|
39
|
+
...request.response.headers,
|
|
40
|
+
...headers,
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
if (message) {
|
|
45
|
+
const headerContentType =
|
|
46
|
+
typeof jsonSafeParse(message) === "string"
|
|
47
|
+
? "text/plain"
|
|
48
|
+
: "application/json";
|
|
49
|
+
request.response.body = message;
|
|
50
|
+
request.response.headers["Content-Type"] = headerContentType;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
onError: httpErrorHandlerMiddlewareOnError,
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
export default httpErrorHandlerMiddleware;
|
package/package.json
CHANGED
|
@@ -1,76 +1,73 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
"@types/node": "^20.0.0"
|
|
74
|
-
},
|
|
75
|
-
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
2
|
+
"name": "@middy/http-error-handler",
|
|
3
|
+
"version": "6.2.1",
|
|
4
|
+
"description": "Http error handler middleware for the middy framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"types": "index.d.ts",
|
|
26
|
+
"files": ["index.js", "index.d.ts"],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "npm run test:unit && npm run test:fuzz",
|
|
29
|
+
"test:unit": "node --test",
|
|
30
|
+
"test:fuzz": "node --test index.fuzz.js",
|
|
31
|
+
"test:perf": "node --test index.perf.js"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"Lambda",
|
|
36
|
+
"Middleware",
|
|
37
|
+
"Serverless",
|
|
38
|
+
"Framework",
|
|
39
|
+
"AWS",
|
|
40
|
+
"AWS Lambda",
|
|
41
|
+
"Middy",
|
|
42
|
+
"HTTP",
|
|
43
|
+
"API",
|
|
44
|
+
"Error Handler",
|
|
45
|
+
"Error Handling"
|
|
46
|
+
],
|
|
47
|
+
"author": {
|
|
48
|
+
"name": "Middy contributors",
|
|
49
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
50
|
+
},
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "git+https://github.com/middyjs/middy.git",
|
|
54
|
+
"directory": "packages/http-error-handler"
|
|
55
|
+
},
|
|
56
|
+
"bugs": {
|
|
57
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
58
|
+
},
|
|
59
|
+
"homepage": "https://middy.js.org",
|
|
60
|
+
"funding": {
|
|
61
|
+
"type": "github",
|
|
62
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
63
|
+
},
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"@middy/util": "6.2.1"
|
|
66
|
+
},
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"@middy/core": "6.2.1",
|
|
69
|
+
"@types/http-errors": "^2.0.0",
|
|
70
|
+
"@types/node": "^20.0.0"
|
|
71
|
+
},
|
|
72
|
+
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
76
73
|
}
|