Skip to content

Commit

Permalink
Merge pull request #78 from vira-khdr/development
Browse files Browse the repository at this point in the history
Fix: Handle Promise rejection, when batching is turned off
  • Loading branch information
JaniAnttonen authored Oct 7, 2021
2 parents 397bc29 + b0eb4eb commit ae37b3f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ class LokiTransport extends Transport {
}

// Pushes the log to the batcher
this.batcher.pushLogEntry(logEntry)
this.batcher.pushLogEntry(logEntry).catch(err => {
// eslint-disable-next-line no-console
console.error(err)
})

// Trigger the optional callback
callback()
Expand Down
1 change: 1 addition & 0 deletions test/fixtures.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"options_json": { "host": "http://localhost", "interval": 0.1, "json": true },
"options_no_batching": { "host": "http://localhost", "batching": false },
"options_protobuf": {
"host": "http://localhost",
"interval": 0.1,
Expand Down
16 changes: 16 additions & 0 deletions test/transport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ describe('Integration tests', function () {
})
expect(callback).toBe(true)
})
it('LokiTransport shouldn\'t cause unhandledPromiseRejection when batching is turned off', async function () {
let consoleError = false
// eslint-disable-next-line no-console
console.error = jest
.spyOn(console, 'error')
.mockImplementation(() => {
consoleError = true
})

const lokiTransport = new LokiTransport(fixtures.options_no_batching)
lokiTransport.log(fixtures.logs[0], () => {})

// Wait for Promise (this.batcher.pushLogEntry) be rejected
await new Promise(resolve => setTimeout(resolve, 100))
expect(consoleError).toBe(true)
})
it('LokiTransport should transfer logs to the Batcher', function () {
const lokiTransport = new LokiTransport(fixtures.options_json)
lokiTransport.log(fixtures.logs[0], () => {})
Expand Down

0 comments on commit ae37b3f

Please sign in to comment.