DRY up protocols importer services errors

This commit is contained in:
Jure Grabnar 2019-06-27 10:11:36 +02:00
parent 86bc69e510
commit d31f978e55
2 changed files with 23 additions and 26 deletions

View file

@ -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

View file

@ -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