This project is no longer maintained and has been archived. No further issues, PRs, or updates will be made.
An error handling service for AngularJS
- Unique handling of each HTTP status code
- Batch handling of HTTP status codes
- Prevents handled errors from bubbling up (no unexpected errors in dependencies -- like promises)
- Configurable flood control to prevent spamming of duplicate errors
AngularJS ~1.2 is required
npm install angular-errorz
var exampleApp = angular.module("exampleApp", ["ttErrorz"]);
Configure the service within your app module's run block:
exampleApp.run(["errorz", function (errorz) {
errorz.addHandler(/* see Examples */);
}]);
Add a handler for an HTTP status code
Param | Type | Details |
---|---|---|
status | Number | The HTTP status code to handle. |
handler | Function | The function the service should call when handling the specified HTTP status code. |
errorz
: for chaining.
Add a handler for multiple HTTP status codes
Name | Type | Details |
---|---|---|
statuses | Array | The HTTP status codes to handle. |
handler | Function | The function the service should call when handling the specified HTTP status codes. |
errorz
: for chaining.
Determines whether the service has been configured to handle the specified status
Param | Type | Details |
---|---|---|
status | Number | The HTTP status code to check. |
Boolean
: true when the service contains a handler for the specified status.
Gets the list of HTTP status codes the service has been configured to handle.
None
Array[String]
: an array of HTTP status codes.
Name | Type | Details |
---|---|---|
floodControl.threshold | Number | The number of milliseconds the service will wait before calling handlers for identical HTTP status codes. Setting this to undefined , null , or 0 will disable Flood Control. Defaults to 1000. |
Capture HTTP status code 500, and output the error to the browser's console.
errorz.addHandler(500, function (rejection) {
console.error("Please try again.", "Server Error 500");
});
Capture HTTP status codes: -1, 0, 500, using the same handler for all of them. Outputting the error to the browser's console.
errorz.addBatch([-1, 0, 500], function (rejection) {
console.error("Please try again.", "Server Error " + rejection.status);
});
You can do the same thing with multiple calls to addHandler
instead of addBatch
. Impractical, for this use case, but here it is for demonstrative purposes:
errorz.addHandler(-1, handler)
.addHandler(0, handler)
.addHandler(500, handler);
var handler = function (rejection) {
console.error("Please try again.", "Server Error " + rejection.status);
};
Similar to Example 2, but handle HTTP status code 404 conditionally within the same handler.
errorz.addBatch([-1, 0, 404, 500], function (rejection) {
rejection.status === 404 && console.warn(rejection.config.url, "404 Not Found")
|| console.error("Please try again.", "Server Error " + rejection.status);
});
Effectively the same end result as Example 3, just spread out.
errorz.addBatch([-1, 0, 500], function (rejection) {
console.error("Please try again.", "Server Error " + rejection.status);
});
Then, elsewhere/elsewhile...
errorz.addHandler(404, function(rejection) {
console.warn(rejection.config.url, "404 Not Found");
});
Same as Example 3, but notify the user with toastr instead of logging to the console. We also enable flood control with a threshold equal to toastr's timeout, to prevent spamming the user with toast when there is a flood of identical errors.
errorz.addBatch([-1, 0, 404, 500], function (rejection) {
rejection.status === 404 && toastr.warning(rejection.config.url, "404 Not Found")
|| toastr.error("Please try again.", "Server Error " + rejection.status);
}).floodControl.threshold = +toastr.options.timeOut;