File tree Expand file tree Collapse file tree 4 files changed +48
-5
lines changed
common/src/response-messages Expand file tree Collapse file tree 4 files changed +48
-5
lines changed Original file line number Diff line number Diff line change @@ -24,16 +24,21 @@ export class CustomExceptionFilter extends BaseExceptionFilter {
24
24
}
25
25
if ( exception instanceof HttpException ) {
26
26
status = exception . getStatus ( ) ;
27
+
27
28
}
28
29
29
- let exceptionResponse : ExceptionResponse ;
30
+ let exceptionResponse : ExceptionResponse = { } as ExceptionResponse ;
31
+ const exceptionResponseData = exception . getResponse ? exception . getResponse ( ) : exception ;
30
32
31
- if ( exception [ 'response' ] ) {
32
- exceptionResponse = exception [ 'response' ] ;
33
+ if ( 'string' === typeof exceptionResponseData ) {
34
+ exceptionResponse . message = exceptionResponseData ;
33
35
} else {
34
- exceptionResponse = exception as unknown as ExceptionResponse ;
36
+ exceptionResponse = exceptionResponseData as unknown as ExceptionResponse ;
35
37
}
36
38
39
+ if ( exceptionResponse . message && exceptionResponse . message . includes ( ResponseMessages . nats . error . noSubscribers ) ) {
40
+ exceptionResponse . message = ResponseMessages . nats . error . noSubscribers ;
41
+ }
37
42
errorResponse = {
38
43
statusCode : exceptionResponse . statusCode ? exceptionResponse . statusCode : status ,
39
44
message : exceptionResponse . message
@@ -43,7 +48,6 @@ export class CustomExceptionFilter extends BaseExceptionFilter {
43
48
? exceptionResponse . error
44
49
: ResponseMessages . errorMessages . serverError
45
50
} ;
46
-
47
51
response . status ( errorResponse . statusCode ) . json ( errorResponse ) ;
48
52
}
49
53
}
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import { getNatsOptions } from '@credebl/common/nats.config';
13
13
import helmet from 'helmet' ;
14
14
import { CommonConstants } from '@credebl/common/common.constant' ;
15
15
import NestjsLoggerServiceAdapter from '@credebl/logger/nestjsLoggerServiceAdapter' ;
16
+ import { NatsInterceptor } from '../../../libs/interceptors/nats.interceptor' ;
16
17
dotenv . config ( ) ;
17
18
18
19
async function bootstrap ( ) : Promise < void > {
@@ -95,6 +96,7 @@ async function bootstrap(): Promise<void> {
95
96
xssFilter : true
96
97
} )
97
98
) ;
99
+ app . useGlobalInterceptors ( new NatsInterceptor ( ) ) ;
98
100
await app . listen ( process . env . API_GATEWAY_PORT , `${ process . env . API_GATEWAY_HOST } ` ) ;
99
101
Logger . log ( `API Gateway is listening on port ${ process . env . API_GATEWAY_PORT } ` ) ;
100
102
}
Original file line number Diff line number Diff line change @@ -475,5 +475,14 @@ export const ResponseMessages = {
475
475
notFoundBaseWallet : 'The base wallet record is missing.' ,
476
476
walletRecordNotFound : 'Wallet record not found.'
477
477
}
478
+ } ,
479
+ nats : {
480
+ success : {
481
+
482
+ } ,
483
+ error : {
484
+ noSubscribers : 'No subscribers for the requested message. Error while connecting to NATS, service might not be started' ,
485
+ natsConnect : 'Empty response. There are no subscribers listening to that message'
486
+ }
478
487
}
479
488
} ;
Original file line number Diff line number Diff line change
1
+ import { ResponseMessages } from '@credebl/common/response-messages' ;
2
+ import {
3
+ CallHandler ,
4
+ ExecutionContext ,
5
+ Injectable ,
6
+ NestInterceptor ,
7
+ HttpException ,
8
+ Logger
9
+ } from '@nestjs/common' ;
10
+ import { Observable , throwError } from 'rxjs' ;
11
+ import { catchError } from 'rxjs/operators' ;
12
+
13
+ @Injectable ( )
14
+ export class NatsInterceptor implements NestInterceptor {
15
+ private readonly logger = new Logger ( NatsInterceptor . name ) ;
16
+
17
+ intercept ( context : ExecutionContext , next : CallHandler ) : Observable < unknown > {
18
+ return next . handle ( ) . pipe (
19
+ catchError ( ( error ) => {
20
+ if ( error . message . includes ( ResponseMessages . nats . error . natsConnect ) ) {
21
+ this . logger . error ( `No subscribers for message: ${ error . message } ` ) ;
22
+ return throwError ( ( ) => new HttpException ( ResponseMessages . nats . error . noSubscribers , 500 ) ) ;
23
+ }
24
+ return throwError ( ( ) => error ) ;
25
+ } )
26
+ ) ;
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments