@middy/http-error-handler 3.0.0-alpha.3 → 3.0.0-alpha.7
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 +25 -15
- package/index.js +2 -54
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,26 +1,36 @@
|
|
|
1
|
-
# Middy http-error-handler middleware
|
|
2
|
-
|
|
3
|
-
<div align="center">
|
|
4
|
-
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
|
|
5
|
-
</div>
|
|
6
|
-
|
|
7
1
|
<div align="center">
|
|
2
|
+
<h1>Middy http-error-handler middleware</h1>
|
|
3
|
+
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
|
|
8
4
|
<p><strong>HTTP error handler middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
|
|
9
|
-
</div>
|
|
10
|
-
|
|
11
|
-
<div align="center">
|
|
12
5
|
<p>
|
|
13
|
-
<a href="
|
|
6
|
+
<a href="https://www.npmjs.com/package/@middy/http-error-handler?activeTab=versions">
|
|
14
7
|
<img src="https://badge.fury.io/js/%40middy%2Fhttp-error-handler.svg" alt="npm version" style="max-width:100%;">
|
|
15
8
|
</a>
|
|
9
|
+
<a href="https://packagephobia.com/result?p=@middy/http-error-handler">
|
|
10
|
+
<img src="https://packagephobia.com/badge?p=@middy/http-error-handler" alt="npm install size" style="max-width:100%;">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
|
|
14
|
+
</a>
|
|
15
|
+
<br/>
|
|
16
|
+
<a href="https://standardjs.com/">
|
|
17
|
+
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
|
|
18
|
+
</a>
|
|
16
19
|
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
17
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%;">
|
|
18
21
|
</a>
|
|
19
|
-
<a href="https://
|
|
20
|
-
<img src="https://img.shields.io/
|
|
22
|
+
<a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
|
|
23
|
+
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
|
|
24
|
+
</a>
|
|
25
|
+
<a href="https://bestpractices.coreinfrastructure.org/projects/5280">
|
|
26
|
+
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
|
|
21
27
|
</a>
|
|
28
|
+
<br/>
|
|
22
29
|
<a href="https://gitter.im/middyjs/Lobby">
|
|
23
|
-
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter"
|
|
30
|
+
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
|
|
33
|
+
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
24
34
|
</a>
|
|
25
35
|
</p>
|
|
26
36
|
</div>
|
|
@@ -44,8 +54,8 @@ npm install --save @middy/http-error-handler
|
|
|
44
54
|
|
|
45
55
|
## Options
|
|
46
56
|
|
|
47
|
-
- `logger` function (
|
|
48
|
-
- `fallbackMessage` (default
|
|
57
|
+
- `logger` (function) (default `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.
|
|
58
|
+
- `fallbackMessage` (string) (default `undefined`) - 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
59
|
|
|
50
60
|
## Sample usage
|
|
51
61
|
|
package/index.js
CHANGED
|
@@ -1,55 +1,3 @@
|
|
|
1
|
-
import
|
|
1
|
+
import{jsonSafeParse,normalizeHttpResponse}from'@middy/util';const defaults={logger:console.error,fallbackMessage:null};const httpErrorHandlerMiddleware=(opts={})=>{const options={...defaults,...opts};const httpErrorHandlerMiddlewareOnError=async request=>{if(request.response!==undefined)return;if(typeof options.logger==='function'){options.logger(request.error)}if(request.error.statusCode&&request.error.expose===undefined){request.error.expose=request.error.statusCode<500}if(options.fallbackMessage&&(!request.error.statusCode||!request.error.expose)){request.error={statusCode:500,message:options.fallbackMessage,expose:true}}if(request.error.expose){normalizeHttpResponse(request);request.response={...request.response,statusCode:request.error.statusCode,body:request.error.message,headers:{...request.response.headers,'Content-Type':typeof jsonSafeParse(request.error.message)==='string'?'text/plain':'application/json'}}}};return{onError:httpErrorHandlerMiddlewareOnError}};export default httpErrorHandlerMiddleware
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
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
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return {
|
|
52
|
-
onError: httpErrorHandlerMiddlewareOnError
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
export default httpErrorHandlerMiddleware
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-error-handler",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.7",
|
|
4
4
|
"description": "Http error handler middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -49,12 +49,12 @@
|
|
|
49
49
|
},
|
|
50
50
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@middy/util": "^3.0.0-alpha.
|
|
52
|
+
"@middy/util": "^3.0.0-alpha.7"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@middy/core": "^3.0.0-alpha.
|
|
55
|
+
"@middy/core": "^3.0.0-alpha.7",
|
|
56
56
|
"@types/http-errors": "^1.8.0",
|
|
57
57
|
"@types/node": "^17.0.0"
|
|
58
58
|
},
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "5cef39ebe49c201f97d71bb0680004de4b82cb91"
|
|
60
60
|
}
|