MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/functions-framework-nodejs / wrapHttpFunction

Function wrapHttpFunction

src/function_wrappers.ts:103–128  ·  view source on GitHub ↗
(execute: HttpFunction)

Source from the content-addressed store, hash-verified

101 * @return An Express handler function
102 */
103const 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.

Callers 5

wrapCloudEventFunctionFunction · 0.85
wrapEventFunctionFunction · 0.85
wrapUserFunctionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected