Skip to content

Commit e88e3e2

Browse files
committed
include worker id in all error messages from worker code
1 parent 189bb58 commit e88e3e2

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

source/worker.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ const debug = createDebug(
2626
}`,
2727
)
2828

29+
const errorPrefix = `ClusterMemoryStoreWorker:${
30+
cluster.worker?.id ?? 'not-a-worker'
31+
}:`
32+
2933
/**
3034
* A `Store` for the `express-rate-limit` package that communicates with the primary process to store and retrieve hits
3135
*/
@@ -63,9 +67,7 @@ export class ClusterMemoryStoreWorker implements Store {
6367
this.prefix = options?.prefix ?? 'default'
6468
if (!cluster.isWorker) {
6569
console.warn(
66-
new Error(
67-
'ClusterMemoryStoreWorker instance created in non-worker process',
68-
),
70+
new Error(errorPrefix + ' instance created in non-worker process'),
6971
)
7072
}
7173
}
@@ -78,18 +80,18 @@ export class ClusterMemoryStoreWorker implements Store {
7880
* @impl
7981
*/
8082
async init(options: RateLimitConfiguration) {
81-
debug('Initializing with options %o', options)
83+
debug('Initializing with parent options %o', options)
8284
this.windowMs = options.windowMs
8385
if (!cluster.worker) {
8486
throw new Error(
85-
'ClusterMemoryStoreWorker: cluster.worker is undefined, unable to initialize',
87+
`${errorPrefix} cluster.worker is undefined, unable to initialize`,
8688
)
8789
}
8890

8991
cluster.worker.on('message', this.onMessage.bind(this))
9092
return this.send('init', [{ windowMs: this.windowMs }]).catch(
9193
(error: any) => {
92-
console.error('ClusterMemoryStoreWorker: failed to initialize', error)
94+
console.error(`${errorPrefix} failed to initialize`, error)
9395
},
9496
)
9597
}
@@ -137,7 +139,7 @@ export class ClusterMemoryStoreWorker implements Store {
137139
const timeoutId = setTimeout(() => {
138140
reject(
139141
new Error(
140-
`ClusterMemoryStoreWorker: no response recieved to ${command} command after ${timelimit}ms.`,
142+
`${errorPrefix} no response recieved to ${command} command after ${timelimit}ms.`,
141143
),
142144
)
143145
this.openRequests.delete(requestId)
@@ -146,7 +148,7 @@ export class ClusterMemoryStoreWorker implements Store {
146148
if (!process.send) {
147149
reject(
148150
new Error(
149-
'ClusterMemoryStoreWorker: process.send is undefined, indicating that this is probably not a node:cluster worker.',
151+
`${errorPrefix} process.send is undefined, indicating that this is probably not a node:cluster worker.`,
150152
),
151153
)
152154
return
@@ -175,7 +177,8 @@ export class ClusterMemoryStoreWorker implements Store {
175177
if (!shouldSendMore) {
176178
console.warn(
177179
new Error(
178-
'CluserMemoryStoreWorker: process.send() returned false indicating that the channel is closed and/or there is a large backlog of messages waiting to be sent.',
180+
errorPrefix +
181+
' process.send() returned false indicating that the channel is closed and/or there is a large backlog of messages waiting to be sent.',
179182
),
180183
)
181184
}
@@ -196,7 +199,7 @@ export class ClusterMemoryStoreWorker implements Store {
196199
} else {
197200
console.warn(
198201
new Error(
199-
'ClusterMemoryStoreWorker: response recieved without matching open request: ' +
202+
`${errorPrefix} response recieved without matching open request: ` +
200203
JSON.stringify(message_),
201204
),
202205
)

0 commit comments

Comments
 (0)