npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

express-no-try-catch

v1.0.1

Published

A simple Express middleware to safely handle async errors without try/catch everywhere.

Downloads

14

Readme

express-no-try-catch

A simple Express middleware to safely handle async errors without try/catch everywhere.

What This Package Does

This package solves a common problem in Express.js applications: the need to wrap every async route in try/catch blocks to prevent server crashes from unhandled promise rejections.

How It's Different From Existing Packages

Most existing packages either:

  • Require global overrides that affect all Express behavior
  • Have complex APIs with many options
  • Include unnecessary dependencies
  • Don't provide consistent error formatting

This package is different because:

  • Simple API: Just wrap your async routes with wrapAsync()
  • Zero Dependencies: Lightweight with no external packages
  • Consistent Errors: Standard JSON error responses
  • No Global Changes: Only affects routes you explicitly wrap
  • Built-in Trace IDs: Automatic request tracking

Installation

npm install express-no-try-catch

Quick Start

const express = require("express");
const { wrapAsync, errorHandler } = require("express-no-try-catch");

const app = express();

// Wrap your async routes with wrapAsync
app.get("/user/:id", wrapAsync(async (req, res) => {
  const user = await getUserFromDB(req.params.id);
  if (!user) {
    const error = new Error("User not found");
    error.status = 404;
    throw error;
  }
  res.json(user);
}));

// Add error handler at the end
app.use(errorHandler);

app.listen(3000);

How It Works

The Problem

Without this package, you need to wrap every async route in try/catch:

app.get("/user/:id", async (req, res, next) => {
  try {
    const user = await getUserFromDB(req.params.id);
    res.json(user);
  } catch (err) {
    next(err); // Easy to forget!
  }
});

The Solution

With express-no-try-catch, errors are caught automatically:

app.get("/user/:id", wrapAsync(async (req, res) => {
  const user = await getUserFromDB(req.params.id);
  res.json(user);
  // No try/catch needed!
}));

API

wrapAsync(fn)

Wraps an async route handler to automatically catch errors.

const handler = wrapAsync(async (req, res) => {
  // Your async code here
  const result = await someAsyncOperation();
  res.json(result);
});

errorHandler(err, req, res, next)

Global error handler that formats errors as JSON.

app.use(errorHandler);

Error responses are formatted as:

{
  "success": false,
  "message": "Error message",
  "traceId": "request-id"
}

Features

  • Automatic error catching for async routes
  • No try/catch boilerplate needed
  • Consistent JSON error responses
  • Built-in trace ID support
  • Zero dependencies
  • Lightweight (0.5KB)

Examples

Database Operations

app.get("/users/:id", wrapAsync(async (req, res) => {
  const user = await User.findById(req.params.id);
  if (!user) {
    const error = new Error("User not found");
    error.status = 404;
    throw error;
  }
  res.json(user);
}));

API Calls

app.post("/webhook", wrapAsync(async (req, res) => {
  const result = await externalAPI.process(req.body);
  res.json(result);
}));

Custom Status Codes

app.post("/data", wrapAsync(async (req, res) => {
  if (!req.body.name) {
    const error = new Error("Name is required");
    error.status = 400;
    throw error;
  }
  res.json({ success: true });
}));

License

MIT

Author

Maheshwari KM