Skip to content

Commit 4a7e410

Browse files
fix: address review comments for logging and tests
1 parent 7e6fa8a commit 4a7e410

7 files changed

Lines changed: 52 additions & 7 deletions

File tree

src/factories/logger-factory.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ const logAtLevel = (
4747
return
4848
}
4949

50+
const errorFromArgs = args.find((arg) => arg instanceof Error) as Error | undefined
51+
52+
if (errorFromArgs) {
53+
if (typeof message === 'string') {
54+
instance[level]({ err: errorFromArgs }, safeFormat(message, args))
55+
return
56+
}
57+
58+
const data = [message, ...args].filter((arg) => !(arg instanceof Error))
59+
const formatted = data.map(stringifyForLog).join(' ')
60+
61+
if (formatted) {
62+
instance[level]({ err: errorFromArgs }, formatted)
63+
} else {
64+
instance[level]({ err: errorFromArgs })
65+
}
66+
67+
return
68+
}
69+
5070
if (typeof message === 'string') {
5171
instance[level](safeFormat(message, args))
5272
return
@@ -87,7 +107,7 @@ const createMessageLogger = (instance: PinoLogger, scope: string): MessageLogger
87107

88108
export const createLogger = (
89109
namespace: string,
90-
options: { enabled?: boolean; stdout?: boolean } = { enabled: false, stdout: false },
110+
options: { enabled?: boolean } = { enabled: false },
91111
) => {
92112
const prefix = cluster.isWorker ? (process.env.WORKER_TYPE ?? 'worker') : 'primary'
93113
const scope = namespace ? `${prefix}:${namespace}` : prefix

src/repositories/invoice-repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class InvoiceRepository implements IInvoiceRepository {
2323
try {
2424
await client.raw('select confirm_invoice(?, ?, ?)', [invoiceId, amountPaid.toString(), confirmedAt.toISOString()])
2525
} catch (error) {
26-
logger.error('Unable to confirm invoice. Reason:', error.message)
26+
logger.error('Unable to confirm invoice. Reason:', error)
2727

2828
throw error
2929
}

src/services/payments-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class PaymentsService implements IPaymentsService {
2929
try {
3030
return await this.invoiceRepository.findPendingInvoices(0, 10)
3131
} catch (error) {
32-
logger.info('Unable to get pending invoices.', error)
32+
logger.error('Unable to get pending invoices.', error)
3333

3434
throw error
3535
}
@@ -41,7 +41,7 @@ export class PaymentsService implements IPaymentsService {
4141
typeof invoice === 'string' || invoice?.verifyURL ? invoice : invoice.id,
4242
)
4343
} catch (error) {
44-
logger.info('Unable to get invoice from payments processor. Reason:', error)
44+
logger.error('Unable to get invoice from payments processor. Reason:', error)
4545

4646
throw error
4747
}

src/tor/client.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ export const addOnion = async (port: number, host?: string): Promise<string> =>
204204

205205
if (hiddenService?.PrivateKey) {
206206
logger.info('saving private key to %s', path)
207-
logger('saving private key to %s', path)
208207

209208
await writeFile(path, hiddenService.PrivateKey, 'utf8')
210209
return hiddenService.ServiceID!

test/unit/factories/logger-factory.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@ describe('createLogger', () => {
6767
Sinon.assert.calledOnceWithExactly(rootInstance.error, { err: error })
6868
})
6969

70+
it('logs structured err when Error is passed as an arg to string message', () => {
71+
const logger = createLogger('payments')
72+
const error = new Error('boom')
73+
74+
logger.error('Unable to create invoice. Reason:', error)
75+
76+
Sinon.assert.calledOnce(rootInstance.error)
77+
Sinon.assert.calledWith(rootInstance.error, { err: error })
78+
expect(rootInstance.error.firstCall.args[1]).to.be.a('string').and.include('Unable to create invoice. Reason:')
79+
})
80+
81+
it('keeps non-error args in message while logging structured err', () => {
82+
const logger = createLogger('payments')
83+
const error = new Error('boom')
84+
85+
logger.error('error handling message', { kind: 1 }, error)
86+
87+
Sinon.assert.calledOnce(rootInstance.error)
88+
Sinon.assert.calledWith(rootInstance.error, { err: error })
89+
expect(rootInstance.error.firstCall.args[1]).to.be.a('string').and.include("kind: 1")
90+
})
91+
7092
it('logs mixed non-string messages safely', () => {
7193
const logger = createLogger('payments')
7294

test/unit/services/payments-service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ describe('PaymentsService', () => {
463463
expect(eventUtils.broadcastEvent as Sinon.SinonStub).to.have.been.calledOnce
464464
})
465465

466-
it('calls logError and does not throw when the pipeline fails', async () => {
466+
it('does not throw when the pipeline fails', async () => {
467467
;(eventUtils.identifyEvent as Sinon.SinonStub).rejects(new Error('identify failed'))
468468

469469
// otherwise() swallows the error — the method must resolve, not reject

test/unit/utils/settings.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ describe('SettingsStatic', () => {
198198

199199
expect(SettingsStatic.createSettings()).to.be.an('object')
200200

201-
expect(existsSyncStub).to.have.been.calledWithExactly('/some/path/settings.json')
201+
const settingsPathExistsChecks = existsSyncStub.getCalls().filter((call) => {
202+
return call.args.length === 1 && call.args[0] === '/some/path/settings.json'
203+
})
204+
205+
expect(settingsPathExistsChecks).to.have.lengthOf(1)
202206
expect(getSettingsFileBasePathStub).to.have.been.calledOnce
203207
expect(saveSettingsStub).to.have.been.calledOnceWithExactly('/some/path/settings.json', Sinon.match.object)
204208
expect(loadSettingsStub).to.have.been.called

0 commit comments

Comments
 (0)