ushu / branch_io

Client for branch.io public API
MIT License
16 stars 7 forks source link

Occasional Branch error on deep link creation #8

Open dustinkerstein opened 4 years ago

dustinkerstein commented 4 years ago

I don't have a good grasp on what is happening, but occasionally my Rails new user signup flow breaks on what appears to be a branch_io error. Here's the error I am receiving. Note I am using the omniauth-google-oauth2 gem:

2020-06-15T12:25:32.979236+00:00 heroku[router]: at=info method=POST path="/auth/google/callback" host=XYZ.herokuapp.com request_id=34a5f001-cc1d-4e61-8663-621ca7784dc7 fwd="209.6.42.243" dyno=web.1 connect=0ms service=1660ms status=204 bytes=487 protocol=https
2020-06-15T12:25:32.949836+00:00 app[web.1]: W, [2020-06-15T12:25:32.949696 #26]  WARN -- : [34a5f001-cc1d-4e61-8663-621ca7784dc7] Exception undefined method `errors' for #<BranchIO::Client::ErrorResponse:0x0000000641afb8>
2020-06-15T12:25:32.949878+00:00 app[web.1]: Did you mean?  error
2020-06-15T12:25:32.974823+00:00 app[web.1]: I, [2020-06-15T12:25:32.974652 #26]  INFO -- : [34a5f001-cc1d-4e61-8663-621ca7784dc7] No template found for ApplicationController#omniauth_callback, rendering head :no_content
2020-06-15T12:25:32.975322+00:00 app[web.1]: I, [2020-06-15T12:25:32.975229 #26]  INFO -- : [34a5f001-cc1d-4e61-8663-621ca7784dc7] Completed 204 No Content in 1357ms

Here's my branch_io related Ruby code:

after_create do |user|
    follow_users_from_default_user!()
    client = BranchIO::Client.new(Rails.application.config.branch_key, Rails.application.config.branch_secret)
    url_configs = [
        {
          channel: "#{username}",
          campaign: "Author",
          data: {
            '$og_type': "article", 
            '$og_image_url': "XYZ",
            '$og_title': "XYZ",
            '$og_description': "XYZ",
            '$fallback_url': "https://XYZ.com/u/#{username}",
            '$canonical_url': "https://XYZ.com/u/#{username}",
            author_id: "#{id}"
          }
        },
        { 
          channel: "#{username}",
          campaign: "Author",
          data: {
            '$og_type': "article", 
            '$og_image_url': "http://www.XYZ.com/media/social.png",
            '$og_title': "XYZ",
            '$og_description': "XYZ",
            '$canonical_url': "https://XYZ.com/u/#{username}",
            author_id: "#{id}"
          }
        } 
      ]

      res = client.links(url_configs)

      if res.success?
        set author_deep_link: res.urls[0]
        set author_deep_link_app_store: res.urls[1]
        logger.info "Successfully created #{res.urls[0]} and #{res.urls[1]}"
      else
        logger.error "Error creating URLs: #{res.errors.join(',')}"
        # There has been errors, but some URLs might be valid
        if res.urls.count > 0
            logger.error "Successfully created #{res.urls.count} URLs out of #{url_configs.count}: #{res.urls.join(',')}"
        end
      end
  end

Can you see what could be going on? I'm probably not handling potential errors correctly (as it breaks my entire signup flow when this happens), but any help you can provide would be greatly appreciated. Note I am using the 0.2.0 version of branch_io - Please let me know if I can get you any further info or debug. Thanks!

jdee commented 3 years ago

@dustinkerstein Sorry for the long delay in replying. Do you still experience this error? It looks like the API is returning 204, which is technically success, but the gem is treating it like an error and possibly not handing things well.

dustinkerstein commented 3 years ago

@jdee no worries at all. Yeah, I am still seeing this occasionally. Any ideas on how to tackle? Thanks!

jdee commented 3 years ago

@dustinkerstein I can see something you can try. In your code, where you have logger.error "Error creating URLs: #{res.errors.join(',')}", the ErrorResponse class does not have a method called #errors, only #error. So change that to

        logger.error "Error creating URLs: #{res.error}"

That should give you more information. The API can return a mixed result of successes and failures: https://help.branch.io/developers-hub/docs/deep-linking-api#sample-response---bulk-create. The BulkUrlsResponse handles that, but you don't get a BulkUrlsResponse unless HTTParty reports success. https://github.com/ushu/branch_io/blob/master/lib/branch_io/client/links.rb#L57. You could even do something like:

error_message = case res
  when BulkUrlsResponse
    res.errors.join(',')
  else
    res.error
  end
logger.error "Error creating URLs: #{error_message}"

That should at least let the error message come through. I'm not sure what is causing the 204 or why this is being treated as an error. That determination seems to come from HTTParty::Response#success?, not this gem, though I'm not sure.

jdee commented 3 years ago

One more thought is that if you're only creating two links at a time, try this:

res1 = client.link(url_configs[0])
res2 = client.link(url_configs[1])
dustinkerstein commented 2 years ago

@jdee Thanks! I'll take that for a spin. New code looks like:

        res1 = client.links(url_configs[0])
        if res1.success?
          @user.author_deep_link = res1.url
          logger.info "Successfully created #{res1.url}"
        else
          logger.error "Error creating URL: #{res1.error}"
        end

        res2 = client.links(url_configs[1])
        if res2.success?
          @user.author_deep_link_app_store = res2.url
          logger.info "Successfully created #{res2.url}"
        else
          logger.error "Error creating URL: #{res2.error}"
        end
dustinkerstein commented 2 years ago

I got this error:

undefined method `keys' for ["XYZ"]:Array

On this line:

res1 = client.links(url_configs[0])

I'll need to look the syntax for accessing that json. For now though, I'll just implement your other recommended logging (which works great). Thanks!