Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Latest commit

 

History

History
91 lines (71 loc) · 3.53 KB

README.md

File metadata and controls

91 lines (71 loc) · 3.53 KB

Exception.io: JavaScript NPM version

A collection of custom exception classes written in JavaScript.

Inheritance Hierarchy

Error
└── Exception
    ├── ArgumentException
    ├── InvalidInputException
    ├── NotImplementedException
    ├── NotSupportedException
    ├── NotFoundException
    ├── UnauthenticatedException
    └── UnauthorizedException

Classes

Class NameDescriptionDefault Message
ExceptionThe exception that is thrown when an error occurs.No Description
ArgumentExceptionThe exception that is thrown when one of the arguments provided to a method is not valid.Invalid Argument
InvalidInputExceptionThe exception that is thrown when one of the input provided to a method is not valid.Invalid Input
NotImplementedExceptionThe exception that is thrown when a requested method or operation is not implemented.Not Implemented
NotSupportedExceptionThe exception that is thrown when a requested method or operation is not supported.Not Supported
NotFoundExceptionThe exception that is thrown when an attempt is made to find something that does not exist.Not Found
UnauthenticatedExceptionThe exception that is thrown when a requested method or operation requires authentication.Not Authenticated
UnauthorizedExceptionThe exception that is thrown when the current user is not allowed to perform an attempted operation.Not Authorized

Usage

Initiate any one of the exception classes.

if (item == null) {
    throw new NotFoundException("Item not found.");
}

Check the type of exception we are dealing with:

var err = new Exception();

err instanceof Exception               // → true
err instanceof Error                   // → true
Exception.prototype.isPrototypeOf(err) // → true
Error.prototype.isPrototypeOf(err)     // → true
err.constructor.name                   // → "Exception"
err.name                               // → "Exception"
err.message                            // → "No description"
err.toString()                         // → "[Exception] No description"
err.stack                              // → prints the stack trace

Handle specific exception types using exception's constructor property:

try {
  foo("valA", "valB");
}
catch (e) {
  if (e instanceof ArgumentException) {
    console.error(e.toString());
  }
  else if (e instanceof InvalidInputException) {
    console.error(e.toString());
  }
  else {
    console.error(e.toString());
  }
}

Supported Platforms

Creator

License

Exception.io is released under the MIT license. See LICENSE for details.