Skip to content

Commit 9b0a3d6

Browse files
authored
Update jetstream logging (#495)
1 parent 525bff1 commit 9b0a3d6

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

boilerplate/ts/infra/nest-jetstream/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bitloops/bl-boilerplate-infra-nest-jetstream",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"description": "TypeScript nats jetstream code for Bitloops Language generated projects using NestJS",
55
"engines": {
66
"node": ">= 13"

boilerplate/ts/infra/nest-jetstream/src/buses/nats-pubsub-command-bus.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ export class NatsPubSubCommandBus implements Infra.CommandBus.IPubSubCommandBus
5858
});
5959
return jsonCodec.decode(response.data);
6060
} catch (error: any) {
61-
this.logger.error('Error in command request for:' + topic, error);
61+
this.logger.error(
62+
`Error in command request for: ${topic}. Error message: ${error.message}`,
63+
error.stack,
64+
);
65+
6266
return { isOk: false, data: error.message };
6367
}
6468
}
@@ -77,9 +81,10 @@ export class NatsPubSubCommandBus implements Infra.CommandBus.IPubSubCommandBus
7781
await this.asyncLocalStorage.run(contextData, async () => {
7882
return this.handleReceivedCommand(handler, command, m);
7983
});
80-
} catch (err) {
84+
} catch (err: any) {
8185
this.logger.error(
82-
`[${subject}]Error in command-bus subscribe:: ${JSON.stringify(err)}`,
86+
`[${subject}]: Error in command handling. Error message: ${err.message}`,
87+
err.stack,
8388
);
8489
}
8590
}

boilerplate/ts/infra/nest-jetstream/src/buses/nats-pubsub-query-bus.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Inject, Injectable, Logger } from '@nestjs/common';
22
import { NatsConnection, JSONCodec, headers, MsgHdrs } from 'nats';
33
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';
55
import { AsyncLocalStorage } from 'node:async_hooks';
66
import { ContextPropagation } from './utils/context-propagation';
77

@@ -17,6 +17,8 @@ export class NatsPubSubQueryBus implements Infra.QueryBus.IQueryBus {
1717
// @Optional()
1818
@Inject(ASYNC_LOCAL_STORAGE)
1919
private readonly asyncLocalStorage: AsyncLocalStorage<any>,
20+
@Inject(TIMEOUT_MILLIS)
21+
private readonly timeoutMillis: number,
2022
) {
2123
this.nc = this.nats.getConnection();
2224
}
@@ -39,14 +41,17 @@ export class NatsPubSubQueryBus implements Infra.QueryBus.IQueryBus {
3941
try {
4042
const response = await this.nc.request(topic, jsonCodec.encode(query), {
4143
headers,
42-
timeout: 10000,
44+
timeout: this.timeoutMillis,
4345
});
4446

4547
const data = jsonCodec.decode(response.data);
4648
this.logger.log('Response in query request:' + data);
4749
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+
);
5055
}
5156
}
5257

@@ -85,8 +90,11 @@ export class NatsPubSubQueryBus implements Infra.QueryBus.IQueryBus {
8590
}
8691

8792
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+
);
9098
}
9199
}
92100
})();

0 commit comments

Comments
 (0)