How to handle stream errors passed to reply.raw?
- Dominant language
- No language data
- Stars
- 68
- Forks
- 8
- Avg merge
- 11h 2m
- Merged PRs (30d)
- 2
Description
## 💬 Question here
Try to use React 18 SSR converting original code https://reactjs.org/docs/react-dom-server.html#rendertopipeablestream for Express
```js
const App = () => {
throw new Error('some error in React App');
};
```
set Errorhandler
```js
fastifyInstance.setErrorHandler(function (error, req, res) {
.....
}
```
and registered the router
```js
fastify.route({
url: '/',
method: 'GET',
handler: async (req, reply) => {
let didError = false;
// here is another strange thing:
// if you do not place this code here and put it inside the renderToPipeableStream.onShellReady -
// the server will not give anything when requested !!!
//
// this code doesn't work either:
// reply.raw.statusCode = 200;
// reply.raw.setHeader('Content-Type', 'text/html; charset=utf-8')
reply.raw.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
const stream = renderToPipeableStream(, {
onShellReady: async () => {
reply.raw.statusCode = didError ? 500 : 200;
reply.raw.write('');
stream.pipe(reply.raw);
reply.raw.end('');
},
onShellError: (error) => {
didError = true;
req.log.error({ error, where: 'onShellError' });
reply.raw.statusCode = 500;
reply.raw.end('Error !!');
},
onError: async (error) => {
didError = true;
req.log.error({ error, where: 'onError' });
},
});
}
});
````
When try to render the App get the `uncaughtException` instead of being intercepted by the application handler (so the service is shutting down due to this error):
```
"Error: onError returned something with a type other than \"string\". onError should return a string and may return null or undefined but must not return anything else. It received something of type \"object\" instead
at Y (webpack://web-app/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:69:70)
at Qc (webpack://web-app/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:88:311)
at Immediate. (webpack://web-app/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:98:56)
at process.processImmediate (node:internal/timers:471:21)"
```
How to intercept such errors by the fastify application?
and an additional question: why if you put the code
```js
reply.raw.writeHead(HTTP_OK_CODE, { 'Content-Type': 'text/html; charset=utf-8' });
```
inside the function `renderToPipeableStream.onShellReady` (where it belongs) the server returns an empty string?
## Your Environment
- *node version*: 18
- *fastify version*: 4.5.3
- *os*: Mac, Windows
- React 18
Contributor guide
Assessment
This issue has not been assessed yet.