我在捕获服务器渲染错误时遇到问题,对我来说奇怪的是它到达了 catch 语句,但它仍然冒泡并崩溃并获得未捕获的异常。
这是我的功能的剥离版本:
export default (htmlFilePath, observer) => async (req, res) => {
try {
const { state } = await rehydrate.dispatchRequired(store, [
setMobileDetect.bind(null, mobileDetect),
...REQUIRED_ACTIONS,
]);
// Will dispatch the required data through the store,
// the returned value is not important
const matches = rehydrate.getMatches(req, state);
let status = 200;
componentNames = matches.map(
({ route }) => route.component.displayName,
);
await rehydrate.rehydrate(matches, store, state);
const markupStream = renderToNodeStream(
<Provider store={store}>
<ConnectedRouter history={history} location={context}>
<App />
</ConnectedRouter>
</Provider>,
);
if (context.url) {
return res.redirect(301, context.url);
}
// if no routes match we return 404
if (!matches.length) {
status = 404;
}
return fs
.createReadStream(htmlFilePath)
.pipe(htmlReplace("#root", markupStream))
.pipe(
replaceStream("__SERVER_DATA__", serialize(store.getState())),
)
.pipe(res.status(status));
} catch (err) {
const errMarkup = renderToNodeStream(
<Provider store={store}>
<ConnectedRouter history={history} location={context}>
<Error500 error={err} />
</ConnectedRouter>
</Provider>,
);
logger.log({
level: "error",
message: `Rendering ${
req.originalUrl
} fallback to Error render method`,
...{
errorMessage: err.message,
stack: err.stack,
},
});
return fs
.createReadStream(htmlFilePath)
.pipe(htmlReplace("#root", errMarkup))
.pipe(res.status(500));
} finally {
logger.info(
`Request finished ${req.originalUrl} :: ${res.statusCode}`,
);
end({
route: path,
componentName: componentNames[0],
code: res.statusCode,
});
logger.profile(profileMsg);
}
};
我希望我的 catch 块返回 500 个错误的模块,但节点实例由于来自markupStream()的未捕获异常而崩溃
更新:根据要求,这是日志: https ://pastebin.com/A4vL8zcc