Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: propagate framework errors to user client application #685

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

gregorygaines
Copy link

@gregorygaines gregorygaines commented Mar 30, 2025

Currently, errors that happen in the framework express layer are passed to the default express error handler. The default error handler returns the error to the client with a stack trace if in non-prod enviornments. It would be useful for certain cases for the user to have context of what happened during a request instead of the user being bypassed entirely. Instead, if the user has an error handler middleware installed in their application, we should pass the framework error to the user's error handler.

For example, if the framework gets a request with a bad json body.

const app = express():

app.post("/", (req, res) => {
 ...
});

// User error handler
app.use((err, req, res, next) => {
  logger.log(err);
  res.send("Caught error!");
});

functions.http("function, app);
// Post request with bad JSON
http.post("/", "{"id": "Hello}");

The framework responds with the following not very helpful message from the default Express error handler:

SyntaxError: Expected double-quoted property name in JSON at position 20 (line 3 column 1)
    at JSON.parse (<anonymous>)
    at parse (functions-framework-nodejs/node_modules/body-parser/lib/types/json.js:92:19)
    at functions-framework-nodejs/node_modules/body-parser/lib/read.js:128:18
    at AsyncResource.runInAsyncScope (node:async_hooks:211:14)
    at invokeCallback (functions-framework-nodejs/node_modules/raw-body/index.js:238:16)
    at done (functions-framework-nodejs/node_modules/raw-body/index.js:227:7)
    at IncomingMessage.onEnd (functions-framework-nodejs/node_modules/raw-body/index.js:287:7)
    at IncomingMessage.emit (node:events:518:28)
    at endReadableNT (node:internal/streams/readable:1698:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:90:21)

This change introduces a propagate error to client middleware that when enabled, propagates errors to the user client application error middleware chain, where we give power to the user to handle the error, log details, or do whatever they want to do:

// Post request with bad JSON
http.post("/", "{"id": "Hello}");

The framework responds with a user defined message from the user's error handler!:

"Caught error!"

Don't forget the user also logged this error, giving them more visibility and control of their application. I think this is a useful change for users who want a bit more autonomy.

@gregorygaines gregorygaines force-pushed the propagate-error-to-client branch from 5579ccb to 90ea72e Compare March 30, 2025 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant