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

@nestling/errors

v1.5.1

Published

Error Handling for NestJS

Downloads

3

Readme

Nestling errors module

This module tries to make it easier to handle errors and send back meaningful information to the client.

It consist of a way to define your own messages for common errors and also intercept third party errors and normalize them into a common response.

Application level error messages

As a convention you can suffix your files with .errors.ts and keep the errors close to where they are used.

users.errors.ts

import { HttpStatus } from '@nestjs/common'
import { IErrorMessages } from '../errors/ErrorMessage'

// Optional: to typecheck interpolation vars
export interface UserEmailAlreadyExistError {
  email: string
}

export const userErrors: IErrorMessages = {
  type: 'user',
  errors: [
    {
      code: 'credentialsInvalid',
      statusCode: HttpStatus.BAD_REQUEST,
      message: 'Invalid Credentials.'
    },
    {
      code: 'emailAlreadyExist',
      statusCode: HttpStatus.BAD_REQUEST,
      message: 'Email {email} already exist.'
    }
    ...etc
  ]
}

In order to use the error messages they must be registered first:

ErrorMessage.addErrorMessages(userErrors)

The error messages can be used like this:

import { ErrorMessage } from '@nestling/errors'

throw new ErrorMessage('user:credentialsInvalid')

throw new ErrorMessage<UserEmailAlreadyExist>('user:emailAlreadyExists', {
  email: '[email protected]'
})

Setup

In able to filter for errors the exception filter must be added to the app's components:

import { HttpExceptionFilter } from '@nestling/errors'

@Module({
  components: [
    HttpExceptionFilter
  ]
})
export class ApplicationModule implements NestModule {
  configure (consumer: MiddlewaresConsumer): void {
  }
}

The filter will intercept all exceptions thrown, if an unknown exception is encountered a 500 error will be returned.

Adding extra error handlers

To register a new error handler use:

import ValidationError = require('ajv')
import { HttpStatus } from '@nestjs/common'
import { IErrorMessage } from '../../ErrorMessage'

export const validationErrorHandler = {
  type: ValidationError,
  handler: (exception): IErrorMessage => {
    return {
      code: `validation:${exception.errors[0].keyword}`,
      message: exception.errors[0].message,
      statusCode: HttpStatus.BAD_REQUEST
    }
  }
}

HttpExceptionFilter.addExceptionHandler(validationErrorHandler)

The type will be used to do an instance of check, the handler normalizes the error message into a common format.

Error Response Handler

To override the default response handler use:

HttpExceptionFilter.setErrorResponseHandler((error, response) => {
  response.setHeader(`x-error-code`, error.code)
  response.setHeader(`x-error-message`, error.message)
  response.setHeader(`x-error-statusCode`, error.statusCode)
  response
    .status(error.statusCode)
    .json(error)

  console.error(error)
})