I am trying to handle an axios call failure. I wrote the function and the code:
async function createFunction (url, APIKEY, vendorType) {
return new Promise((resolve, reject) => {
const options = {
method: 'post',
maxBodyLength: Infinity,
url: `${url}/vendors/packages`,
headers: {
'x-api-key': `${APIKEY}`
},
timeout: 10000,
data: {
"contentType": vendorType,
}
};
axios.request(options)
.then(response =>{
if (response.status && response.status == 200) {
resolve(response);
} else {
reject(`createFunction Axios call failed with status ${response.status}: ${response.data}`)
}
}).catch(err => {
reject(`createFunction Axios call failed: ${err}`);
})
});
}
KbartFunctions.createPackage(host,APIKEY,vendortype)
.then((response) => {
console.log(response);
})
I am getting an error:
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "createFunction Axios call failed: AxiosError: Request failed with status code 400".] {
code: 'ERR_UNHANDLED_REJECTION'
}
I thought I handled the rejection correctly, but the code crashes anyway. Because the error reads "createFunction Axios call failed:" I know it's in the Catch segment and not the then segment (otherwise it would have said "createFunction Axios call failed with status").
What am I doing wrong?