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.

  • 相关阅读:
    vue(5)
    vue(4)
    vue(3)-防止闪烁
    vue(2)
    bootstrap 模态框
    2017年上半年工作总结和计划
    vue(1)
    文本框数量加减功能
    meta 控制移动端页面的缩放
    题解 P1550 【[USACO08OCT]打井Watering Hole】
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12196529.html
Copyright © 2011-2022 走看看