Skip to content

Instantly share code, notes, and snippets.

@rrgarciach
Created August 3, 2016 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rrgarciach/b8315f20126aa7539556d1bc5aa96f20 to your computer and use it in GitHub Desktop.
Save rrgarciach/b8315f20126aa7539556d1bc5aa96f20 to your computer and use it in GitHub Desktop.
Error Handler Service
// api service calling example
apiService.get()
.then(response => {
return response.data;
})
.catch(err => {
return errorHandler(err);
});
/// handler service pseudo code
function errorHandler() {
var _err = {};
function handler(err) {
switch (err.status) {
case '401':
label = 'sorry! but you are unauthorized!'
_err = {type: constant.WARNING, message: err.message}
case '404':
_err = {type: constant.WARNING, message: err.message}
break;
default:
displayFatalDialog()
}
}
function dialog(err) {
handler(err)
mdDialog(label)
}
function toast(err) {
handler(err)
showToast(label)
}
function getLabel(err) {
handler(err)
return label;
}
}
const error ={
ERROR:{color, size}
}
// my Ctrl
getSomething()
.then(something => {
// something
})
.catch(err => {
// dialog
errorServiceHandler.dialog(err);
});
login()
.then(token => {
// token
})
.catch(err => {
// dont want a dialog
$scope.error = errorServiceHandler.getLabel(err);
});
<div>{{error}}<div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment