I've been struggling with a puzzling error for nearly a day now. It appears that my rejected promise is slipping through without being caught by the catch
block. While I'm relatively new to using es6
, async
, and await
, it does remind me of concepts I am familiar with in C#.
Get: async function (request, cancelToken = null) {
console.info('Sending GET', request)
store.commit('global/setLoader', true)
let headers = getAuthTokenHeader()
try {
var res = await Vue.axios({
url: baseUrl + request.Url,
params: request.Params,
method: 'GET',
headers
}).catch(function (error) {
console.error('Failed to GET: ' + error)
if (typeof error.response !== 'undefined' && typeof error.response.data !== 'undefined' && error.response.data !== null) {
throw new Error(error.response.data)
} else {
throw error
}
})
console.info('Request received', res)
return res
} finally {
store.commit('global/setLoader', false)
}
},
The issue arises on the line
if (typeof error.response !== 'undefined' && typeof error.response.data !== 'undefined' && error.response.data !== null) {
Based on information from https://javascript.info/async-await#error-handling
I shouldn't be seeing this "Uncaught (in promise)" error because I have the entire asynchronous call wrapped in an await
.
The calling code also waits for the completion of this code execution. Here is how the call looks like for reference.
static async GetPackagingDimensions (input, Id) {
const request = {
Url: 'api/packing/getPackagingBoxDimensionsByBarCode',
Params: {
boxBarcode: input,
orderId: Id
}
}
const res = await HttpUtil.Get(request)
return res
}
Interestingly, if I enclose the calling code itself within a try
catch
block, the error gets caught appropriately.
What could possibly prevent me from catching the rejected promise within my catch block?
Quick Edit
- Even my console.log
statement within the catch block is not getting executed, indicating that the error surfaces beyond that point.