Skip to content

Commit 62b4a5a

Browse files
authored
- throw an error when log message is not string (#225)
1 parent 3cd1e16 commit 62b4a5a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/logging/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export default class Logging {
4747
}
4848

4949
push(logger, logLevel, message, exception) {
50+
if (typeof message !== 'string') {
51+
throw new Error('"message" must be a string')
52+
}
53+
5054
this.pool.push({ logger, message, exception, 'log-level': logLevel, timestamp: Date.now() })
5155

5256
this.checkMessagesLen()

test/unit/specs/logging.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,29 @@ describe('<Logging>', function() {
128128
expect(req1.body[4].exception).to.include('fatal exception')
129129
})
130130

131+
it('throws an error when message is not string', async () => {
132+
const errorMsg = '"message" must be a string'
133+
134+
async function check(method) {
135+
expect(() => logger[method](0)).to.throw(errorMsg)
136+
expect(() => logger[method](123)).to.throw(errorMsg)
137+
expect(() => logger[method](true)).to.throw(errorMsg)
138+
expect(() => logger[method](false)).to.throw(errorMsg)
139+
expect(() => logger[method](null)).to.throw(errorMsg)
140+
expect(() => logger[method](undefined)).to.throw(errorMsg)
141+
expect(() => logger[method](_ => _)).to.throw(errorMsg)
142+
expect(() => logger[method]({ bar: 123 })).to.throw(errorMsg)
143+
expect(() => logger[method](['foo', 123, true, false, null, undefined, { bar: 123 }])).to.throw(errorMsg)
144+
}
145+
146+
await check('debug')
147+
await check('info')
148+
await check('warn')
149+
await check('error')
150+
await check('fatal')
151+
await check('trace')
152+
})
153+
131154
it('send messages pool by timer', async () => {
132155
const req1 = prepareMockRequest()
133156

0 commit comments

Comments
 (0)