@@ -3,21 +3,17 @@ import {Express, Router} from 'express';
33
44import { cspMiddleware , getAppPresets } from './csp/middleware' ;
55import {
6- AppErrorHandler ,
76 AppMiddleware ,
87 AppMountDescription ,
98 AppRouteDescription ,
109 AppRouteHandler ,
1110 AppRoutes ,
1211 AuthPolicy ,
13- ExpressFinalError ,
1412 HTTP_METHODS ,
1513 HttpMethod ,
1614} from './types' ;
1715import { prepareCSRFMiddleware } from './csrf' ;
1816
19- import { validationErrorMiddleware } from './validator' ;
20-
2117function isAllowedMethod ( method : string ) : method is HttpMethod | 'mount' {
2218 // eslint-disable-next-line @typescript-eslint/no-explicit-any
2319 return HTTP_METHODS . includes ( method as any ) || method === 'mount' ;
@@ -26,21 +22,24 @@ function isAllowedMethod(method: string): method is HttpMethod | 'mount' {
2622function wrapMiddleware ( fn : AppMiddleware , i ?: number ) : AppMiddleware {
2723 const result : AppMiddleware = async ( req , res , next ) => {
2824 const reqCtx = req . ctx ;
25+ const ctx = reqCtx . create ( `${ fn . name || `noname-${ i } ` } middleware` ) ;
26+
2927 let ended = false ;
28+ req . ctx = ctx ;
29+
3030 try {
31- return await reqCtx . call ( `${ fn . name || `noname-${ i } ` } middleware` , async ( ctx ) => {
32- req . ctx = ctx ;
33- return await fn ( req , res , ( ...args : unknown [ ] ) => {
34- req . ctx = reqCtx ;
35- ended = true ;
36- next ( ...args ) ;
37- } ) ;
31+ await fn ( req , res , ( ...args : unknown [ ] ) => {
32+ ctx . end ( ) ;
33+ req . ctx = reqCtx ;
34+ ended = true ;
35+ next ( ...args ) ;
3836 } ) ;
3937 } catch ( error ) {
40- return next ( error ) ;
41- } finally {
4238 if ( ! ended ) {
39+ ctx . fail ( error ) ;
4340 req . ctx = reqCtx ;
41+ next ( error ) ;
42+ return ;
4443 }
4544 }
4645 } ;
@@ -53,7 +52,7 @@ const UNNAMED_CONTROLLER = 'unnamedController';
5352function wrapRouteHandler ( fn : AppRouteHandler , handlerName ?: string ) {
5453 const handlerNameLocal = handlerName || fn . name || UNNAMED_CONTROLLER ;
5554
56- const handler : AppMiddleware = ( req , res , next ) => {
55+ const handler : AppMiddleware = async ( req , res , next ) => {
5756 req . ctx = req . originalContext . create ( handlerNameLocal ) ;
5857 if ( req . routeInfo . handlerName !== handlerNameLocal ) {
5958 if ( req . routeInfo . handlerName === UNNAMED_CONTROLLER ) {
@@ -62,12 +61,16 @@ function wrapRouteHandler(fn: AppRouteHandler, handlerName?: string) {
6261 req . routeInfo . handlerName = `${ req . routeInfo . handlerName } (${ handlerNameLocal } )` ;
6362 }
6463 }
65- Promise . resolve ( fn ( req , res ) )
66- . catch ( next )
67- . finally ( ( ) => {
68- req . ctx . end ( ) ;
69- req . ctx = req . originalContext ;
70- } ) ;
64+ try {
65+ await fn ( req , res ) ;
66+ req . ctx . end ( ) ;
67+ req . ctx = req . originalContext ;
68+ } catch ( error ) {
69+ req . ctx . fail ( error ) ;
70+ req . ctx = req . originalContext ;
71+ next ( error ) ;
72+ return ;
73+ }
7174 } ;
7275
7376 Object . defineProperty ( handler , 'name' , { value : handlerNameLocal } ) ;
@@ -102,7 +105,7 @@ export function setupRoutes(ctx: AppContext, expressApp: Express, routes: AppRou
102105 } = route ;
103106
104107 const handlerName = routeHandlerName || route . handler . name || UNNAMED_CONTROLLER ;
105- const authPolicy = routeAuthPolicy || ctx . config . appAuthPolicy || AuthPolicy . disabled ;
108+ const authPolicy = routeAuthPolicy || ctx . config . appAuthPolicy || ` ${ AuthPolicy . disabled } ` ;
106109 const enableCaching =
107110 typeof routeEnableCaching === 'boolean'
108111 ? routeEnableCaching
@@ -155,7 +158,7 @@ export function setupRoutes(ctx: AppContext, expressApp: Express, routes: AppRou
155158 routeMiddleware . push (
156159 cspMiddleware ( {
157160 appPresets,
158- routPresets : cspPresets ,
161+ routePresets : cspPresets ,
159162 reportOnly : ctx . config . expressCspReportOnly ,
160163 reportTo : ctx . config . expressCspReportTo ,
161164 reportUri : ctx . config . expressCspReportUri ,
@@ -175,7 +178,7 @@ export function setupRoutes(ctx: AppContext, expressApp: Express, routes: AppRou
175178 routeMiddleware . push ( authHandler ) ;
176179
177180 if ( ctx . config . appCsrfSecret ) {
178- routeMiddleware . push ( prepareCSRFMiddleware ( ctx , authPolicy as AuthPolicy ) ) ;
181+ routeMiddleware . push ( prepareCSRFMiddleware ( ctx , authPolicy ) ) ;
179182 }
180183 }
181184
@@ -194,25 +197,4 @@ export function setupRoutes(ctx: AppContext, expressApp: Express, routes: AppRou
194197 expressApp [ method ] ( routePath , wrappedMiddleware , handler ) ;
195198 }
196199 } ) ;
197-
198- const errorHandler = ctx . config . appValidationErrorHandler
199- ? ctx . config . appValidationErrorHandler ( ctx )
200- : validationErrorMiddleware ;
201-
202- expressApp . use ( errorHandler ) ;
203-
204- if ( ctx . config . appFinalErrorHandler ) {
205- const appFinalRequestHandler : AppErrorHandler = ( error , req , res , next ) =>
206- Promise . resolve ( ctx . config . appFinalErrorHandler ?.( error , req , res , next ) ) . catch ( next ) ;
207- expressApp . use ( appFinalRequestHandler ) ;
208- }
209-
210- const finalRequestHandler : AppErrorHandler = ( error : ExpressFinalError , _ , res , __ ) => {
211- const errorDescription = 'Unhandled error during request processing' ;
212- ctx . logError ( errorDescription , error ) ;
213- const statusCode = ( error && error . statusCode ) || 500 ;
214- res . status ( statusCode ) . send ( statusCode === 400 ? 'Bad request' : 'Internal server error' ) ;
215- } ;
216-
217- expressApp . use ( finalRequestHandler ) ;
218200}
0 commit comments