From a63b9fcf804b91057588c9a97253694fefbb0d8e Mon Sep 17 00:00:00 2001 From: Tim Cowlishaw Date: Mon, 14 Oct 2024 08:23:47 +0200 Subject: [PATCH] raise error to be captured on parser failure --- app/lib/raw_mqtt_message_parser.rb | 4 +++- spec/lib/raw_mqtt_message_parser_spec.rb | 5 ++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/lib/raw_mqtt_message_parser.rb b/app/lib/raw_mqtt_message_parser.rb index ba2c8f48..dfa5e61d 100644 --- a/app/lib/raw_mqtt_message_parser.rb +++ b/app/lib/raw_mqtt_message_parser.rb @@ -6,7 +6,9 @@ def initialize end def parse(message) - parser.parse(self.convert_to_ascii(message.strip))&.to_hash + message = parser.parse(self.convert_to_ascii(message.strip))&.to_hash + raise "Message not parsed: #{message}" unless message + return message end private diff --git a/spec/lib/raw_mqtt_message_parser_spec.rb b/spec/lib/raw_mqtt_message_parser_spec.rb index 7e3d798a..6987187a 100644 --- a/spec/lib/raw_mqtt_message_parser_spec.rb +++ b/spec/lib/raw_mqtt_message_parser_spec.rb @@ -59,10 +59,9 @@ expect(parsed).to eq({ data: [ { recorded_at: "2024-09-25T13:19:38Z", sensors: [{id: "100", value: "-2000.12345"}] }]}) end - it "returns nil if no valid message parsed" do + it "raises an error if no valid message parsed" do message = "ceci n'est pas un message" - parsed = parser.parse(message) - expect(parsed).to eq(nil) + expect {parser.parse(message)}.to raise_error(RuntimeError) end it "parses timestamps at any position in the packet" do