A utility package for simplified error handling and management in Node.js applications.
npm install @dev-abhi/errorifybash
npm install @dev-abhi/errorify
`
---
Features
- Custom Error Class: Define your own errors with status codes, error codes, and additional details.
- Predefined Error Types: Quickly use predefined errors like ValidationError, DatabaseError, etc.
- Error Serialization: Convert errors to JSON for structured API responses.
- Environment-Based Stack Traces: Automatically include or exclude stack traces based on environment configuration.
- TypeScript Support: Built with TypeScript for strong typing and generics.
---
Usage
$3
You can define custom errors using the CustomError class.
`typescript
import { CustomError } from "@dev-abhi/errorify";
// Example: Creating a custom error
const error = new CustomError(
"Something went wrong", // message
400, // statusCode
"BAD_REQUEST", // errorCode
{ invalidField: "name" } // additional details (optional)
);
console.log(error.toJSON());
// Output:
// {
// message: "Something went wrong",
// statusCode: 400,
// errorCode: "BAD_REQUEST",
// details: { invalidField: "name" }
// }
`
---
$3
The package includes a set of predefined errors for common use cases, such as:
- Validation Errors
- Database Errors
- Authentication Errors
- File Errors
- Client-Side Errors
- Server-Side Errors
#### Example: Using Predefined Errors
`typescript
import { ValidationError, DatabaseError } from "@dev-abhi/errorify";
// Validation error
const validationError = new ValidationError(
"Invalid email address",
{ field: "email" } // additional details
);
console.log(validationError.toJSON());
// Database error
const dbError = new DatabaseError("Failed to connect to the database");
console.log(dbError.toJSON());
`
---
$3
Control whether stack traces are included in errors based on your environment. By default:
- In development, stack traces are included.
- In production, stack traces are excluded.
#### Setup:
Configure the environment once during application startup:
`typescript
import { setErrorConfig } from "@dev-abhi/errorify";
setErrorConfig({
includeStackTrace: process.env.NODE_ENV === "development", // true for dev, false for prod
});
`
Now, all errors created using CustomError or predefined errors will respect this setting.
---
$3
Easily handle errors in an Express app using the provided structure.
#### Example Middleware:
`typescript
import express, { Request, Response, NextFunction } from "express";
import { CustomError } from "@dev-abhi/errorify";
const app = express();
// Sample route
app.get("/", (req, res, next) => {
next(new CustomError("Resource not found", 404, "NOT_FOUND"));
});
// Error-handling middleware
app.use((err: CustomError, req: Request, res: Response, next: NextFunction) => {
if (err instanceof CustomError) {
return res.status(err.statusCode).json(err.toJSON());
}
res.status(500).json({ message: "Internal Server Error" });
});
app.listen(3000, () => console.log("Server running on port 3000"));
`
---
$3
You can extend CustomError to create your own error types.
#### Example:
`typescript
import { CustomError } from "@dev-abhi/errorify";
class PaymentError extends CustomError {
constructor(message = "Payment failed", details?: any) {
super(message, 402, "PAYMENT_ERROR", details);
}
}
// Usage
const paymentError = new PaymentError("Insufficient balance", {
userId: "12345",
});
console.log(paymentError.toJSON());
`
---
$3
The package uses generics to allow strongly-typed error details.
#### Example:
`typescript
import { CustomError } from "@dev-abhi/errorify";
interface UserDetails {
field: string;
issue: string;
}
const userError = new CustomError(
"Validation error",
400,
"VALIDATION_ERROR",
{ field: "username", issue: "too short" }
);
console.log(userError.details.field); // "username"
`
---
$3
#### CustomError Class
| Property | Type | Description |
| ------------ | ---------- | ------------------------------------------------------- |
| message | string | Error message. |
| statusCode | number | HTTP status code (default: 500). |
| errorCode | string | A unique error code (default: INTERNAL_SERVER_ERROR). |
| details | any | Additional details for the error (optional). |
| toJSON() | function | Serialize the error to JSON format. |
#### setErrorConfig Function
| Parameter | Type | Description |
| ------------------- | --------- | ------------------------------------------ |
| includeStackTrace | boolean | Whether to include stack traces in errors. |
---
Development
$3
1. Clone the repository:
`bash
git clone https://github.com/abhisek-kar/errorify.git
cd errorify
`
2. Install dependencies:
`bash
npm install
`
3. Build the project:
`bash
npm run build
`
4. Test the project:
`bash
npm test
``