/
error.interceptor.ts
executable file
路31 lines (29 loc) 路 1.04 KB
/
error.interceptor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* @file Error interceptor
* @module interceptor/error
* @author Surmon <https://github.com/surmon-china>
*/
import { Observable, throwError } from 'rxjs'
import { catchError } from 'rxjs/operators'
import { Injectable, NestInterceptor, CallHandler, ExecutionContext } from '@nestjs/common'
import { getResponserOptions } from '@app/decorators/responser.decorator'
import { CustomError } from '@app/errors/custom.error'
import * as TEXT from '@app/constants/text.constant'
/**
* @class ErrorInterceptor
* @classdesc catch error when controller Promise rejected
*/
@Injectable()
export class ErrorInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> {
const target = context.getHandler()
const { errorCode, errorMessage } = getResponserOptions(target)
return next.handle().pipe(
catchError((error) => {
return throwError(
() => new CustomError({ message: errorMessage || TEXT.HTTP_DEFAULT_ERROR_TEXT, error }, errorCode)
)
})
)
}
}