在fetch api的catch块中重新引发错误时,未触发Web Worker onError事件处理程序 [英] web worker onerror event handler not triggered when rethrowing an error in the catch block of fetch api
问题描述
TL; DR; 是否存在在Promise(即fetch-api)和Webworker全局错误事件处理程序中重新抛出错误的问题?
我很难将网络工作者中的获取api引发的错误鼓吹到启动网络工作者的客户端上.我看到" 未捕获(承诺)错误 ",而不是Webworkers self.onerror 事件中的代码经理...当我在同一Web Worker中的同步函数中引发错误时,它的确会出现在self.onerror事件处理程序中...
要进一步解释,在我的Webworker内部:
- onerror事件处理程序
- 使用获取api调用webapi的功能
我想传递调用Web api到客户端时遇到的某些错误...因此,我在获取的末尾(在catch块中)抛出了错误.这就是我当前的测试结果:
fetch('https://< server>/api/token?key = version',{方法:'GET',凭据:'include'}).then((response)=> {抛出新的错误(哦,不-出了点问题");返回response.json();}).then((data)=> {/*对正常流中的数据执行某些操作*/}).catch((错误)=> {/*我们要传递某些类型的错误*/投掷(错误);/*< ----这是我在F12调试窗口中看到的最后一个错误->})
但是我从不看到错误到达onerror事件处理程序,看起来像这样(只是一些测试输出):
self.onerror =函数(事件){console.trace(event);}
我看到(使用F12调试)在获取api捕获的行上记录了 未捕获(承诺)错误
我忽略了什么?重新抛出错误和全局onerror事件处理程序是否存在问题?
事实证明,有两种方法可以确保promise(fetch-api基于promise)中的重新抛出错误:>
->为您的网络工作人员添加 unhandledrejection 的事件处理程序:
self.addEventListener('unhandledrejection',function(event){//事件对象具有两个特殊属性://event.promise-产生错误的承诺//event.reason-未处理的错误对象抛出event.reason;});
TL;DR; Is there an issue with rethrowing errors in promises (i.e. the fetch-api) and the webworker global onerror event handler ?
I'm having a hard time bubbling up an error thrown by the fetch api in a web worker to the client that started the web worker. I see an "Uncaught (in promise) Error" instead of the code in the webworkers self.onerror event handler.... When I throw an error in a synchronous function in that same web worker it does end up in the self.onerror event handler...
To explain in further detail, inside my webworker I'm having :
- an onerror event handler
- functions that call a webapi with the fetch api
I want to pass along certain errors encountered when calling the web api to my client... Thus I rethrow the error at the end of the fetch (in the catch block). This is how my current test looks like :
fetch('https://<server>/api/token?key=version', { method: 'GET', credentials: 'include'})
.then((response) => {
throw new Error("Oh no - something went wrong");
return response.json();
})
.then((data) => {
/* do something with the data in normal flow */
})
.catch((error) => {
/* we want to pass along certain types of errors */
throw(error); /* <---- this is the last error I see logged in F12 debug windows -->
})
However i never see the error reaching the onerror event handler, which looks like this (just some test output) :
self.onerror = function (event) {
console.trace(event);
}
I see (using F12 debug) that an Uncaught (in promise) Error is logged at the line that the fetch api catch
Anything I'm overlooking ? Is there an issue with rethrowing errors and the global onerror event handler ?
It turns out there are two ways that I can make sure a rethrown error in a promise (fetch-api is promise-based) get's bubbled up :
1 ) The trick mentioned in How to bubble a web worker error in a promise via worker.onerror?
-- > just wrap the throw (inside the fetch catch block) in a setTimeout :
fetch
.then()
.catch((error) => {
setTimeout(function() { throw error; });
})
2) The new way mentioned in How to catch uncaught exception in Promise
-- > add an eventhandler for unhandledrejection to your webworker :
self.addEventListener('unhandledrejection', function (event) {
// the event object has two special properties:
// event.promise - the promise that generated the error
// event.reason - the unhandled error object
throw event.reason;
});
这篇关于在fetch api的catch块中重新引发错误时,未触发Web Worker onError事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!