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

@sjfrhafe/nest-problem-details

v1.2.1

Published

Provides rfc9457 logic for NestJS

Downloads

1,311

Readme

Nest Problem Details

Overview

This library provides tools to easily use the Problem Details standard for HTTP APIs as specified in rfc9457 in NestJS. It provides support for both Express and Fastify.

Functionalties

  • Adjustable ProblemDetailsException
  • Converting every thrown error in custom detail format
  • Automatically handling HttpExceptions from @nestjs/common like BadRequestException
  • Validation Pipe support
  • Autogenerating type URL
  • Extensible type URL logic
  • Overriding content-type header
  • Express and Fastify support
  • Custom Logger Support

Getting started

The heart of the library is the exception filter. It's added to bootstrap method in main.ts as a global exception filter.

//...
async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.useGlobalFilters(new ProblemDetailFilter());

  await app.listen(3000);
}
//...

Each error thrown is now returned in problem details formatting.

ProblemDetailException (auto type and instance)

throw new ProblemDetailException(403, {
  title: 'Forbidden',
  detail: 'You are not allowed to access this resource',
});

results in

{
  "status": 403,
  "type": "https://httpstatuses.com/403",
  "title": "Forbidden",
  "detail": "You are not allowed to access this resource",
  "instance": "cat/sam"
}

ProblemDetailException (additional custom args)

throw new ProblemDetailException(401, {
  type: 'https://example.com/errors/auth',
  title: 'JWT Expired',
  detail: `The provided JWT expired ${expired} minutes ago`,
  instance: 'profile/me',
  customHint: 'try to refresh you token',
});

results in

{
  "status": 401,
  "type": "https://example.com/errors/auth",
  "title": "JWT Expired",
  "detail": "The provided JWT expired 4 minutes ago",
  "instance": "profile/me",
  "customHint": "try to refresh your token"
}

Validation Pipe BadRequestExceptions

test.dto.ts

import { IsString } from 'class-validator';

export class TestDto {
  @IsString()
  name: string;
}

test.controller.ts

@Post()
  postHello(@Body() dto: TestDto): string {
    //do something with dto
  }

results in

{
  "status": 400,
  "type": "https://httpstatuses.com/400",
  "title": "Bad Request",
  "detail": "name must be a string",
  "instance": "/"
}

NestJS HttpExceptions

new BadRequestException('some error');

results in

{
  "status": 400,
  "type": "https://httpstatuses.com/400",
  "title": "Bad Request",
  "detail": "some error",
  "instance": "/"
}

Raw Errors

new Error('some error');

results in

{
  "status": 500,
  "type": "https://httpstatuses.com/500",
  "title": "Internal Server Error",
  "detail": "some error",
  "instance": "/"
}

Customize auto type generation

You can customize the auto generated type url by providing a ProblemDetailTypeUrlResolver.

const typeUrlResolver = (code: number, title: string) =>
  `https://example.com/errors/${code}`;

app.useGlobalFilters(new TestProblemDetailFilter(typeUrlResolver));

Disclaimer

This open-source library is provided as-is, without any express or implied warranties. The author(s) of this library shall not be held responsible for any direct, indirect, incidental, special, exemplary, or consequential damages arising from the use of this library, including but not limited to, procurement of substitute goods or services, loss of use, data, or profits, or business interruption. Users are encouraged to thoroughly test the library in their own environments and use it at their own risk. Additionally, it is recommended to review the license and terms of use associated with this library before incorporating it into any projects.