Skip to content

Commit b725c85

Browse files
committed
Added "Handling exceptions" section
1 parent 1e5efb6 commit b725c85

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

Middleware.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,33 @@ module.exports = (req, res, next) => {
2424

2525
'use strict';
2626

27+
const {RouterError} = require('@lambda-lambda-lambda/router/src/router/Error.js');
28+
2729
/**
2830
* Middleware to define session state, if exists.
2931
*/
3032
module.exports = async (req, res, next) => {
3133
if (await checkSession()) {
32-
req.plugin('session', true); // Passed down the chain.
34+
35+
// Passed down the Router stack.
36+
req.plugin('session', true);
37+
3338
} else {
34-
return Promise.reject('Output to console');
39+
40+
// Write to CloudWatch, continue..
41+
throw new Error('Output to console');
3542
}
3643

37-
// next() should be omitted, use Promise.reject().
44+
// next() should be omitted
3845
};
3946
```
4047

4148
See [L³ middleware](https://github.com/lambda-lambda-lambda/middleware) for more complex use cases.
49+
50+
## Handling exceptions
51+
52+
| Exception type | Description |
53+
|-------------------------|--------------------------------------------------------------------------|
54+
| `throw new RouterError` | When called will immediately _exit the middleware stack_ and write the exception to [CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html) logs. |
55+
| `throw new Error` | When called will write the exception to [CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html) and _run the subsequent handler in middleware stack_. |
56+
| `Promise.reject()` | Same as `throw new Error` with no exception thrown. |

0 commit comments

Comments
 (0)