segmentio / analytics-node

The hassle-free way to integrate analytics into any node application.

Home Page:https://segment.com/libraries/node

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Promise returned by flush never rejects (and possible tests that do not work properly)

Dahaden opened this issue · comments

Just noticed the latest changes includes the promise being returned by flush. Love it!

However, after looking at the code, I noticed that the catch swallows the error and then the promise is resolved with undefined.

This is easily testable by running something like

Promise.reject('test').catch(e =>
    console.log(e)
).then(() =>
    console.log('I end up in console.')
).catch(e =>
    console.log('I will not be logged.')
)

A fix to this is either:

  1. return Promise.reject(error) in the catch block, or
  2. throw error also inside the catch block

One big caveat to doing this though is that it may cause consumers to see Unhandled rejection errors if they do not catch these promises themselves.

I then had a look at the tests to see how to test this and saw https://github.com/segmentio/analytics-node/blob/master/test.js#L334-L346


test('flush - respond with an error', async t => {
  const client = createClient()
  const callback = spy()

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

  await t.throws(client.flush(), 'Bad Request')
})

which appears to test this exact functionality (even before the flush function returned a promise!)

I noticed that the dependency ava is quite out of date and bumped it to 3.15.0 and almost got everything working, but am now getting Unhanlded rejection errors.

Will push my branch up in a PR to show what I have done, but no idea how to fix this.

@Dahaden can you let me know if this will be fixed soon? I am facing the same problem.

@muhammadmahindar Sorry almost forgot about this. Found what was causing me all the issues. New PR up with less changes in it
#294

@Dahaden any timeline for when this PR will be released?

I dont work for Segment or have any permissions on this repo.

I have made a comment suggesting that this could be considered a major version bump due to the change in client.flush actually rejecting promises and the issue of causing some consumers to get many warnings or even throws errors.

@Dahaden Thanks a lot dude. I think I'll have to clone until this is fixed.