Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@algoan/http-exception-filter

Package Overview
Dependencies
Maintainers
2
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@algoan/http-exception-filter

A simple http exception filter for NestJS applications

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
increased by100%
Maintainers
2
Weekly downloads
 
Created
Source

Nest Logo

A Nest http exception filter.

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/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/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/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",
  "exception": {
    "response": {
      "statusCode": 400,
      "message": "Bad Request"
    },
    "status": 400,
    "message": "Bad Request"
  },
  "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": "*/*"
  },
  "exception": {
    "response": {
      "statusCode": 500,
      "message": "Internal Server Error"
    },
    "status": 500,
    "message": "Internal Server Error"
  }
}

Keywords

FAQs

Package last updated on 15 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc