|
| 1 | +# Propagate Internal Framework Errors |
| 2 | + |
| 3 | +The Functions Framework normally sends express level errors to the default express error handler which sends the error to the calling client with an optional stack trace if in a non-prod environment. |
| 4 | + |
| 5 | +## Example |
| 6 | + |
| 7 | +```ts |
| 8 | +const app = express(); |
| 9 | + |
| 10 | +app.post("/", (req, res) => { |
| 11 | +... |
| 12 | +}); |
| 13 | + |
| 14 | +// User error handler |
| 15 | +app.use((err, req, res, next) => { |
| 16 | + logger.log(err); |
| 17 | + res.send("Caught error!"); |
| 18 | +}); |
| 19 | + |
| 20 | +functions.http("helloWorld, app); |
| 21 | +``` |
| 22 | +
|
| 23 | +```ts |
| 24 | +// Post request with bad JSON |
| 25 | +http.post("/", "{"id": "Hello}"); |
| 26 | +``` |
| 27 | +
|
| 28 | +Default express error handler: |
| 29 | +
|
| 30 | +``` |
| 31 | +SyntaxError: Expected double-quoted property name in JSON at position 20 (line 3 column 1) |
| 32 | + at JSON.parse (<anonymous>) |
| 33 | + at parse (functions-framework-nodejs/node_modules/body-parser/lib/types/json.js:92:19) |
| 34 | + at functions-framework-nodejs/node_modules/body-parser/lib/read.js:128:18 |
| 35 | + at AsyncResource.runInAsyncScope (node:async_hooks:211:14) |
| 36 | + at invokeCallback (functions-framework-nodejs/node_modules/raw-body/index.js:238:16) |
| 37 | + at done (functions-framework-nodejs/node_modules/raw-body/index.js:227:7) |
| 38 | + at IncomingMessage.onEnd (functions-framework-nodejs/node_modules/raw-body/index.js:287:7) |
| 39 | + at IncomingMessage.emit (node:events:518:28) |
| 40 | + at endReadableNT (node:internal/streams/readable:1698:12) |
| 41 | + at process.processTicksAndRejections (node:internal/process/task_queues:90:21) |
| 42 | +``` |
| 43 | +
|
| 44 | +## Propagating Errors |
| 45 | +
|
| 46 | +If you want to propgate internal express level errors to your application, enabling the propagate option and defining a custom error handler will allow your application to receive errors: |
| 47 | +
|
| 48 | +1. In your `package.json`, specify `--propagate-framework-errors=true"` for the `functions-framework`: |
| 49 | + |
| 50 | +```sh |
| 51 | +{ |
| 52 | + "scripts": { |
| 53 | + "start": "functions-framework --target=helloWorld --propagate-framework-errors=true" |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +2. Define a express error handler: |
| 59 | + |
| 60 | +```ts |
| 61 | +const app = express(); |
| 62 | +
|
| 63 | +// User error handler |
| 64 | +app.use((err, req, res, next) => { |
| 65 | + logger.log(err); |
| 66 | + res.send("Caught error!"); |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +Now your application will receive internal express level errors! |
| 71 | + |
| 72 | +```ts |
| 73 | +// Post request with bad JSON |
| 74 | +http.post("/", "{"id": "Hello}"); |
| 75 | +``` |
| 76 | + |
| 77 | +The custom error handler logic executes: |
| 78 | + |
| 79 | +``` |
| 80 | +Caught error! |
| 81 | +``` |
0 commit comments