Should I add a try catch
block to my componentDidMount
method when I’m just fetching data from an api endpoint and I’m not anticipating any expected errors. But if I don’t add the try catch
block then how am I supposed to handle unexpected errors?
I currently have it like this:
async componentDidMount() {
try {
const { data } = await Axios.get(apiEndpointl);
this.setState({ data });
} catch (ex) {
console.log("Logging the error", ex);
alert("An unexpected error ocurred.");
}
}
The unexpected error is going to be handle with Toast Notifications, but I want to know is if I should be doing it the way I did it, keep in mind that I’m not implementing any CRUD operations so there’s no need to create a method to handle unexpected errors globally.