(execute: HttpFunction)
| 101 | * @return An Express handler function |
| 102 | */ |
| 103 | const wrapHttpFunction = (execute: HttpFunction): RequestHandler => { |
| 104 | return (req: Request, res: Response) => { |
| 105 | const d = domain.create(); |
| 106 | const errorHandler = (err: Error) => { |
| 107 | if (res.locals.functionExecutionFinished) { |
| 108 | console.error(`Exception from a finished function: ${err}`); |
| 109 | } else { |
| 110 | res.locals.functionExecutionFinished = true; |
| 111 | sendCrashResponse({err, res}); |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | // Catch unhandled errors originating from this request. |
| 116 | d.on('error', errorHandler); |
| 117 | |
| 118 | d.run(() => { |
| 119 | process.nextTick(() => { |
| 120 | const ret = execute(req, res); |
| 121 | // Catch rejected promises if the function is async. |
| 122 | if (ret instanceof Promise) { |
| 123 | ret.catch(errorHandler); |
| 124 | } |
| 125 | }); |
| 126 | }); |
| 127 | }; |
| 128 | }; |
| 129 | |
| 130 | /** |
| 131 | * Wraps an async CloudEvent function in an express RequestHandler. |
no outgoing calls
no test coverage detected