/node_modules/q/error.prototype.js
JavaScript | 123 lines | 77 code | 19 blank | 27 comment | 23 complexity | 0daccb7c2c6ab3c84700521463599dfc MD5 | raw file
Possible License(s): Unlicense
- /*
- ////////////////////////////////////////////////////////////////////////////////
- ______ ____ __ __
- / ____/_____________ _____ / __ \_________ / /_____ / /___ ______ ___ _____
- / __/ / ___/ ___/ __ \/ ___/ / /_/ / ___/ __ \/ __/ __ \/ __/ / / / __ \/ _ \/ ___/
- / /___/ / / / / /_/ / / / ____/ / / /_/ / /_/ /_/ / /_/ /_/ / /_/ / __(__ )
- /_____/_/ /_/ \____/_/ /_/ /_/ \____/\__/\____/\__/\__, / .___/\___/____/
- /____/_/
-
- ////////////////////////////////////////////////////////////////////////////////
- */
-
- (function() {
-
- Error.prototype.matches = function(err) {
- if (err.isNumber()) return(this.number == err);
- if (err.isError()) return(this.number == err.number);
- };
-
- Error.prototype.toString = function() {
- //Returns a better string representation of an error object
- //TODO: branch logic for browser based error messages
- return '\n' +
- '\nERROR: ' + this.message +
- '\nSTACK: ' +
- this.stack;
- };
-
- Error.prototype.ignore = function() {
- //returns true for known "non-errors" used for workflow purposes
- if (this.description == "break" || this.exit == true) {
- return true;
- }
- };
-
- Error.prototype.debug = function(code) {
- //prompt user for debugger hook
- code = code || "[no code provided]";
- //BUGFIX: need to implement with callback handler, until then disabled
- if (false && confirm("An error has occurred:\n\n" + this.toString() + "\n\n" + code + "\n\nWould you like to debug?")) {
- //if the user clicks yes - hit debug statement
- debugger;
- return true;
- }
-
- return false;
- };
-
- Error.prototype.bubble = function() {
- if (this.bbl) {
- this.count--;
- if (this.count <= 0) {
- return true;
- }
- throw this;
- }
- };
-
- Error.prototype.handle = function() {
- //attempts to handle all errors
- //will return undefined if nothing further to do
- //will return a value if the error was intended to return a value (each iterators)
- //will rethrow the error if it was intended to be reported via the .report attribute
- if (this.ignore()) {
- return this.retval;
- }
-
- if (this.bubble()) {
- return this.retval;
- }
-
- if (this.debug()) return;
-
- if (this.die) {
- throw this; //Keep throwing until we die!
- }
-
- if (this.report != null || this.die) {
- delete this.report;
- throw this;
- }
- else {
- if (q.debug) {
- alert(this.toString());
- }
- }
- };
-
- Error.toss = function(m) {
- //alias for throwing a new exception
- throw new Error(m);
- };
-
- Error.prototype.toss = function(m) {
- //A gentler type of throw
- //Designed to throw a newly defined Error inside things like var definitions or array indicies
- this.report = true;
- this.extra = m;
- this.handle();
- };
-
- Error.prototype.pass = function(f) {
- //pass an error to the provided error handling function {f}
- if (f) {
- f(this);
- }
- };
-
- Object.prototype.pass =
- Object.prototype.toss = function() {
- //noop: this is designed to reduce complexity of the node framework's error handling architecture
- };
-
- Error.prototype.print = function() {
- return " ERROR: " + this.number + " - " + this.description;
- };
-
- Error.notImplemented = function() {
- //helper error for functions yet to be implemented
- Error.toss("Function Not Implemented: " + q.me().caller.id());
- };
-
- })();