PDMLab/http-problem-details

Add guideline on how to use with nestjs

lekhasy opened this issue · 0 comments

Nestjs is famous among nodejs API framework, it would be helpful to have a short section describe how to use this with nestjs.
I tried to copy the code provided in readme but it does not work, I think other people might have the same experience.

FYI, this is what i have done:

  • Follow Nestjs guide to create a new nestjs app.

  • Follow readme.md in this repo to create all necessary files

  • Throw an error in app.controller.ts:
    throw new NotFoundError({type: 'customer', id: '123' })

  • add HttpProblemResponse to main.ts:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { HttpProblemResponse } from 'express-http-problem-details';
import { NotFoundErrorMapper } from './ErrorMapper';
import { DefaultMappingStrategy, MapperRegistry } from 'http-problem-details-mapper'

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const strategy = new DefaultMappingStrategy(
    new MapperRegistry()
      .registerMapper(new NotFoundErrorMapper()))
  app.use(HttpProblemResponse({ strategy }))
  await app.listen(3000);
}
bootstrap();

Expected result:

{
    "status": 404,
    "title": "customer with id 123 could not be found.",
    "type": "http://tempuri.org/NotFoundError"
}

Actual Result:

{
    "statusCode": 500,
    "message": "Internal server error"
}