@@ -27,6 +27,7 @@ import { HttpContext } from '../HttpContext'
2727import { RequestHandler } from './RequestHandler'
2828import { MiddlewareStore } from '../MiddlewareStore'
2929import { ExceptionManager } from './ExceptionManager'
30+ import { InternalAsyncHttpContext } from '../AsyncHttpContext'
3031
3132/**
3233 * Server class handles the HTTP requests by using all Adonis micro modules.
@@ -123,6 +124,13 @@ export class Server implements ServerContract {
123124 )
124125 }
125126
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+
126134 /**
127135 * Define custom error handler to handler all errors
128136 * occurred during HTTP request
@@ -160,34 +168,40 @@ export class Server implements ServerContract {
160168
161169 const requestAction = this . getProfilerRow ( request )
162170 const ctx = this . getContext ( request , response , requestAction )
171+ const asyncContext = this . getAsyncContext ( ctx )
163172
164173 /*
165- * Handle request by executing hooks, request middleware stack
166- * and route handler
174+ * Run everything within the async HTTP context
167175 */
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+ }
173186
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+ } )
192206 }
193207}
0 commit comments