Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #342 from tam-carre/master
Browse files Browse the repository at this point in the history
Add an errorHandler property to constructor options
  • Loading branch information
pooyaj committed Jun 22, 2022
2 parents 2787295 + 81024d9 commit fb41963
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Analytics {
* @property {Object} [axiosInstance] (default: axios.create(options.axiosConfig))
* @property {Object} [axiosRetryConfig] (optional)
* @property {Number} [retryCount] (default: 3)
* @property {Function} [errorHandler] (optional)
*/

constructor (writeKey, options) {
Expand All @@ -50,6 +51,7 @@ class Analytics {
this.maxQueueSize = options.maxQueueSize || 1024 * 450 // 500kb is the API limit, if we approach the limit i.e., 450kb, we'll flush
this.flushInterval = options.flushInterval || 10000
this.flushed = false
this.errorHandler = options.errorHandler
Object.defineProperty(this, 'enable', {
configurable: false,
writable: false,
Expand Down Expand Up @@ -295,6 +297,10 @@ class Analytics {
return Promise.resolve(data)
})
.catch(err => {
if (typeof this.errorHandler === 'function') {
return this.errorHandler(err)
}

if (err.response) {
const error = new Error(err.response.statusText)
done(error)
Expand Down
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,22 @@ test('flush - respond with an error', async t => {
await t.throws(client.flush(), 'Bad Request')
})

test('flush - do not throw on axios failure if errorHandler option is specified', async t => {
const errorHandler = spy()
const client = createClient({ errorHandler })
const callback = spy()

client.queue = [
{
message: 'error',
callback
}
]

await t.notThrows(client.flush())
t.true(errorHandler.calledOnce)
})

test('flush - time out if configured', async t => {
const client = createClient({ timeout: 500 })
const callback = spy()
Expand Down

0 comments on commit fb41963

Please sign in to comment.