Skip to content

Instantly share code, notes, and snippets.

@earnubs

earnubs/trycatch.js

Created May 20, 2019
Embed
What would you like to do?
const callApi = () => {
const response = fetch(...);
// do stuff with response
return response;
}
try {
const response = callApi();
next(completeAction(response));
}
catch (e) {
captureException(error);
next(completeAction(error));
}
//
// Versus
//
const callApi = () => {
// a specfic try/catch for the fetch
try {
return fetch(...);
}
// do stuff with response
catch (error) {
captureException(error); // get a precise stacktrace
throw error;
}
}
try {
const response = callApi();
next(completeAction(response));
}
catch (error) {
captureException(error);
next(completeAction(new Error('CALL_API Error')));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment