在我的应用程序中,我使用全局事件处理程序来处理“unhandledrejection”事件。其目的是捕捉对本地服务器的各种网络请求的错误并处理“服务器无响应”的情况。
事实上它正在工作,但我仍然在 FF 中得到一个红色控制台。
这是我的代码:
private setupEmergencyHandler() {
window.addEventListener('unhandledrejection', e => {
e.preventDefault();
e.promise.catch((error: Error) => {
if (
!this.emergencyHandlerIsWorking &&
error.message === REST_CLIENT_ERROR.CONNECTION_REFUSED
) {
this.emergencyHandlerIsWorking = true;
this.popup404.show();
const retry = async () => {
try {
await REST_CLIENT_GARAGE.getCars();
await this.sendRebootMsg();
this.popup404.hide();
this.emergencyHandlerIsWorking = false;
} catch {
await new Promise(res => setTimeout(res, 1000));
retry();
}
};
retry();
}
});
});
}
问题是为什么在 chromium 中我只收到 HTTP 请求错误:
GET *** net::ERR_CONNECTION_REFUSED
但在 FF 我得到相同的结果:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at *** (Reason: CORS request did not succeed).
并且:
Uncaught (in promise) Error: CONNECTION_REFUSED