score:0
you are trying to catch the promise.reject and wrapping it in a try...catch
. only one of this will work.
you can either catch a promise rejection or wrap the promise in a try...catch
and throw a new error on promise rejection and catch it in catch block.
try this code
const url = five_day_forecast_url.replace("{0}", city);
axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
return promise.reject(error);
});
try {
const request = axios.get(`${url}`)
.then(
response => {
debugger;
})
.catch(
e => {
debugger;
throw e // throw the error and catch it
});
debugger;
return {
type: fetch_five_day_forecast,
payload: request
};
} catch {
debugger;
// now you can catch the error in catch block
console.log("error!");
}
// or you can't write async/await code
// make the the function is marked as `async`
try {
const response = await axios.get(`${url}`)
return {
type: fetch_five_day_forecast,
payload: response
};
} catch (e) {
console.error("error happened during fetch");
console.error(e);
}
score:3
when using try...catch
with axios
you explicitly have to state the error response like so
catch(error) {
console.log('[error]', error.response);
// use the error.response object for some logic here if you'd like
}
otherwise it just returns the string value.
with that response object you can then utilize some logic to do something based on the particular error. more info can be found here https://github.com/axios/axios/issues/960
i hope this helps.
Source: stackoverflow.com
Related Query
- reactjs try catch in render does not catch children errors
- Axios.catch does not catch network/console errors
- Why does React.js axios data show in console but not on the screen?
- React front end is not communicating with spring boot REST API. Axios network err in console
- Axios does not catch and send data to client side
- Redux Forms submit validation does not set errors messages
- Semantic-ui-react library does not work; no errors
- axios get params does not inherit the params in create
- Why does chrome dev tools console show a link for Post 500 error in one website but not another?
- Yarn Workspaces, workspace does not emit errors or warnings
- Request made with axios does not work but if it works with XMLHttpRequest
- Yup and useForm validation errors does not trigger when contact is form is empty
- OnClick is not working, but no errors appear in the console
- react Todo list erases in console but does not erase from screen
- Axios with firebase does not return the data while putting it
- Axios then, catch are not called
- Webpack hot module replacement lists updated React components in console but does not update them
- Cannot access data property of Axios response object after successful GET request: Property 'data' does not exist on type 'void'
- axios does not work with while fetch does
- I just used react context, why does static context not work if I console on other components
- Axios post does not send data
- React Query return undefined data while in network tab the data exists but it does not show on the page
- TypeScript errors in React Class Component property does not exist on type 'Readonly<{}>', not sure how to set types for state
- React + Axios - Data loaded able to console log but not showing up in DOM?
- React Native release apk does not fetch data from local network
- React axios post request does not send the data
- Starting a php session from react with an axios call does not keep the session alive, but doing it with postman works just fine
- My dynamic API path for Axios does not appear to be formed correctly
- React not importing file. Console shows no errors
- Data comes into console but does not show in application
More Query from same tag
- Match paths that do not have __magic__ folders
- Testing a function inside a component with jest and testing-library
- webpack build failed
- Adding a class to a certain element in ReactJS
- I'm getting an "Uncaught TypeError: Cannot read properties of undefined (reading 'push')" with react-router-dom v6
- Switch between divs when clicking prev/next buttons using React Hooks
- Alternative to html offsetTop
- How to add properties to child components using React.cloneElement?
- How do I insert an element or component in an array at a specific index?
- beforeunload works only on page refresh, not on page leave
- Update data from API fetch request with React Hooks?
- Parsing Data in action
- When to load an async relation with Flux
- Nested route under id
- React Component not returning simple console log properly
- In Redux, where does the state actually get stored?
- How to render the Images in my Card instance
- nextjs ie11 Expected identifier
- React component does no re-render when it is swapped with the same one
- Handling events on dynamically created <select> dropdown options
- persist a user id from redux state after page refreshes
- React in HTML, blank page
- Passing react props as object
- Redux + React how to change only one element value in array full of objects? I have working app, just want to make sure I do it right
- How to change state value when checkbox change using ReactJS
- Nested React Router : hide parent component on showing nested child component
- React's output not responding
- React dom , getElementByID
- Couldn't Update array in MongoDB with mongoose
- How to render a react component from html string in ReactJS while setting in dangerouslysetinnerHtml?