This library provides common exception filters for NestJS applications, including the AxiosExceptionFilter, AllExceptionFilter, and HttpExceptionFilter.
To install this library, use npm:
npm i @mia-platform-internal/taringa-exception-filters-library
To use the exception filters in your NestJS application, follow these steps:
- Import the exception filters from the library and APP_FILTER from @nestjs/core in your app.module.ts file and it as a provider as shown below
import { Module } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { HttpExceptionFilter, AllExceptionFilter, AxiosExceptionFilter } from '@mia-platform-internal/taringa-exception-filters-library';
@Module({
providers: [
{
provide: APP_FILTER,
useClass: AxiosExceptionFilter,
},
{
provide: APP_FILTER,
useClass: AllExceptionFilter,
},
{
provide: APP_FILTER,
useClass: HttpExceptionFilter,
},
],
})
export class AppModule {}
- Now your application is using the provided exception filters.
import { Controller, Get, UseFilters } from '@nestjs/common';
import axios from 'axios';
@Controller()
export class AppController {
@Get()
@UseFilters(HttpExceptionFilter, AxiosExceptionFilter, AllExceptionFilter)
async getData(): Promise<string> {
try {
const response = await axios.get('https://example.com/example');
return response.data;
} catch (error) {
throw error;
}
}
}
In this example, we are making a GET request to an external API using Axios. If the request fails, the AxiosExceptionFilter will catch the error and throw the defined response
import { Controller, Get, UseFilters } from '@nestjs/common';
import axios from 'axios';
@Controller()
export class AppController {
@Get()
@UseFilters(HttpExceptionFilter, AxiosExceptionFilter, AllExceptionFilter)
getData(): string {
throw new Error('Something went wrong');
}
}
In this example, we throw an error with the string 'Something went wrong' but the AllExceptionFilter with intercept it throwing an 'Internal server error' message with 500 status code