score:2
i think that you can handle if the response is success but it doesn't has data on i, or if the request just got an error the catch statement can handle it :
getdata()
.then(response =>{
if(!response.data) {
dispatch({ type: 'some_action_for_the_error', payload: response.error });
}
else {
transformdata(response)
}
).catch(( error ) =>{
console.error(error);
dispatch({ type: 'some_action_for_the_error', payload: error });
}
score:0
the .then() function can take two arguments. the first is a success callback, which you are using. the second is a failure callback, which is what you want.
getdata().then(
success => transformdata(success),
failure => dosomething(failure)
);
score:0
first you have to understand that how promise works. then() method has two functions. first is success and second is failure. failure can be written with .catch in es6. now first understand that catch only gets called for network failure, 500 error, 404 not found error. in that case there is no data to process.
but what you would want is 200 ok but if data is missing. you have put code in success itself :
getdata().then(response => {
const res = transformdata(response);
if(!res.data) {
// do your stuff
}
return res; // else normal behaviour
});
Source: stackoverflow.com
Related Query
- Handling error in promise by checking the response
- Unable to catch and log the Error response from an axios request
- handling error response from express to react
- How to get the response data from promise in React?
- Yield inside .then function of promise response throws error
- What is the best way to do Error Handling in React Native App.
- How can i use my custom http error msg that are in the response body in react-admin
- How to centralize the error handling for API calls in react (with Axios)
- How catch error in the front-end from response of expressjs?
- Access the error response without modifying the Amplify library
- How to catch HTTP response error and then pass to the caller
- Axios error handling the err.response object
- How to show success or error message based on the response of the api in React js
- Proper Promise handling success and error callbacks
- Handling 404 Error Response with Async/Await
- Object is possibly 'null'.ts(2531), Error occurs even though I am checking that in the if condition
- why is typescript returning this compile error even though the code is checking for undefined?
- Which is more preferable? Dispatch the response of a promise or dispatch an action stating a promise needs to be triggered?
- Next.JS - Error when Checking if window == undefined : Hydration failed because the initial UI does not match what was rendered on the server
- Handling the Post request's Response in React JS
- React Error Handling - why does the app crash?
- Extracting the common code of handling response from callback function
- In Axios, how do I capture the response error body that the network tab reports?
- i`m getting the 400 response error in react.js i know the backendd is ok but the client side i am not sure i am not fimiliar with react
- Getting error error rejection promise of the file tried to fix it but its not working
- The code in the then method is not executed and error handling is performed
- Handling React Context in App.js Component - error Hooks can only be called inside the body of a function
- How to resolve the error from a promise in reactjs
- Webpack 4 Babel and React error in handling the JSX files
- Fetched API Data not recognized after handling the promise with a Middleware
More Query from same tag
- Cypress Assertion .should() fails
- how to get data from localhost:9200 (elasticsearch) using axios in react
- How to hide Side Panel and the add data popup when the map is created?
- How can I take the previous 5 values I am passing through search and then using it in API. I want to display pervious five values
- How to return true or false using the input checkbox in react js?
- Have scrollable div fill up the above of BottomNavigation bar?
- "No exported member" error when trying to add & { session: Express.Session } to type MyContext when following React, Express and Typescript tutorial
- How to apply styling to MapView when using @arcgis/core for imports?
- Conditional (ternary) operator
- Warning: Each child in a list should have a unique "key" prop. mapStateToProps
- Using Dygraph in React with redux?
- Material-UI ClickAwayListener fires immediately
- Can't display an image in react from backend django
- React native flatlist, not updating correctly when deleting element from state
- Jest encountered an unexpected token in typescript
- Redirect Login React
- Apply framer-motion to non-functional components
- How can I stop the execution of my promise based on the boolean value of my prop?
- Next JS Images after next build gives 404 images
- React js react-responsive-carousel not loading with webpack
- Styled-JSX variables not working
- How to change the spellcheck language in Quilljs (React)
- How to make canvas drawing always center
- ReactCSSTransitionGroup doesn't add classes
- ReactJS Bootstrap Collapse Individual Card
- Cannot close an antd modal using reactJS library
- Why is each MenuItem in my react-contextmenu list identical?
- TextField default value from parent not rendering on child
- Can create-react-app be used with TypeScript
- Download Files from Url using React