From d31f978e5574187b1ebd352d347cfa2b31fb7fb1 Mon Sep 17 00:00:00 2001 From: Jure Grabnar Date: Thu, 27 Jun 2019 10:11:36 +0200 Subject: [PATCH] DRY up protocols importer services errors --- .../build_protocol_from_client_service.rb | 25 ++++++++----------- .../search_protocols_service.rb | 24 +++++++++--------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/app/services/protocol_importers/build_protocol_from_client_service.rb b/app/services/protocol_importers/build_protocol_from_client_service.rb index bc6dac297..3746e6925 100644 --- a/app/services/protocol_importers/build_protocol_from_client_service.rb +++ b/app/services/protocol_importers/build_protocol_from_client_service.rb @@ -3,6 +3,7 @@ module ProtocolImporters class BuildProtocolFromClientService extend Service + require 'protocol_importers/protocols_io/v3/errors' attr_reader :errors, :built_protocol @@ -18,20 +19,10 @@ module ProtocolImporters return self unless valid? # Call api client - begin - api_response = api_client.single_protocol(@id) - rescue api_errors => e - @errors[e.error_type] = e.message - return self - end + api_response = api_client.single_protocol(@id) # Normalize protocol - begin - normalized_hash = normalizer.normalize_protocol(api_response) - rescue normalizer_errors => e - @errors[e.error_type] = e.message - return self - end + normalized_hash = normalizer.normalize_protocol(api_response) pio = ProtocolImporters::ProtocolIntermediateObject.new(normalized_json: normalized_hash, user: @user, @@ -40,6 +31,12 @@ module ProtocolImporters @built_protocol = pio.build @errors[:protocol] = pio.protocol.errors unless @built_protocol.valid? self + rescue api_errors => e + @errors[e.error_type] = e.message + self + rescue normalizer_errors => e + @errors[e.error_type] = e.message + self rescue StandardError => e @errors[:build_protocol] = e.message self @@ -77,11 +74,11 @@ module ProtocolImporters end def api_errors - "ProtocolImporters::#{endpoint_name}::V3Errors::Error".constantize + "ProtocolImporters::#{endpoint_name}::Error".constantize end def normalizer_errors - "ProtocolImporters::#{endpoint_name}::V3Errors::NormalizerError".constantize + "ProtocolImporters::#{endpoint_name}::NormalizerError".constantize end end end diff --git a/app/services/protocol_importers/search_protocols_service.rb b/app/services/protocol_importers/search_protocols_service.rb index 7d68a022e..4c56af527 100644 --- a/app/services/protocol_importers/search_protocols_service.rb +++ b/app/services/protocol_importers/search_protocols_service.rb @@ -3,6 +3,7 @@ module ProtocolImporters class SearchProtocolsService extend Service + require 'protocol_importers/protocols_io/v3/errors' attr_reader :errors, :protocols_list @@ -18,21 +19,20 @@ module ProtocolImporters return self unless valid? # Call api client - begin - api_response = api_client.protocol_list(@query_params) - rescue api_errors => e - @errors[e.error_type] = e.message - return self - end + api_response = api_client.protocol_list(@query_params) # Normalize protocols list - begin - @protocols_list = normalizer.normalize_list(api_response) - rescue normalizer_errors => e - @errors[e.error_type] = e.message - return self - end + @protocols_list = normalizer.normalize_list(api_response) + self + rescue api_errors => e + @errors[e.error_type] = e.message + self + rescue normalizer_errors => e + @errors[e.error_type] = e.message + self + rescue StandardError => e + @errors[:build_protocol] = e.message self end