Skip to content
This repository was archived by the owner on Aug 8, 2024. It is now read-only.

Commit 2c41f52

Browse files
committed
documentate generic error handler
1 parent 7a229b5 commit 2c41f52

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ export const handler = router.handler({
220220
221221
</details>
222222
223-
## Error mapping
223+
## Error mapping / handling
224224
225225
```js
226226
import * as router from 'aws-lambda-router'
@@ -255,7 +255,43 @@ The action can throw an object like
255255
"throw {reason: 'NotFound', message: 'object id not found'}"
256256
257257
and the http response then contains the configured value as response code and the message as the body.
258-
258+
259+
### Genereric error handler for proxyIntegration
260+
261+
Also there is implemented an generic error handler. The idea is to have a place for handling error logging and also return custom error messages.
262+
263+
<details>
264+
<summary>Generic error handling example</summary>
265+
266+
```js
267+
onError: (error, event, context) => {
268+
// Global exceptions goes here, works for sns, s3 and sqs should end up here aswell
269+
console.log(error)
270+
},
271+
proxyIntegration: {
272+
onError: (error, request, context) => {
273+
// proxy integration exceptions goes here
274+
console.log('Error', error, request, context)
275+
},
276+
routes: ...
277+
}
278+
279+
// Also support returning a response:
280+
proxyIntegration: {
281+
onError: async (error) => {
282+
console.log('Error', error)
283+
await someAsyncMethod();
284+
return {
285+
statusCode: 401,
286+
body: Not allowed
287+
}
288+
},
289+
routes: ...
290+
}
291+
```
292+
</details>
293+
294+
For more examples please look into the [tests](test/proxyIntegration.test.ts) of proxyIntegration.
259295
260296
## SNS to Lambda Integrations
261297

0 commit comments

Comments
 (0)