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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xtreamsrl/nest-exception

v1.5.1

Published

A Nestjs global exception filter to handle exceptions and standardize error responses.

Downloads

746

Readme

@xtreamsrl/nest-exception

This package provides a NestJS global exception filter that uses a MessageSource component to translate an exception into a http error with a proper user-facing message.

The MessageSource component is a generic interface that can be implemented to provide a way to translate an exception into a user-facing message. The InMemoryMessageSource is a simple implementation that uses a map to translate the exception code into a message.

All exceptions will be logged and every error response will contain an incident id for troubleshooting purposes. An incident id is a unique identifier that can be used to trace the error in the logs. The incident id is generated by the IncidentIdSupplier component. The OpenTelemetryIncidentIdSupplier is an implementation that uses the OpenTelemetry trace id, if available.

You can also provide a custom ErrorContextSupplier to add additional context to the logged error. For example, if the underlying logger is the WinstonGCPLogger, the context will be added to the log entry as metadata.

Installation

npm install @xtreamsrl/nest-exception

Usage

Define your custom exception that extends the BaseException class and define your error messages map:

export class MyBaseException extends BaseException {
  constructor(code: MyErrorCodes, message?: string, context?: ExceptionContext) {
    super(code, message, context);
  }
}

enum MyErrorCodes {
  BAD_CONFIG = 'BAD_CONFIG'
}

// Error messages map
const errorMessages = {
  ERR_GENERIC: 'Generic error',
  BAD_CONFIG: 'bad configuration url',
} as const;

The enum is shared with the BaseException, so that the code can be used as key to return to the user an appropriate error message (which differ from the exception message).

At this point just create the exception filter and register it as the global one.

  const app = moduleRef.createNestApplication();
  const adapterHost = await app.get<HttpAdapterHost>(HttpAdapterHost);
  const messageSource = new InMemoryMessageSource<MyErrorCodes>(errorMessages);
  const logger = await app.resolve<LoggerServicePort>(LoggerServicePort);
  const exceptionFilter = new RestExceptionFilter(adapterHost, new OpenTelemetryIncidentIdSupplier(), messageSource, logger);
  app.useGlobalFilters(exceptionFilter);

Remember to use the error codes enum as generic param for the MessageSource component, otherwise the string type will be automatically inferred by Typescript.

Build

Run nx build nest-exception to build the library.

Run unit tests

Run nx test nest-exception to execute the unit tests via Jest.

Linting

Run nx lint nest-exception to execute the lint via ESLint.

Versioning

Export the GH_TOKEN environment variable with your GitHub token with at least the repo scope:

export GH_TOKEN=<YOUR_PERSONAL_GH_TOKEN>

Then run the following command:

lerna version

The GH_TOKEN is needed to push the version commit and tag to the remote repository and to create the release on GitHub.

For general information about the versioning process, please refer to the root Readme Versioning section.

Publishing

Update your local .npmrc file to include the following lines:

@xtreamsrl:registry=https://registry.npmjs.org/
//registry.npmjs.org/:_authToken=${NPM_TOKEN}

The ${NPM_TOKEN} placeholder is a npm personal access token publish permissions on the @xtreamsrl organization. It can be treated as placeholder to replace with the actual token value, or you can set it as an environment variable:

export NPM_TOKEN=<YOUR_PERSONAL_NPM_TOKEN>

Then run the following command:

npm run lerna-publish

Who we are