Axios.get() request Async/await catch block error is not defined #5873
Unanswered
airnichols13
asked this question in
Q&A
Replies: 3 comments
-
const axios = require('axios');
async function tryGet(config, endpoint) {
try {
const resp= await axios.get(endpoint, config);
return resp.data
} catch (error) {
console.log(error);
throw error;
}
}
tryGet({}, 'https://httpbin.org/status/500').then(console.log).catch(console.error); Unable to reproduce, can you try with above code as reference. |
Beta Was this translation helpful? Give feedback.
0 replies
-
That didn't work either, the error in the catch block is still not defined. I don't know if this has anything to do with it, but I'm creating an axios instance to use throughout my app:
|
Beta Was this translation helpful? Give feedback.
0 replies
-
You don't need either of those headers and |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Describe the bug
I am having an issue where if I try to use async/await to make an API request, the
catch
in the try/catch is not passed theerror
. If I switch to using the .then/.catch syntax, it does work.I've tried all sorts of combinations and nothing I have done gets the error to the catch block. All the documentation I've read makes it seem like as long as you're within an async function, and await'ing the api request, any exception should be caught in the catch, but I have not seen that work.
The server error is present in the interceptor, and the .catch syntax, but not the try/catch.
Code snippet
Expected behavior
The
error
is available in the catch block of the async function.Axios Version
1.3.4
Browser
Chrome
Browser Version
110.0.5481.178
Node.js Version
18.13.0
OS
Windows 11
Additional Library Versions
Beta Was this translation helpful? Give feedback.
All reactions