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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@yilmazcik/common

v1.0.14

Published

This is an npm package that includes shared code used betweeen other services:

Readme

This is an npm package that includes shared code used betweeen other services:

Error Handling with Typescript

In microservices, every service can be made by different programming languages so each service might have a different response structures. Eventually all those responses will go to the same front end application, in my project to react application. Inside of react app, we have to encode the knowledge of how to work with these different kinds of error responses. React app is going to have to know all the different error responses. We need to make absolutely sure every single service is going to send back an identical looking structure.

In typescript we cannot add custom properties to the error object but we need to add some more custom properties to 'Error'. That means we need to subclass Error. We take the built-in Error object, we are going to subclass into some separate subclasses, like RequestValidationError or DatabaseConnectionError. When we create subclasses we can add in some additional custom properties to it that will further clarify the reason for the error.

Create abstract CustomError:

export abstract class CustomError extends Error {
  abstract statusCode: number;
  constructor(message: string) {
    super(message);
    // Only because we are extending a built in class
    Object.setPrototypeOf(this, CustomError.prototype);
  }
  //   this is not a method. it is a method signature. abstract class should have at least one method
  abstract serializeErrors(): { message: string; field?: string }[];
}

Abstract classes are mainly for inheritance where other classes may derive from them. We cannot create an instance of an abstract class. Abstract classes are implemented as regular classes in the JavaScript generated by the TypeScript compiler. The drawback of this approach is that it is the TypeScript compiler that prevents abstract classes from being instantiated, and this isn’t carried over into the JavaScript code, potentially allowing objects to be created from the abstract class. However, this approach does mean that the instanceof keyword can be used to narrow types. All other subclasses inside "/error" will inherit from CustomError.

Then we write our express middleware for error handling:

export const errorHandler = (
  err: Error,
  req: Request,
  res: Response,
  next: NextFunction
) => {
  // We categorized some errors under CustomERror
  if (err instanceof CustomError) {
    return res.status(err.statusCode).send({ errors: err.serializeErrors() });
  }
  // this is for debugging. In case I forgot some error other than CustomError
  console.error(err);
  res.status(400).send({ message: "Unexpected Error" });
};
  • express is aware of number of arguments that are going into a middleware. If we have a middleware function with 4 arguments express is going to assume that this is a function meant to handle errors. Wherever we throw an error inside our app, this middleware will catch them.

in services , after all route handlers we are going to use this middleware:

app.all("*", async (req, res, next) => {
  // next() is necessary for handling async errors. if we had only sync errors: throw new NotFoundError
  //   next(new NotFoundError());
  // express-async-errors will help this error to be resolved without next()

  throw new NotFoundError();
});

npm i express-async-errors

all we have to do import it: import "express-async-errors"

Events in microservices