@middy/http-error-handler 2.5.6 → 3.0.0-alpha.3

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 (4) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +3 -3
  3. package/index.js +19 -14
  4. package/package.json +11 -9
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2021 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
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
@@ -54,7 +54,7 @@ import middy from '@middy/core'
54
54
  import httpErrorHandler from '@middy/http-error-handler'
55
55
 
56
56
  const handler = middy((event, context) => {
57
- throw createError(422)
57
+ throw new createError.UnprocessableEntity()
58
58
  })
59
59
 
60
60
  handler
@@ -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-2021 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
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,4 +1,4 @@
1
- const { jsonSafeParse, normalizeHttpResponse } = require('@middy/util')
1
+ import { jsonSafeParse, normalizeHttpResponse } from '@middy/util'
2
2
 
3
3
  const defaults = {
4
4
  logger: console.error,
@@ -9,19 +9,20 @@ const httpErrorHandlerMiddleware = (opts = {}) => {
9
9
  const options = { ...defaults, ...opts }
10
10
 
11
11
  const httpErrorHandlerMiddlewareOnError = async (request) => {
12
+ if (request.response !== undefined) return
12
13
  if (typeof options.logger === 'function') {
13
14
  options.logger(request.error)
14
15
  }
15
16
 
16
17
  // Set default expose value, only passes in when there is an override
17
- if (request.error?.statusCode && request.error?.expose === undefined) {
18
+ if (request.error.statusCode && request.error.expose === undefined) {
18
19
  request.error.expose = request.error.statusCode < 500
19
20
  }
20
21
 
21
22
  // Non-http error OR expose set to false
22
23
  if (
23
24
  options.fallbackMessage &&
24
- (!request.error?.statusCode || !request.error?.expose)
25
+ (!request.error.statusCode || !request.error.expose)
25
26
  ) {
26
27
  request.error = {
27
28
  statusCode: 500,
@@ -30,16 +31,20 @@ const httpErrorHandlerMiddleware = (opts = {}) => {
30
31
  }
31
32
  }
32
33
 
33
- if (request.error?.expose) {
34
- request.response = normalizeHttpResponse(request.response)
35
- request.response.statusCode = request.error?.statusCode
36
- request.response.body = request.error?.message
37
- request.response.headers['Content-Type'] =
38
- typeof jsonSafeParse(request.response.body) === 'string'
39
- ? 'text/plain'
40
- : 'application/json'
41
-
42
- return request.response
34
+ if (request.error.expose) {
35
+ normalizeHttpResponse(request)
36
+
37
+ request.response = {
38
+ ...request.response,
39
+ statusCode: request.error.statusCode,
40
+ body: request.error.message,
41
+ headers: {
42
+ ...request.response.headers,
43
+ 'Content-Type': typeof jsonSafeParse(request.error.message) === 'string'
44
+ ? 'text/plain'
45
+ : 'application/json'
46
+ }
47
+ }
43
48
  }
44
49
  }
45
50
 
@@ -47,4 +52,4 @@ const httpErrorHandlerMiddleware = (opts = {}) => {
47
52
  onError: httpErrorHandlerMiddlewareOnError
48
53
  }
49
54
  }
50
- module.exports = httpErrorHandlerMiddleware
55
+ export default httpErrorHandlerMiddleware
package/package.json CHANGED
@@ -1,23 +1,25 @@
1
1
  {
2
2
  "name": "@middy/http-error-handler",
3
- "version": "2.5.6",
3
+ "version": "3.0.0-alpha.3",
4
4
  "description": "Http error handler middleware for the middy framework",
5
- "type": "commonjs",
5
+ "type": "module",
6
6
  "engines": {
7
- "node": ">=12"
7
+ "node": ">=14"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "index.js",
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": {
19
20
  "test": "npm run test:unit",
20
- "test:unit": "ava"
21
+ "test:unit": "ava",
22
+ "test:benchmark": "node __benchmarks__/index.js"
21
23
  },
22
24
  "license": "MIT",
23
25
  "keywords": [
@@ -47,12 +49,12 @@
47
49
  },
48
50
  "homepage": "https://github.com/middyjs/middy#readme",
49
51
  "dependencies": {
50
- "@middy/util": "^2.5.6"
52
+ "@middy/util": "^3.0.0-alpha.3"
51
53
  },
52
54
  "devDependencies": {
53
- "@middy/core": "^2.5.6",
55
+ "@middy/core": "^3.0.0-alpha.3",
54
56
  "@types/http-errors": "^1.8.0",
55
- "@types/node": "^16.0.0"
57
+ "@types/node": "^17.0.0"
56
58
  },
57
- "gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b"
59
+ "gitHead": "1441158711580313765e6d156046ef0fade0d156"
58
60
  }