@@ -39,11 +39,21 @@ They can let processing continue down the stack with `next()`, or complete the r
39
39
engine .push (function (req , res , next , end ) {
40
40
if (req .skipCache ) return next ();
41
41
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 ();
43
53
});
44
54
```
45
55
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 .
47
57
48
58
``` js
49
59
engine .push (function (req , res , next , end ) {
@@ -61,105 +71,43 @@ const subengine = new JsonRpcEngine();
61
71
engine .push (subengine .asMiddleware ());
62
72
```
63
73
64
- ### ` async ` Middleware
74
+ ### Error Handling
65
75
66
- If you require your middleware function to be ` async ` , use ` createAsyncMiddleware ` :
76
+ Errors should be handled by throwing inside middleware functions.
67
77
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.
79
81
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.
82
84
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.
108
87
109
88
``` js
89
+ // Throwing is preferred.
110
90
engine .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 ();
118
92
});
119
93
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:
135
95
engine .push (function (req , res , next , end ) {
136
- next (new Error ());
96
+ end (new Error ());
137
97
});
138
98
139
- /* CORRECT */
140
99
engine .push (function (req , res , next , end ) {
141
- end (new Error ());
100
+ res .error = new Error ();
101
+ end ();
142
102
});
143
- ```
144
-
145
- However, ` next() ` will detect errors on the response object, and cause
146
- ` end(res.error) ` to be called.
147
103
148
- ``` js
149
104
engine .push (function (req , res , next , end ) {
150
105
res .error = new Error ();
151
- next (); /* This will cause end(res.error) to be called. */
106
+ next ();
152
107
});
153
- ```
154
-
155
- ## Running tests
156
108
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
+ });
165
113
```
0 commit comments