@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.
Files changed (3) hide show
  1. package/index.d.ts +5 -5
  2. package/index.js +55 -55
  3. package/package.json +71 -74
package/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import middy from '@middy/core'
1
+ import type middy from "@middy/core";
2
2
 
3
3
  interface Options {
4
- logger?: ((error: any) => void) | boolean
5
- fallbackMessage?: string
4
+ logger?: ((error: any) => undefined) | boolean;
5
+ fallbackMessage?: string;
6
6
  }
7
7
 
8
- declare function httpErrorHandler (options?: Options): middy.MiddlewareObj
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 '@middy/util'
1
+ import { jsonSafeParse, normalizeHttpResponse } from "@middy/util";
2
2
 
3
3
  const defaults = {
4
- logger: console.error,
5
- fallbackMessage: undefined
6
- }
4
+ logger: console.error,
5
+ fallbackMessage: undefined,
6
+ };
7
7
 
8
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 (!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
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
- "name": "@middy/http-error-handler",
3
- "version": "6.1.6",
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": [
27
- "index.js",
28
- "index.d.ts"
29
- ],
30
- "scripts": {
31
- "test": "npm run test:unit && npm run test:fuzz",
32
- "test:unit": "node --test __tests__/index.js",
33
- "test:fuzz": "node --test __tests__/fuzz.js",
34
- "test:benchmark": "node __benchmarks__/index.js"
35
- },
36
- "license": "MIT",
37
- "keywords": [
38
- "Lambda",
39
- "Middleware",
40
- "Serverless",
41
- "Framework",
42
- "AWS",
43
- "AWS Lambda",
44
- "Middy",
45
- "HTTP",
46
- "API",
47
- "Error Handler",
48
- "Error Handling"
49
- ],
50
- "author": {
51
- "name": "Middy contributors",
52
- "url": "https://github.com/middyjs/middy/graphs/contributors"
53
- },
54
- "repository": {
55
- "type": "git",
56
- "url": "git+https://github.com/middyjs/middy.git",
57
- "directory": "packages/http-error-handler"
58
- },
59
- "bugs": {
60
- "url": "https://github.com/middyjs/middy/issues"
61
- },
62
- "homepage": "https://middy.js.org",
63
- "funding": {
64
- "type": "github",
65
- "url": "https://github.com/sponsors/willfarrell"
66
- },
67
- "dependencies": {
68
- "@middy/util": "6.1.6"
69
- },
70
- "devDependencies": {
71
- "@middy/core": "6.1.6",
72
- "@types/http-errors": "^2.0.0",
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
  }