Skip to content

Handle Thrown Exceptions #60

Open
Open
@grayside

Description

@grayside

Current Behavior

If an exception is thrown in developer code, the functions framework leaves it unhandled.

call_user_func($this->function, $cloudevent);
return new Response();

Assuming nowhere else in the Function Framework catches the exception, this will ultimately crash the FPM process. FPM error logging will probably handle that crash (and place in Error Logging), but the next request will incur cold start costs.

In addition to the crashing, this means developers do not have a mechanism to request a Pub/Sub retry without triggering a crash.

Expected Behavior

  • Functions Framework catches the thrown exception, creates an HTTP 500 response, and logs the error.
  • Exceptions do not crash the FPM process. If the developer wants to stop the container use exit().
  • Logged error should align with the format for error grouping and lift to Cloud Error Reporting if possible
  • X-Google-Status header is set to error. I'm not familiar with what this does, but see it in multiple frameworks. Maybe @grant has insight?

Other Language Implementations

Golang Implementation

Node.js can infer errors from callbacks and rejected promises in addition to thrown errors. On any error, set X-Google-Status header to 'error':

https://github.com/GoogleCloudPlatform/functions-framework-go/blob/fed357741a8521eeeaad9b32dc140d69ea69626c/funcframework/framework.go#L207

On thrown errors, send a crash response:

https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/01b7df3128cd5c6b736aa0c6cedb35e53a3e92ad/src/invoker.ts#L247-283

https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/01b7df3128cd5c6b736aa0c6cedb35e53a3e92ad/src/logger.ts#L25-52

Node.js Implementation

Set X-Google-Status header to 'error' and return the error message:

https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/01b7df3128cd5c6b736aa0c6cedb35e53a3e92ad/src/invoker.ts#L65-72

Metadata

Metadata

Assignees

Labels

P2bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions