Skip to content

.ready: fix behavior when packages fail to load #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

calvinfo
Copy link
Contributor

@calvinfo calvinfo commented May 19, 2018

Netto brought an error with the Appcues integration up to my
attention. Apparently when the script fails to load, it causes
an exception which bubbles up to the top.

I looked through the code and was pretty surprised by this, since
the Appcues integration itself is very few lines of code, and
nothing out of the ordinary.

When I dug in, I realized that this comes from an error where the
Appcues javascript fails to load for some reason.

When that happens here's the sequence of events:

  • segmentio/load-script gets called, and then calls a callback
    with the error
  • this in turn calls .ready (in Appcues, but I think also many
    other integrations)
  • this emits a ready handler, which then immediately starts
    flushing the message queue, causing an uncaught exception

This fix instead causes .ready when called with an error to
not mark the integration as ready. Avoiding further errors.

The one thing I'm unsure of here is whether we ever end up calling
.ready() with an argument–my hunch is 'no' given that it wouldn't
do anything, but it might be worth adding an instanceof Error check.

Thoughts?

Relevant Appcues code: https://github.com/segment-integrations/analytics.js-integration-appcues/blob/master/lib/index.js#L27-L29

Relevant a.js integration load code: https://github.com/segmentio/analytics.js-integration/blob/master/lib/protos.js#L237-L280

Netto brought an error with the Appcues integration up to my
attention. Apparently when the script fails to load, it causes
an exception which bubbles up to the top.

I looked through the code and was pretty surprised by this, since
the Appcues integration itself is very few lines of code, and
nothing out of the ordinary.

When I dug in, I realized that this comes from an error where the
Appcues javascript fails to load for some reason.

When that happens here's the sequence of events:

- segmentio/load-script gets called, and then calls a callback
  with the error
- this in turn calls .ready (in Appcues, but I think also many
  other integrations)
- this emits a ready handler, which then immediately starts
  flushing the message queue, causing an uncaught exception

This fix instead causes .ready when called with an error to
_not_ mark the integration as ready. Avoiding further errors.

The one thing I'm unsure of here is whether we ever end up calling
.ready() with an argument–my hunch is 'no' given that it wouldn't
do anything, but it might be worth adding an instanceof Error check.

Thoughts?
@calvinfo
Copy link
Contributor Author

cc @nettofarah this should fix the uncaughts we're seeing

@calvinfo
Copy link
Contributor Author

Separately, we should probably think about a way to make sure that this is try-catched in each integration, if it somehow errors in the callback and set of queues.

@Peripheral1994
Copy link

One thing I notice is that https://github.com/segmentio/analytics.js-core/blob/master/test/analytics.test.js seems to use the only variety of .ready(callback). I can't tell for sure what it intends and this might just be legacy behavior.

It don't see any production cases of calling .ready with any arguments, so we should be safe in that regard, though I agree that adding in a simple instanceof check should protect us in case this changes in the future.

@f2prateek
Copy link
Contributor

f2prateek commented May 19, 2018

Nice - I think the reasoning sound good to me.

Couple of notes:

@calvinfo
Copy link
Contributor Author

Thanks for the reviews guys! I hadn't realized either the 2.x branch or the .ready problem... I'll circle back here. One idea for the short-term is to update the Appcues code to now fire when an error is encountered. I'm not sure if I'll have enough cycles on-hand to go through and audit everything right now, but let me think on it for another day.

@nettofarah
Copy link

thanks for the update, @calvinfo.

@f2prateek
Copy link
Contributor

I've added this to our JIRA (https://segment.atlassian.net/browse/LIB-372) so we can pick this up soon.

@dobesv
Copy link

dobesv commented May 22, 2019

We're running into this issue now. An Ad-Blocker blocks twitter ads from loading, and then none of our integrations load. Is there anything we can do to move this fix forward? Once this part of it is merged we would want to update the twitter integration itself to propagate errors back to analytics.js

@f2prateek f2prateek removed their request for review July 2, 2019 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants