Created
August 3, 2016 21:00
-
-
Save rrgarciach/b8315f20126aa7539556d1bc5aa96f20 to your computer and use it in GitHub Desktop.
Error Handler Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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