@nivinjoseph/n-exception
v2.0.2
Published
Library of Exception classes
Downloads
4,753
Readme
n-exception
A TypeScript library providing a comprehensive set of exception classes for robust error handling in applications.
Overview
n-exception is a TypeScript library that extends the standard JavaScript Error class to provide a more structured and type-safe way of handling exceptions in your applications. It includes a base Exception class and several specialized exception types for common error scenarios.
Installation
npm install @nivinjoseph/n-exception
# or
yarn add @nivinjoseph/n-exceptionFeatures
- Base
Exceptionclass extending JavaScript'sError - Support for inner exceptions (error chaining)
- Type-safe exception handling
- Comprehensive stack trace support
- Common exception types for various scenarios
Available Exception Types
Exception- Base exception classApplicationException- For application-level errorsArgumentException- For invalid argument errorsArgumentNullException- For null argument errorsInvalidArgumentException- For invalid argument valuesInvalidOperationException- For invalid operation attemptsNotImplementedException- For unimplemented functionalityObjectDisposedException- For attempts to use disposed objects
Usage
Basic Usage
import { Exception, ArgumentNullException } from "@nivinjoseph/n-exception";
// Creating a basic exception
const ex = new Exception("Something went wrong");
// Creating an exception with an inner exception
const innerEx = new Error("Original error");
const exWithInner = new Exception("Operation failed", innerEx);
// Using specialized exceptions
function processUser(user: any) {
if (!user) {
throw new ArgumentNullException("user");
}
// ... rest of the function
}Error Chaining
try {
// Some operation that might fail
} catch (error) {
throw new ApplicationException("Failed to process data", error);
}Custom Exceptions
You can create your own exception types by extending the base Exception class:
class CustomException extends Exception {
constructor(message: string, innerException?: Error) {
super(message, innerException);
}
}API Reference
Exception Class
The base Exception class provides the following properties and methods:
name: The name of the exception typemessage: The error messageinnerException: The inner exception (if any)stack: The stack trace
License
This project is licensed under the MIT License - see the LICENSE file for details.
