admin管理员组文章数量:1327668
I'm trying to catch a bunch of errors related to the same origin policy when using the Fetch API but without any success:
window.onerror = (message, file, line, col, error) => console.log(error)
window.addEventListener('error', (error) => console.log(error))
try {
fetch('/').catch(e => {
console.log('Caugth error:')
console.log(e)
console.log(JSON.stringify(e))
})
}
catch (e) {
console.log('try-catch')
console.log(e)
}
The errors I want to catch only appear in the web console:
See code example here:
How can I catch these errors to provide an on-screen message?
EDIT: The fetch's catch block is actually executed.
fetch('/')
.then(response => response.text())
.then(pre)
.catch(e => {
pre(`Caugth error: ${e.message}`)
})
function pre(text) {
var pre = document.createElement('pre')
document.body.insertAdjacentElement('afterbegin', pre)
pre.insertAdjacentText('beforeend', text)
}
pre {
border: 1px solid black;
margin: 20px;
padding: 20px;
}
I'm trying to catch a bunch of errors related to the same origin policy when using the Fetch API but without any success:
window.onerror = (message, file, line, col, error) => console.log(error)
window.addEventListener('error', (error) => console.log(error))
try {
fetch('https://www.bitstamp/api/ticker/').catch(e => {
console.log('Caugth error:')
console.log(e)
console.log(JSON.stringify(e))
})
}
catch (e) {
console.log('try-catch')
console.log(e)
}
The errors I want to catch only appear in the web console:
See code example here: https://github./nyg/fetch-error-test
How can I catch these errors to provide an on-screen message?
EDIT: The fetch's catch block is actually executed.
fetch('https://www.bitstamp/api/ticker/')
.then(response => response.text())
.then(pre)
.catch(e => {
pre(`Caugth error: ${e.message}`)
})
function pre(text) {
var pre = document.createElement('pre')
document.body.insertAdjacentElement('afterbegin', pre)
pre.insertAdjacentText('beforeend', text)
}
pre {
border: 1px solid black;
margin: 20px;
padding: 20px;
}
Share
Improve this question
edited Aug 6, 2017 at 12:04
nyg
asked Aug 4, 2017 at 17:11
nygnyg
2,5523 gold badges29 silver badges44 bronze badges
0
1 Answer
Reset to default 3As far as I remember, you can not catch
browser driven exceptions in your typical try->catch
or a catch chain
inside of fetch
.
CORS exceptions are thrown with intent, to have the user browsing the site, know of such abnormalities if you may call them that, and to protect any leak of possible secure information on the called api/server
Read here Of a previous SO discussion on whether you could catch these errors with an exception handler
If the request throws an error that can be part of the response , like a status error and such, then you may catch it and show a custom message
本文标签: javascriptCatching quotFailed to load resourcequot when using the Fetch APIStack Overflow
版权声明:本文标题:javascript - Catching "Failed to load resource" when using the Fetch API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742223121a2435578.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论