1.0.21 • Published 5 months ago

@algoan/nestjs-http-exception-filter v1.0.21

Weekly downloads
171
License
ISC
Repository
github
Last release
5 months ago

NestJS Http Exception Filter

A simple NestJS Http Exception Filter logging the HTTP response and formatting errors returned by the API.

Installation

npm install --save @algoan/nestjs-http-exception-filter

Usage

Default usage

Use the http exception filter as a global filter (cf. refer to the last paragraph of this section for more details).

Example:

import { Module } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { HttpExceptionFilter } from '@algoan/nestjs-http-exception-filter';

/**
 * Core module: This module sets the http exception filter globally
 */
@Module({
  providers: [
    {
      provide: APP_FILTER,
      useClass: HttpExceptionFilter,
    },
  ],
})
export class CoreModule {}

Example 2:

import { NestFactory} from '@nestjs/core';
import {HttpExceptionFilter} from '@algoan/nestjs-http-exception-filter';
import { MainModule } from './main.module';

async function bootstrap() {
  const app = await NestFactory.create(MainModule,{
  });

  app.useGlobalFilters(new HttpExceptionFilter());
  await app.listen(3000);
  console.log(`App listening on port 3000`)
}
bootstrap();

Default response body

{
  "code": string,
  "message": string,
  "status": number
}

// Examples
// 400
{
  "code": "BAD_REQUEST",
  "message": "Bad Request",
  "status": 400
}
// 500
{
  "code": "INTERNAL_SERVER_ERROR",
  "message": "Internal Server Error",
  "status": 500
}
// 413
{
  code: 'PAYLOAD_TOO_LARGE',
  message: `
    Your request entity size is too big for the server to process it:
      - request size: 590001;
      - request limit: 102400.`,
  status: 413,
}

Default Logger messages

# Warning
[Nest] 96665   - 04/14/2020, 6:35:27 PM   [HttpExceptionFilter] Object:
{
  "message": "400 [GET /badrequest] has thrown an HTTP client error",
  "exceptionStack": "stackTrace",
  "headers": {
    "host": "localhost:3000",
    "user-agent": "insomnia/7.1.1",
    "accept": "*/*"
  }
}

# Error
[Nest] 96665   - 04/14/2020, 6:34:33 PM   [HttpExceptionFilter] Object:
{
  "message": "500 [GET /error] has thrown a critical error",
  "headers": {
    "host": "localhost:3000",
    "user-agent": "insomnia/7.1.1",
    "accept": "*/*"
  },
  "exceptionStack": "stackTrace",
}
1.0.21

5 months ago

1.0.20

6 months ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago