file module import { ArgumentsHost , Catch , ExceptionFilter , HttpException , HttpStatus } from '@nestjs/common' ; import * as lodash from 'lodash' ; import { isDevMode } from '../app.environment' ; import { EHttpStatus , TExceptionOption , THttpErrorResponse , TMessage } from '../common/types/interfaces/http.interface' ; class classdesc @ Catch ( ) export class HttpExceptionFilter implements ExceptionFilter { catch ( exception , host ) { const request = host . switchToHttp ( ) . getRequest ( ) ; const response = host . switchToHttp ( ) . getResponse ( ) ; const status = exception . getStatus ( ) || HttpStatus . INTERNAL_SERVER_ERROR ; const errorOption = exception . message as TExceptionOption ; const isString = ( value ) : value is TMessage => lodash . isString ( value ) ; const errMessage = isString ( errorOption ) ? errorOption : errorOption . message ; const errorInfo = isString ( errorOption ) ? null : errorOption . error ; const parentErrorInfo = errorInfo ? String ( errorInfo ) : null ; const isChildrenError = errorInfo . status && errorInfo . message ; const resultError = isChildrenError ? errorInfo . message : parentErrorInfo ; const resultStatus = isChildrenError ? errorInfo . status : status ; const data = { status : EHttpStatus . Error , message : errMessage , error : resultError , debug : isDevMode ? exception . stack : undefined , } ; if ( status === HttpStatus . NOT_FOUND && data . error === 'Not Found' ) { data . error = `资源不存在` ; data . message = `接口 ${ request . method } -> ${ request . url } 无效` ; } return response . status ( resultStatus ) . jsonp ( data ) ; } }