Link to home
Start Free TrialLog in
Avatar of Michael Sterling
Michael SterlingFlag for United States of America

asked on

In react, what is the correct syntax in a fetch call to catch a 404 error?

I'm having trouble with finding the correct syntax to catcha 404 error from my api call. Currently if there is a 404 it never appears to make it to the:

.catch(() => this.setState({ hasErrors: true }))

Open in new window


part of the call. I'm currently searching for an example but can't seem to find a simplified one.

fetch snippet

        fetch('http://localhost/MICROSERVICES/TestAPI1/api/Users/test/' + mid)
            .then(res => res.json())
            .then((res) => {
                setUserInfo({userInfo: res});
            })
            .catch(() => this.setState({ hasErrors: true }))

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Michael Sterling
Michael Sterling
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial