4

I am trying to figure out how to properly error handle with react streams. Let say there is an error inside <App />. What is the best way to catch the error? The catch block don't seem to be catching the error as it doesn't console.log anything when I throw an error inside the <App /> component. I tried to add stream.on('error', (err) => {console.log(err)}); and that doesn't catch the error either. How should I catch the error in App on the server-side?

try {
  const header = <div>Hello</div>
  const footer = <div>Bye</div>
  const sheet = new ServerStyleSheet();
  const jsx = sheet.collectStyles(<App />);
  const stream = sheet.interleaveWithNodeStream(renderToNodeStream(jsx));
  ctx.body = multi_stream([
    string_stream(header),
    stream,
    string_stream(footer)
  ]);
} catch (err) {
  console.log('There is an error', err);
  ctx.throw(err.status, 'Failed to SSR');
}
PBandJ
  • 2,537
  • 3
  • 21
  • 31

0 Answers0