joshersil-custom-errors
v1.0.3
Published
Throw custom errors in JavaScript
Downloads
9
Maintainers
Readme
joshersil-custom-errors
A simple JavaScript library for throwing custom errors with additional data.
Installation
npm install joshersil-custom-errorsUsage
import { ErrorPersonalizado } from "joshersil-custom-errors";
// Basic usage
throw new ErrorPersonalizado("Something went wrong", "ValidationError");
// With additional data
throw new ErrorPersonalizado("Invalid user data", "ValidationError", {
field: "email",
value: "invalid-email",
});
// Catching and handling
try {
throw new ErrorPersonalizado("Test error", "TestError", { code: 400 });
} catch (error) {
console.log(error.name); // 'TestError'
console.log(error.message); // 'Test error'
console.log(error.datosExtra); // { code: 400 }
}API
ErrorPersonalizado
Custom error class that extends the native JavaScript Error.
Constructor
new ErrorPersonalizado(mensaje, tipo, (datosExtra = null));mensaje(string): The error messagetipo(string): The error type/namedatosExtra(any, optional): Additional data to attach to the error
Properties
name: The error typemessage: The error messagedatosExtra: Additional data attached to the errorstack: Error stack trace (inherited from Error)
License
ISC
