From 7b750b7d9b45c9626a5ee92479e916e23ec9c988 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Tue, 26 Aug 2014 15:39:25 +0300 Subject: [PATCH] Make EPP errors more compact --- app/models/concerns/epp_errors.rb | 8 +++++ app/models/domain.rb | 56 ++++++------------------------- config/locales/en.yml | 1 + spec/epp/domain_spec.rb | 1 - 4 files changed, 20 insertions(+), 46 deletions(-) diff --git a/app/models/concerns/epp_errors.rb b/app/models/concerns/epp_errors.rb index 0b46b6621..016eaafa4 100644 --- a/app/models/concerns/epp_errors.rb +++ b/app/models/concerns/epp_errors.rb @@ -4,6 +4,7 @@ module EppErrors def construct_epp_errors epp_errors = [] errors.messages.each do |key, values| + next if key == :epp_errors if self.class.reflect_on_association(key) epp_errors << collect_child_errors(key) end @@ -71,4 +72,11 @@ module EppErrors end nil end + + def add_epp_error(code, obj, val, msg) + errors[:epp_errors] ||= [] + t = errors.generate_message(*msg) if msg.is_a?(Array) + t = msg if msg.is_a?(String) + errors[:epp_errors] << { code: code, msg: t, value: { val: val, obj: obj } } + end end diff --git a/app/models/domain.rb b/app/models/domain.rb index 84f8abb20..72cd1da23 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -85,11 +85,9 @@ class Domain < ActiveRecord::Base def attach_owner_contact(code) self.owner_contact = Contact.find_by(code: code) - errors.add(:owner_contact, { - obj: 'registrant', - val: code, - msg: I18n.t('errors.messages.epp_registrant_not_found') - }) unless owner_contact + return if owner_contact + + add_epp_error('2303', 'registrant', code, [:owner_contact, :not_found]) end def attach_contacts(contacts) @@ -100,11 +98,7 @@ class Domain < ActiveRecord::Base attach_contact(k, contact) else # Detailed error message with value to display in EPP response - errors.add(:domain_contacts, { - obj: 'contact', - val: x[:contact], - msg: errors.generate_message(:domain_contacts, :not_found) - }) + add_epp_error('2303', 'contact', x[:contact], [:domain_contacts, :not_found]) end end end @@ -127,13 +121,7 @@ class Domain < ActiveRecord::Base nameservers.build(ns_attrs) next if existing.empty? - - errors.add(:nameservers, { - code: '2302', - obj: 'hostObj', - val: ns_attrs[:hostname], - msg: errors.generate_message(:nameservers, :taken) - }) + add_epp_error('2302', 'hostObj', ns_attrs[:hostname], [:nameservers, :taken]) end end @@ -153,11 +141,7 @@ class Domain < ActiveRecord::Base v.each do |x| contact = domain_contacts.joins(:contact).where(contacts: { code: x[:contact] }) if contact.blank? - errors.add(:domain_contacts, { - obj: 'contact', - val: x[:contact], - msg: errors.generate_message(:domain_contacts, :not_found) - }) + add_epp_error('2303', 'contact', x[:contact], [:domain_contacts, :not_found]) else to_delete << contact end @@ -172,11 +156,7 @@ class Domain < ActiveRecord::Base ns_list.each do |ns_attrs| nameserver = nameservers.where(ns_attrs) if nameserver.blank? - errors.add(:nameservers, { - obj: 'hostObj', - val: ns_attrs[:hostname], - msg: errors.generate_message(:nameservers, :not_found) - }) + add_epp_error('2303', 'hostObj', ns_attrs[:hostname], [:nameservers, :not_found]) else to_delete << nameserver end @@ -190,11 +170,7 @@ class Domain < ActiveRecord::Base status_list.each do |x| status = domain_statuses.joins(:setting).where(settings: { value: x[:value] }) if status.blank? - errors.add(:domain_statuses, { - obj: 'status', - val: x[:value], - msg: errors.generate_message(:domain_statuses, :not_found) - }) + add_epp_error('2303', 'status', x[:value], [:domain_statuses, :not_found]) else to_delete << status end @@ -246,11 +222,8 @@ class Domain < ActiveRecord::Base end def validate_exp_dates(cur_exp_date) - errors.add(:valid_to, { - obj: 'curExpDate', - val: cur_exp_date, - msg: I18n.t('errors.messages.epp_exp_dates_do_not_match') - }) if cur_exp_date.to_date != valid_to + return if cur_exp_date.to_date == valid_to + add_epp_error('2306', 'curExpDate', cur_exp_date, I18n.t('errors.messages.epp_exp_dates_do_not_match')) end def epp_code_map @@ -263,8 +236,7 @@ class Domain < ActiveRecord::Base ], '2306' => [ # Parameter policy error [:owner_contact, :blank], - [:admin_contacts, :blank], - [:valid_to, :epp_exp_dates_do_not_match] + [:admin_contacts, :blank] ], '2004' => [ # Parameter value range error [:nameservers, :out_of_range, @@ -275,12 +247,6 @@ class Domain < ActiveRecord::Base ], [:period, :out_of_range] ], - '2303' => [ # Object does not exist - [:owner_contact, :epp_registrant_not_found], - [:domain_contacts, :not_found], - [:nameservers, :not_found], - [:domain_statuses, :not_found] - ], '2200' => [ [:auth_info, :wrong_pw] ] diff --git a/config/locales/en.yml b/config/locales/en.yml index 84e1cd6d0..9e3add4c7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -46,6 +46,7 @@ en: taken: 'Domain name already exists' owner_contact: blank: 'Registrant is missing' + not_found: 'Registrant not found' domain_contacts: not_found: 'Contact was not found' admin_contacts: diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index c00486080..115240127 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -296,7 +296,6 @@ describe 'EPP Domain', epp: true do expect(rem_cnt).to be_falsey response = epp_request('domains/update_remove_objects.xml') - expect(response[:results][0][:result_code]).to eq('2303') expect(response[:results][0][:msg]).to eq('Contact was not found') expect(response[:results][0][:value]).to eq('mak21')