1
1
import { Inject , Injectable , Logger } from '@nestjs/common' ;
2
2
import { NatsConnection , JSONCodec , headers , MsgHdrs } from 'nats' ;
3
3
import { Application , Infra } from '@bitloops/bl-boilerplate-core' ;
4
- import { ASYNC_LOCAL_STORAGE , ProvidersConstants } from '../jetstream.constants' ;
4
+ import { ASYNC_LOCAL_STORAGE , ProvidersConstants , TIMEOUT_MILLIS } from '../jetstream.constants' ;
5
5
import { AsyncLocalStorage } from 'node:async_hooks' ;
6
6
import { ContextPropagation } from './utils/context-propagation' ;
7
7
@@ -17,6 +17,8 @@ export class NatsPubSubQueryBus implements Infra.QueryBus.IQueryBus {
17
17
// @Optional ()
18
18
@Inject ( ASYNC_LOCAL_STORAGE )
19
19
private readonly asyncLocalStorage : AsyncLocalStorage < any > ,
20
+ @Inject ( TIMEOUT_MILLIS )
21
+ private readonly timeoutMillis : number ,
20
22
) {
21
23
this . nc = this . nats . getConnection ( ) ;
22
24
}
@@ -39,14 +41,17 @@ export class NatsPubSubQueryBus implements Infra.QueryBus.IQueryBus {
39
41
try {
40
42
const response = await this . nc . request ( topic , jsonCodec . encode ( query ) , {
41
43
headers,
42
- timeout : 10000 ,
44
+ timeout : this . timeoutMillis ,
43
45
} ) ;
44
46
45
47
const data = jsonCodec . decode ( response . data ) ;
46
48
this . logger . log ( 'Response in query request:' + data ) ;
47
49
return data ;
48
- } catch ( err ) {
49
- this . logger . error ( 'Error in query request for:' + topic , err ) ;
50
+ } catch ( err : any ) {
51
+ this . logger . error (
52
+ `[${ topic } ]: Error in query request. Error message: ${ err . message } ` ,
53
+ err . stack ,
54
+ ) ;
50
55
}
51
56
}
52
57
@@ -85,8 +90,11 @@ export class NatsPubSubQueryBus implements Infra.QueryBus.IQueryBus {
85
90
}
86
91
87
92
this . logger . log ( `[${ sub . getProcessed ( ) } ]: ${ JSON . stringify ( jsonCodec . decode ( m . data ) ) } ` ) ;
88
- } catch ( err ) {
89
- this . logger . error ( `[PubSubQuery: ${ subject } ] Error in handler:` , err ) ;
93
+ } catch ( err : any ) {
94
+ this . logger . error (
95
+ `[${ subject } ]: Error in query handling. Error message: ${ err . message } ` ,
96
+ err . stack ,
97
+ ) ;
90
98
}
91
99
}
92
100
} ) ( ) ;
0 commit comments