From b0f9d316c94a7968c50892669e715fb480fc9339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 12 Oct 2020 16:26:08 +0300 Subject: [PATCH] REPP: Refactor handle_errors() --- app/controllers/repp/v1/base_controller.rb | 27 +++++++++++----------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/app/controllers/repp/v1/base_controller.rb b/app/controllers/repp/v1/base_controller.rb index 5116a2238..714fb3a47 100644 --- a/app/controllers/repp/v1/base_controller.rb +++ b/app/controllers/repp/v1/base_controller.rb @@ -27,26 +27,25 @@ module Repp def handle_errors(obj = nil, update: false) @epp_errors ||= [] - if obj - obj.construct_epp_errors - @epp_errors += obj.errors[:epp_errors] - end - - if update - @epp_errors.each_with_index do |errors, index| - next unless errors[:code] == '2304' && errors[:value].present? && - errors[:value][:val] == DomainStatus::SERVER_DELETE_PROHIBITED && - errors[:value][:obj] == 'status' - - @epp_errors[index][:value][:val] = DomainStatus::PENDING_UPDATE - end - end + obj&.construct_epp_errors + @epp_errors += obj.errors[:epp_errors] if obj + format_epp_errors if update @epp_errors.uniq! render_epp_error end + def format_epp_errors + @epp_errors.each_with_index do |errors, index| + next unless errors[:code] == '2304' && errors[:value].present? && + errors[:value][:val] == DomainStatus::SERVER_DELETE_PROHIBITED && + errors[:value][:obj] == 'status' + + @epp_errors[index][:value][:val] = DomainStatus::PENDING_UPDATE + end + end + def render_epp_error render( json: { code: @epp_errors[0][:code], message: @epp_errors[0][:msg] },