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"/>
|
4 | <p><strong>HTTP error handler middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
|
5 | <p>
|
6 | <a href="https://www.npmjs.com/package/@middy/http-error-handler?activeTab=versions">
|
7 | <img src="https://badge.fury.io/js/%40middy%2Fhttp-error-handler.svg" alt="npm version" style="max-width:100%;">
|
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/workflows/tests.yml">
|
13 | <img src="https://github.com/middyjs/middy/actions/workflows/tests.yml/badge.svg?branch=main&event=push" alt="GitHub Actions CI 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>
|
19 | <a href="https://snyk.io/test/github/middyjs/middy">
|
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 | </a>
|
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%;">
|
27 | </a>
|
28 | <br/>
|
29 | <a href="https://gitter.im/middyjs/Lobby">
|
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%;">
|
34 | </a>
|
35 | </p>
|
36 | <p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/http-error-handler">https://middy.js.org/docs/middlewares/http-error-handler</a></p>
|
37 | </div>
|
38 |
|
39 | Automatically handles uncaught errors that contain the properties `statusCode` (number) and `message` (string) and creates a proper HTTP response
|
40 | for them (using the message and the status code provided by the error object). Additionally, support for the property `expose` is included with a default value of `statusCode < 500`.
|
41 | We recommend generating these HTTP errors with the npm module [`http-errors`](https://npm.im/http-errors). When manually catching and setting errors with `statusCode >= 500` setting `{expose: true}`
|
42 | is needed for them to be handled.
|
43 |
|
44 | This middleware should be set as the last error handler unless you also want to register the `http-response-serializer`. If so, this middleware should come second-last and the `http-response-serializer` should come last.
|
45 |
|
46 |
|
47 | ## Install
|
48 |
|
49 | To install this middleware you can use NPM:
|
50 |
|
51 | ```bash
|
52 | npm install --save @middy/http-error-handler
|
53 | ```
|
54 |
|
55 |
|
56 | ## Options
|
57 |
|
58 | - `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.
|
59 | - `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.
|
60 |
|
61 | ## Sample usage
|
62 |
|
63 | ```javascript
|
64 | import middy from '@middy/core'
|
65 | import httpErrorHandler from '@middy/http-error-handler'
|
66 |
|
67 | const handler = middy((event, context) => {
|
68 | throw new createError.UnprocessableEntity()
|
69 | })
|
70 |
|
71 | handler
|
72 | .use(httpErrorHandler())
|
73 |
|
74 | // when Lambda runs the handler...
|
75 | handler({}, {}, (_, response) => {
|
76 | t.deepEqual(response,{
|
77 | statusCode: 422,
|
78 | body: 'Unprocessable Entity'
|
79 | })
|
80 | })
|
81 | ```
|
82 |
|
83 |
|
84 | ## Middy documentation and examples
|
85 |
|
86 | For more documentation and examples, refers to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org).
|
87 |
|
88 |
|
89 | ## Contributing
|
90 |
|
91 | Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls).
|
92 |
|
93 |
|
94 | ## License
|
95 |
|
96 | Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
97 |
|
98 | <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
99 | <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
|
100 | </a>
|