Skip to content

Commit

Permalink
Add rescue when receive bad JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
kudrin committed Feb 10, 2022
1 parent 9285c96 commit dd313fa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.3.7] - 2022-02-10
Changed
- Add exception when received broken JSON

## [0.3.6] - 2022-02-09
Changed
- Downgrade minimum ruby version to 2.4.0
Expand Down
8 changes: 6 additions & 2 deletions lib/cyclone_lariat/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ def initialize(dataset: nil, errors_notifier: nil, message_notifier: nil, repo:
end

def call(_worker_instance, queue, _sqs_msg, body, &block)
msg = receive_message(body)
msg = receive_message!(body)

message_notifier&.info 'Receive message', message: msg, queue: queue
return if msg.is_a? String

catch_standard_error(queue, msg) do
event = Event.wrap(msg)
Expand All @@ -30,8 +31,11 @@ def call(_worker_instance, queue, _sqs_msg, body, &block)

attr_reader :errors_notifier, :message_notifier, :events_repo

def receive_message(body)
def receive_message!(body)
body[:Message] ? JSON.parse(body[:Message], symbolize_names: true) : body
rescue JSON::ParserError => e
errors_notifier&.error(e, message: body[:Message])
body[:Message]
end

def store_in_dataset(event)
Expand Down
2 changes: 1 addition & 1 deletion lib/cyclone_lariat/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module CycloneLariat
VERSION = '0.3.6'
VERSION = '0.3.7'
end
17 changes: 17 additions & 0 deletions spec/lib/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@
expect { receive_event }.to raise_error(StandardError)
end
end

context 'receive bad JSON' do
subject(:receive_event) do
middleware.call(nil, 'create_message', nil, { 'MessageId': 12, 'Message': 'I`m bad JSON`' }) do
true
end
end

it 'should write ERROR notify' do
expect(notifier).to receive(:error)
receive_event
end

it 'should not raise error' do
expect { receive_event }.to_not raise_error
end
end
end

context 'when messages_repo is defined' do
Expand Down

0 comments on commit dd313fa

Please sign in to comment.