View as Json

API Error Handler

A lightweight, production-ready error handling utility for Express applications.

Installation Guide

npx servercn-cli@latest add error-handler

Usage Example

import { ApiError } from "../utils/api-error";
import { AsyncHandler } from "../utils/async-handler";
 
export const signinUser = AsyncHandler(async (req, res) => {
  const { email, password } = req.body;
 
  if (!email || !password) {
    throw ApiError.badRequest("Missing email or password");
  }
  // ...
});

Error Response

If email or password is missing, then return standardized responses:

{
  "success": false,
  "message": "Missing email or password",
  "statusCode": 400
}

File & Folder Structure

......

Installation

npx servercn-cli@latest add error-handler

Contributed by