@@ -39,11 +39,21 @@ They can let processing continue down the stack with `next()`, or complete the r
3939engine .push (function (req , res , next , end ) {
4040 if (req .skipCache ) return next ();
4141 res .result = getResultFromCache (req);
42- end ();
42+ return end ();
43+ });
44+ ```
45+
46+ Middleware functions can be ` async ` :
47+
48+ ``` js
49+ engine .push (async function (req , res , next , end ) {
50+ if (req .method !== targetMethod) return next ();
51+ res .result = await processTargetMethodRequest (req);
52+ return end ();
4353});
4454```
4555
46- By passing a _ return handler_ to the ` next ` function, you can get a peek at the result before it returns .
56+ By passing a _ return handler_ to the ` next ` function, you can get a peek at the response before it is returned to the requester .
4757
4858``` js
4959engine .push (function (req , res , next , end ) {
@@ -61,105 +71,43 @@ const subengine = new JsonRpcEngine();
6171engine .push (subengine .asMiddleware ());
6272```
6373
64- ### ` async ` Middleware
74+ ### Error Handling
6575
66- If you require your middleware function to be ` async ` , use ` createAsyncMiddleware ` :
76+ Errors should be handled by throwing inside middleware functions.
6777
68- ``` js
69- const { createAsyncMiddleware } = require (' json-rpc-engine' );
70-
71- let engine = new RpcEngine ();
72- engine .push (
73- createAsyncMiddleware (async (req , res , next ) => {
74- res .result = 42 ;
75- next ();
76- }),
77- );
78- ```
78+ For backwards compatibility, you can also pass an error to the ` end ` callback,
79+ or set the error on the response object, and then call ` end ` or ` next ` .
80+ However, errors must ** not** be passed to the ` next ` callback.
7981
80- ` async ` middleware do not take an ` end ` callback .
81- Instead, the request ends if the middleware returns without calling ` next() ` :
82+ Errors always take precedent over results .
83+ If an error is detected, the response's ` result ` property will be deleted.
8284
83- ``` js
84- engine .push (
85- createAsyncMiddleware (async (req , res , next ) => {
86- res .result = 42 ;
87- /* The request will end when this returns */
88- }),
89- );
90- ```
91-
92- The ` next ` callback of ` async ` middleware also don't take return handlers.
93- Instead, you can ` await next() ` .
94- When the execution of the middleware resumes, you can work with the response again.
95-
96- ``` js
97- engine .push (
98- createAsyncMiddleware (async (req , res , next ) => {
99- res .result = 42 ;
100- await next ();
101- /* Your return handler logic goes here */
102- addToMetrics (res);
103- }),
104- );
105- ```
106-
107- You can freely mix callback-based and ` async ` middleware:
85+ All of the following examples are equivalent.
86+ It does not matter of the middleware function is synchronous or asynchronous.
10887
10988``` js
89+ // Throwing is preferred.
11090engine .push (function (req , res , next , end ) {
111- if (! isCached (req)) {
112- return next ((cb ) => {
113- insertIntoCache (res, cb);
114- });
115- }
116- res .result = getResultFromCache (req);
117- end ();
91+ throw new Error ();
11892});
11993
120- engine .push (
121- createAsyncMiddleware (async (req , res , next ) => {
122- res .result = 42 ;
123- await next ();
124- addToMetrics (res);
125- }),
126- );
127- ```
128-
129- ### Gotchas
130-
131- Handle errors via ` end(err) ` , _ NOT_ ` next(err) ` .
132-
133- ``` js
134- /* INCORRECT */
94+ // For backwards compatibility, you can also do this:
13595engine .push (function (req , res , next , end ) {
136- next (new Error ());
96+ end (new Error ());
13797});
13898
139- /* CORRECT */
14099engine .push (function (req , res , next , end ) {
141- end (new Error ());
100+ res .error = new Error ();
101+ end ();
142102});
143- ```
144-
145- However, ` next() ` will detect errors on the response object, and cause
146- ` end(res.error) ` to be called.
147103
148- ``` js
149104engine .push (function (req , res , next , end ) {
150105 res .error = new Error ();
151- next (); /* This will cause end(res.error) to be called. */
106+ next ();
152107});
153- ```
154-
155- ## Running tests
156108
157- Build the project if not already built:
158-
159- ``` bash
160- yarn build
161- ```
162-
163- ``` bash
164- yarn test
109+ // INCORRECT. Do not do this:
110+ engine .push (function (req , res , next , end ) {
111+ next (new Error ());
112+ });
165113```
0 commit comments