Skip to content

Commit

Permalink
fix: parse 503 plain text error (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhajek authored Sep 1, 2021
1 parent c9a3f0a commit 3cffe85
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 2.0.0 [unreleased]

### Bug Fixes
1. [#90](https://github.com/influxdata/influxdb-client-ruby/pull/90): Fix parse text plain 503 error response

### Breaking Changes
Due to a security reason `Authorization` header is not forwarded when redirect leads to a different domain.
To overcome this limitation you have to set the client property `redirect_forward_authorization` to `true`.
Expand Down
3 changes: 3 additions & 0 deletions lib/influxdb2/client/influx_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def self.from_response(response)
obj = new(message: json['message'] || '', code: response.code, reference: json['code'] || '',
retry_after: response['Retry-After'] || '')
obj
rescue JSON::ParserError
new(message: response.body || '', code: response.code, reference: '',
retry_after: response['Retry-After'] || '')
end

def self.from_message(message)
Expand Down
29 changes: 24 additions & 5 deletions test/influxdb/write_api_batching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,6 @@ def test_jitter_interval
@write_client.write(data: ['h2o_feet,location=coyote_creek water_level=1.0 1',
'h2o_feet,location=coyote_creek water_level=2.0 2'])

sleep(0.05)

assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
times: 0, body: request)

sleep(2)

assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
Expand Down Expand Up @@ -617,4 +612,28 @@ def test_backoff_time_custom
assert_gte backoff, 1_600
assert_lte backoff, 2_000
end

def test_write_error_plain_retry
error_body = 'Service Unavailable'
stub_request(:any, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
.to_return(status: 503, headers: { 'content-type' => 'text/plain', 'Retry-After' => '2' }, body: error_body)
.to_return(status: 503, headers: { 'content-type' => 'text/plain' }, body: error_body).to_return(status: 204)

client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
bucket: 'my-bucket',
org: 'my-org',
precision: InfluxDB2::WritePrecision::NANOSECOND,
use_ssl: false)

@write_options = InfluxDB2::WriteOptions.new(write_type: InfluxDB2::WriteType::BATCHING,
batch_size: 1, flush_interval: 1_000, retry_interval: 1_000)

write_api = client.create_write_api(write_options: @write_options)
request = 'h2o,location=west value=33i 15'
write_api.write(data: request)

sleep(10)
assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
times: 3, body: request)
end
end
21 changes: 21 additions & 0 deletions test/influxdb/write_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,25 @@ def test_write_collection
assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
times: 1, body: expected)
end

def test_write_error_plain
error_body = 'Service Unavailable'
stub_request(:any, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
.to_return(status: 503, headers: { 'content-type' => 'text/plain' },
body: error_body)

client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
bucket: 'my-bucket',
org: 'my-org',
precision: InfluxDB2::WritePrecision::NANOSECOND,
use_ssl: false)

error = assert_raises InfluxDB2::InfluxError do
write_api = client.create_write_api
write_api.write(data: 'h2o,location=west value=33i 15')
end

assert_equal '503', error.code
assert_equal 'Service Unavailable', error.message
end
end

0 comments on commit 3cffe85

Please sign in to comment.