Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guideline on how to use with nestjs #42

Open
lekhasy opened this issue Aug 12, 2024 · 0 comments
Open

Add guideline on how to use with nestjs #42

lekhasy opened this issue Aug 12, 2024 · 0 comments

Comments

@lekhasy
Copy link

lekhasy commented Aug 12, 2024

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"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant