Skip to content

Instantly share code, notes, and snippets.

@Rendez
Created July 14, 2011 14:33
Show Gist options
  • Save Rendez/1082569 to your computer and use it in GitHub Desktop.
Save Rendez/1082569 to your computer and use it in GitHub Desktop.
Extending JS Error object
/**
* Exception thrown if an error which can only be found on runtime occurs.
*/
var RuntimeError = function(message) {
Error.call(this);
this.type = "RuntimeError";
this.message = message || this.type;
};
/**
* Exception that represents error in the program logic.
* This kind of exceptions should directly lead to a fix in your code.
*/
var LogicError = function(message) {
Error.call(this);
this.type = "LogicError";
this.message = message || this.type;
};
Sys.inherits(RuntimeError, Error);
Sys.inherits(LogicError, Error);
/**
* Native Error types list:
* - EvalError: An error in the eval() function has occurred
* - RangeError: Out of range number value has occurred
* - ReferenceError: An illegal reference has occurred
* - SyntaxError: A syntax error within code inside the eval() function has occurred.
* All other syntax errors are not caught by try/catch/finally, and will
* trigger the default engine error message associated with the error.
* To catch actual syntax errors, you may use the onerror event.
* - TypeError: An error in the expected variable type has occurred
* - URIError: An error when encoding or decoding the URI has occurred (ie: when calling encodeURI()).
*/
var TypeError = function(message) {
LogicError.call(this, message);
this.name = "TypeError";
};
/**
* Extending Error for our own custom error types
*/
var InvalidArgumentError = function(message) {
TypeError.call(this, message);
this.name = "InvalidArgumentError";
};
var IOError = function(message) {
RuntimeError.call(this, message);
this.name = "IOError";
};
var BoundsError = function(message) {
RuntimeError.call(this, message);
this.name = "BoundsError";
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment