@@ -27,6 +27,7 @@ import { HttpContext } from '../HttpContext'
27
27
import { RequestHandler } from './RequestHandler'
28
28
import { MiddlewareStore } from '../MiddlewareStore'
29
29
import { ExceptionManager } from './ExceptionManager'
30
+ import { InternalAsyncHttpContext } from '../AsyncHttpContext'
30
31
31
32
/**
32
33
* Server class handles the HTTP requests by using all Adonis micro modules.
@@ -123,6 +124,13 @@ export class Server implements ServerContract {
123
124
)
124
125
}
125
126
127
+ /**
128
+ * Returns a new async HTTP context for the new request
129
+ */
130
+ private getAsyncContext ( ctx : HttpContextContract ) : InternalAsyncHttpContext {
131
+ return new InternalAsyncHttpContext ( ctx )
132
+ }
133
+
126
134
/**
127
135
* Define custom error handler to handler all errors
128
136
* occurred during HTTP request
@@ -160,34 +168,40 @@ export class Server implements ServerContract {
160
168
161
169
const requestAction = this . getProfilerRow ( request )
162
170
const ctx = this . getContext ( request , response , requestAction )
171
+ const asyncContext = this . getAsyncContext ( ctx )
163
172
164
173
/*
165
- * Handle request by executing hooks, request middleware stack
166
- * and route handler
174
+ * Run everything within the async HTTP context
167
175
*/
168
- try {
169
- await this . handleRequest ( ctx )
170
- } catch ( error ) {
171
- await this . exception . handle ( error , ctx )
172
- }
176
+ return asyncContext . run ( async ( ) => {
177
+ /*
178
+ * Handle request by executing hooks, request middleware stack
179
+ * and route handler
180
+ */
181
+ try {
182
+ await this . handleRequest ( ctx )
183
+ } catch ( error ) {
184
+ await this . exception . handle ( error , ctx )
185
+ }
173
186
174
- /*
175
- * Excute hooks when there are one or more hooks. The `ctx.response.finish`
176
- * is intentionally inside both the `try` and `catch` blocks as a defensive
177
- * measure.
178
- *
179
- * When we call `response.finish`, it will serialize the response body and may
180
- * encouter errors while doing so and hence will be catched by the catch
181
- * block.
182
- */
183
- try {
184
- await this . hooks . executeAfter ( ctx )
185
- requestAction . end ( { status_code : res . statusCode } )
186
- ctx . response . finish ( )
187
- } catch ( error ) {
188
- await this . exception . handle ( error , ctx )
189
- requestAction . end ( { status_code : res . statusCode , error } )
190
- ctx . response . finish ( )
191
- }
187
+ /*
188
+ * Excute hooks when there are one or more hooks. The `ctx.response.finish`
189
+ * is intentionally inside both the `try` and `catch` blocks as a defensive
190
+ * measure.
191
+ *
192
+ * When we call `response.finish`, it will serialize the response body and may
193
+ * encouter errors while doing so and hence will be catched by the catch
194
+ * block.
195
+ */
196
+ try {
197
+ await this . hooks . executeAfter ( ctx )
198
+ requestAction . end ( { status_code : res . statusCode } )
199
+ ctx . response . finish ( )
200
+ } catch ( error ) {
201
+ await this . exception . handle ( error , ctx )
202
+ requestAction . end ( { status_code : res . statusCode , error } )
203
+ ctx . response . finish ( )
204
+ }
205
+ } )
192
206
}
193
207
}
0 commit comments