@@ -141,28 +141,26 @@ class CoroutineRouter(val coroutineStart: CoroutineStart, val router: Router) {
141141 route(OPTIONS , pattern, handler)
142142
143143 fun route (method : String , pattern : String , handler : suspend HandlerContext .() -> Any ): Route =
144- router
145- .route(method, pattern) { ctx ->
146- val handlerContext = HandlerContext (ctx)
147- launch(handlerContext) {
144+ router.route(method, pattern) { ctx ->
145+ val handlerContext = HandlerContext (ctx)
146+ launch(handlerContext) {
147+ try {
148+ val result = handler(handlerContext)
149+ ctx.route.after?.apply (ctx, result, null )
150+ if (result != ctx && ! ctx.isResponseStarted) {
151+ ctx.render(result)
152+ }
153+ } catch (cause: Throwable ) {
148154 try {
149- val result = handler(handlerContext)
150- ctx.route.after?.apply (ctx, result, null )
151- if (result != ctx && ! ctx.isResponseStarted) {
152- ctx.render(result)
153- }
154- } catch (cause: Throwable ) {
155- try {
156- ctx.route.after?.apply (ctx, null , cause)
157- } finally {
158- errorHandler.invoke(ErrorHandlerContext (ctx, cause, router.errorCode(cause)))
159- }
155+ ctx.route.after?.apply (ctx, null , cause)
156+ } finally {
157+ errorHandler.invoke(ErrorHandlerContext (ctx, cause, router.errorCode(cause)))
160158 }
161159 }
162- // Return context to mark as handled
163- ctx
164160 }
165- .setHandle(handler)
161+ // Return context to mark as handled
162+ ctx
163+ }
166164
167165 internal fun launch (handlerContext : HandlerContext , block : suspend CoroutineScope .() -> Unit ) {
168166 // Global catch-all exception handler
0 commit comments