Skip to content

Commit 588d2ae

Browse files
todtbdmke
authored andcommitted
Raise error if write point values missing
1 parent fe3fa08 commit 588d2ae

File tree

6 files changed

+26
-6
lines changed

6 files changed

+26
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
For the full commit log, [see here](https://github.com/influxdata/influxdb-ruby/commits/master).
44

5+
## unreleased
6+
7+
- Raise a LineProtocolError if attempting to write empty values as field set is required.
8+
Adds descriptive feedback to `{"error":"unable to parse '{series},{tags} ': invalid field format"}`
9+
510
## v0.7.0, released 2019-01-11
611

712
- Drop support for Ruby 2.2, since Bundler dropped it and we want to use

lib/influxdb/errors.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module InfluxDB # :nodoc:
55
Error = Class.new StandardError
66
AuthenticationError = Class.new Error
77
ConnectionError = Class.new Error
8+
LineProtocolError = Class.new Error
89
SeriesNotFound = Class.new Error
910
JSONParserError = Class.new Error
1011
QueryError = Class.new Error

lib/influxdb/point_value.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ def escape(str, type)
4646
end
4747

4848
def escape_values(values)
49-
return if values.nil?
49+
if values.nil? || values.empty?
50+
raise InfluxDB::LineProtocolError, "Cannot create point with empty values".freeze
51+
end
5052

5153
values.map do |k, v|
5254
key = escape(k.to_s, :field_key)

lib/influxdb/query/core.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,13 @@ def fetch_series(response)
121121
end
122122

123123
def generate_payload(data)
124-
data.map do |point|
125-
InfluxDB::PointValue.new(point).dump
126-
end.join("\n".freeze)
124+
data.map { |point| generate_point(point) }.join("\n".freeze)
125+
end
126+
127+
def generate_point(point)
128+
InfluxDB::PointValue.new(point).dump
129+
rescue InfluxDB::LineProtocolError => e
130+
(log :error, "Cannot write data: #{e.inspect}") && nil
127131
end
128132

129133
def execute(query, db: nil, **options)

spec/influxdb/cases/async_client_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
it "sends writes to client" do
1515
post_request = stub_request(:post, stub_url).to_return(status: 204)
1616

17-
(worker.max_post_points + 100).times do
18-
subject.write_point('a', {})
17+
(worker.max_post_points + 100).times do |i|
18+
subject.write_point('a', values: { i: i })
1919
end
2020

2121
# The timout code is fragile, and heavily dependent on system load

spec/influxdb/point_value_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
expected = series.join(",") + " " + fields.join(",")
4141
expect(point.dump).to eq(expected)
4242
end
43+
44+
context 'with empty values' do
45+
let(:empty_values_data) { { series: 'test_series', values: {} } }
46+
47+
it 'should raise an exception' do
48+
expect { InfluxDB::PointValue.new(empty_values_data) }.to raise_error(InfluxDB::LineProtocolError)
49+
end
50+
end
4351
end
4452

4553
describe 'dump' do

0 commit comments

Comments
 (0)