zoukankan      html  css  js  c++  java
  • [NestJS] Fallback Exception Filter

    Fallback exception filter is mean to catch any exception which are not catched by other exception filters;

    import { Catch, ExceptionFilter, ArgumentsHost } from "@nestjs/common";
    
    @Catch()
    export class FallbackExpectionFilter implements ExceptionFilter {
        catch(exception: any, host: ArgumentsHost) {
            console.log('fallback exception handler triggered',
            JSON.stringify(exception));
    
            const ctx = host.switchToHttp();
            const response = ctx.getResponse();
    
            return response.status(500)
                .json({
                    statusCode: 500,
                    createdBy: "FallbackExceptionFilter",
                    errorMessage: exception.message ? exception.message: 'Unexpected error ocurred';
                });
        }
    }

    main.ts:

    import { NestFactory } from '@nestjs/core';
    
    import { AppModule } from './app.module';
    import { HttpExceptionFilter } from './filters/http-exception.filter';
    import { FallbackExpectionFilter } from './filters/fallback.filter';
    
    async function bootstrap() {
      const app = await NestFactory.create(AppModule);
    
      app.setGlobalPrefix('api');
      app.useGlobalFilters(
        new FallbackExpectionFilter(),
        new HttpExceptionFilter(),
      );
    
      await app.listen(9000);
    }
    
    bootstrap();

    Order matters, from most generic to specific.

  • 相关阅读:
    stm32型号与flash大小
    SVN适用教程
    sylixos初接触
    基于stm32实时操纵系统初接触
    freertos任务调度
    flash结构
    nand nor flash
    反汇编文件分析
    mm.与cache
    CISC(复杂指令集)和RISC(简单指令集),x86、ARM、MIPS架构
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12196529.html
Copyright © 2011-2022 走看看