我有兩個簡單的包裝處理單個頁面應用程序中的請求。一包fetch,如果一個反應是不正常引發錯誤(沒有在200-300範圍內):爲什麼提取錯誤在我的單頁應用程序中沒有堆棧跟蹤?
const fetchy = (...args) =>
fetch(...args).then(response => {
if (response.ok) {
return response
}
throw new Error(response.statusText)
})
export default fetchy
還有一包fetchy,用於GET請求:
const get = endpoint => {
const headers = new Headers({ Authorization: `Bearer ${TOKEN}` })
const init = { method: 'GET', headers }
return fetchy(endpoint, init)
}
現在我「M在像這樣的操作使用它們(這是一個redux-thunk行動的創建者):
export const fetchArticles =() => dispatch => {
dispatch({ type: types.FETCH_ARTICLES })
return get(endpoints.ARTICLES)
.then(response => response.json())
.then(data => normalize(data.items, [schemas.articles]))
.then(normalized => dispatch(fetchArticlesSuccess(normalized)))
// fetch errors caught here do not have error.stack
.catch(error => dispatch(fetchArticlesFail(error)))
}
所以我趕上在獲取自身(網絡錯誤)這兩個錯誤,並從012返回的錯誤包裝(api錯誤)。問題是來自fetchArticles的抓取網絡錯誤不包含堆棧跟蹤。所以error.stack
不存在。這是搞亂我的錯誤報告。
這是一個有效的錯誤,並且error instanceof Error === true
和error.message === 'Failed to fetch'
。那麼爲什麼這個錯誤沒有堆棧跟蹤?我該如何解決它?這似乎也許我可以添加一個錯誤回調fetchy並重新拋出任何錯誤,但這對我來說似乎很奇怪(但也許我錯了)。
顯然,error.stack是非標準的:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Error/stack。但是,由於該錯誤是在支持error.stack的瀏覽器(Chrome 57)中引發的,因此抓取沒有堆棧跟蹤似乎很奇怪。它不使用相同的錯誤構造函數嗎? – vsjn3290ckjnaoij2jikndckjb