analytics-ruby icon indicating copy to clipboard operation
analytics-ruby copied to clipboard

Nil error message in Segment::Analytics::Response object

Open elias19r opened this issue 9 months ago • 1 comments

Hi!

I noticed that this JSON.parse(body) in Segment::Transport#send doesn't have an "error" key when the response status code is 400, which causes the returned Response object to be created with a nil error message because JSON.parse(body)['error'] is nil.

This is the snippet I used in a Rails app console to verify that:

# Gemfile

gem "analytics-ruby", "~> 2.4.0", require: "segment/analytics"
# bundle exec rails console

client = Segment::Analytics.new(
  test: true,
  write_key: "",
  on_error: Proc.new do |status, error|
    puts ">>>>> status=#{status.inspect}"
    puts ">>>>> error=#{error.inspect}"
  end
)

client.track(anonymous_id: 'test', event: 'test', properties: {})

Output:

[analytics-ruby] Sending request for 1 items
[analytics-ruby] {
  "success": false,
  "message": "An invalid write key was provided",
  "code": "invalid_request"
}
[analytics-ruby] Response status code: 400
>>>>> status=400
>>>>> error=nil

The only keys present in the JSON response are "success", "message", and "code".

elias19r avatar Apr 30 '24 15:04 elias19r

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (e4af729) 78.63% compared to head (5a01ab2) 78.54%.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #210      +/-   ##
============================================
- Coverage     78.63%   78.54%   -0.10%     
+ Complexity      498      495       -3     
============================================
  Files            78       78              
  Lines          6586     6586              
  Branches        833      833              
============================================
- Hits           5179     5173       -6     
- Misses          735      739       +4     
- Partials        672      674       +2     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Feb 21 '24 17:02 codecov[bot]