File tree Expand file tree Collapse file tree 3 files changed +22
-10
lines changed Expand file tree Collapse file tree 3 files changed +22
-10
lines changed Original file line number Diff line number Diff line change @@ -40,7 +40,11 @@ export interface RunHandlerResult {
4040 * data populated into the response.
4141 */
4242export function runHandler (
43- handler : express . Handler ,
43+ handler : (
44+ req : https . Request ,
45+ res : express . Response ,
46+ next ?: express . NextFunction
47+ ) => void | Promise < void > ,
4448 request : https . Request
4549) : Promise < RunHandlerResult > {
4650 return new Promise ( ( resolve ) => {
@@ -119,7 +123,7 @@ export function runHandler(
119123 }
120124 }
121125 const response = new MockResponse ( ) ;
122- handler ( request , response as any , ( ) => undefined ) ;
126+ return void handler ( request , response as any , ( ) => undefined ) ;
123127 } ) ;
124128}
125129
Original file line number Diff line number Diff line change @@ -17,21 +17,25 @@ describe("CloudHttpsBuilder async onRequest", () => {
1717 } ) ;
1818
1919 it ( "should catch and log unhandled rejections in async onRequest handlers" , async ( ) => {
20- const error = new Error ( "Async error " ) ;
20+ const err = new Error ( "boom " ) ;
2121 const fn = https . onRequest ( async ( _req , _res ) => {
22- throw error ;
22+ await Promise . resolve ( ) ;
23+ throw err ;
2324 } ) ;
2425
2526 const req = new MockRequest ( { } , { } ) ;
2627 req . method = "GET" ;
2728
28- await runHandler ( fn , req as any ) ;
29+ const result = await runHandler ( fn , req as any ) ;
2930
30- expect ( loggerSpy . calledWith ( "Unhandled error" , error ) ) . to . be . true ;
31+ expect ( loggerSpy . calledWith ( "Unhandled error" , err ) ) . to . be . true ;
32+ expect ( result . status ) . to . equal ( 500 ) ;
33+ expect ( result . body ) . to . equal ( "Internal Server Error" ) ;
3134 } ) ;
3235
3336 it ( "should not log if handler completes successfully" , async ( ) => {
3437 const fn = https . onRequest ( async ( _req , res ) => {
38+ await Promise . resolve ( ) ;
3539 res . send ( 200 ) ;
3640 } ) ;
3741
Original file line number Diff line number Diff line change @@ -17,21 +17,25 @@ describe("v2.https.onRequest async", () => {
1717 } ) ;
1818
1919 it ( "should catch and log unhandled rejections in async onRequest handlers" , async ( ) => {
20- const error = new Error ( "Async error v2 " ) ;
20+ const err = new Error ( "boom " ) ;
2121 const fn = https . onRequest ( async ( _req , _res ) => {
22- throw error ;
22+ await Promise . resolve ( ) ;
23+ throw err ;
2324 } ) ;
2425
2526 const req = new MockRequest ( { } , { } ) ;
2627 req . method = "GET" ;
2728
28- await runHandler ( fn , req as any ) ;
29+ const result = await runHandler ( fn , req as any ) ;
2930
30- expect ( loggerSpy . calledWith ( "Unhandled error" , error ) ) . to . be . true ;
31+ expect ( loggerSpy . calledWith ( "Unhandled error" , err ) ) . to . be . true ;
32+ expect ( result . status ) . to . equal ( 500 ) ;
33+ expect ( result . body ) . to . equal ( "Internal Server Error" ) ;
3134 } ) ;
3235
3336 it ( "should not log if handler completes successfully" , async ( ) => {
3437 const fn = https . onRequest ( async ( _req , res ) => {
38+ await Promise . resolve ( ) ;
3539 res . send ( 200 ) ;
3640 } ) ;
3741
You can’t perform that action at this time.
0 commit comments