Error handling

This commit is contained in:
Martin Lensment 2014-08-06 14:31:29 +03:00
parent 2adbd915f7
commit d81c97052d
7 changed files with 51 additions and 41 deletions

View file

@ -33,33 +33,30 @@ module Epp::Common
end end
def handle_epp_errors(error_code_map, obj) def handle_epp_errors(error_code_map, obj)
obj.errors.each do |key, err| obj.errors.each do |key, msg|
error_code_map.each do |code, values| if msg.is_a?(Hash)
has_error = Proc.new do |x|
if x.is_a?(Array)
obj.errors.generate_message(key, x[0], x[1]) == err
else
obj.errors.generate_message(key, x) == err
end
end
if err.is_a?(Hash)
epp_errors << { epp_errors << {
code: code, code: find_code(msg[:msg]),
msg: err[:msg], msg: msg[:msg],
value: {obj: err[:obj], val: err[:val]}, value: {obj: msg[:obj], val: msg[:val]},
} and break if values.any? {|x| obj.errors.generate_message(key, x) == err[:msg]} }
else else
epp_errors << { next unless code = find_code(msg)
code: code, epp_errors << {
msg: err, code: code,
} and break if values.any? {|x| has_error.call(x)} msg: msg
end }
end end
end end
end end
def find_code(msg)
error_code_map.each do |code, values|
return code if values.include?(msg)
end
nil
end
def validate_request def validate_request
type = OBJECT_TYPES[params_hash['epp']['xmlns:ns2']] type = OBJECT_TYPES[params_hash['epp']['xmlns:ns2']]
return unless type return unless type

View file

@ -60,7 +60,7 @@ module Epp::DomainsHelper
def handle_errors def handle_errors
handle_epp_errors({ handle_epp_errors({
'2302' => [:epp_domain_taken, :epp_domain_reserved], '2302' => [:epp_domain_taken, :reserved],
'2306' => [:blank, [:out_of_range, {min: 1, max: 13}]], '2306' => [:blank, [:out_of_range, {min: 1, max: 13}]],
'2303' => [:not_found], '2303' => [:not_found],
'2005' => [:hostname_invalid, :ip_invalid] '2005' => [:hostname_invalid, :ip_invalid]
@ -68,4 +68,13 @@ module Epp::DomainsHelper
) )
end end
def error_code_map
{
'2302' => ['Domain name already exists', 'Domain name is reserved or restricted'],
'2306' => ['Registrant is missing', 'Nameservers count must be between 1-13', 'Admin contact is missing'],
'2303' => ['Contact was not found'],
'2005' => ['Hostname is invalid', 'IP is invalid']
}
end
end end

View file

@ -17,7 +17,7 @@ class Domain < ActiveRecord::Base
has_and_belongs_to_many :nameservers has_and_belongs_to_many :nameservers
validates :name_dirty, domain_name: true, uniqueness: { message: :epp_domain_taken } validates :name_dirty, domain_name: true, uniqueness: true
validates :period, numericality: { only_integer: true, greater_than: 0, less_than: 100 } validates :period, numericality: { only_integer: true, greater_than: 0, less_than: 100 }
validates :name, :owner_contact, presence: true validates :name, :owner_contact, presence: true
validates_associated :nameservers validates_associated :nameservers
@ -113,8 +113,6 @@ class Domain < ActiveRecord::Base
end end
def validate_nameservers_count def validate_nameservers_count
errors.add(:nameservers, :blank) if nameservers.empty?
unless nameservers.count.between?(1, 13) unless nameservers.count.between?(1, 13)
errors.add(:nameservers, :out_of_range, {min: 1, max: 13}) errors.add(:nameservers, :out_of_range, {min: 1, max: 13})
end end

View file

@ -10,7 +10,7 @@ class DomainNameValidator < ActiveModel::EachValidator
if !self.class.validate_format(value) if !self.class.validate_format(value)
record.errors[attribute] << (options[:message] || 'invalid format') record.errors[attribute] << (options[:message] || 'invalid format')
elsif !self.class.validate_reservation(value) elsif !self.class.validate_reservation(value)
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.epp_domain_reserved')) record.errors.add(attribute, (options[:message] || :reserved))
end end
end end

View file

@ -37,17 +37,17 @@ en:
blank: "Required parameter missing - ident" blank: "Required parameter missing - ident"
domain: domain:
attributes: attributes:
name: name_dirty:
blank: 'Required parameter missing - name' reserved: 'Domain name is reserved or restricted'
taken: 'Domain name already exists'
owner_contact: owner_contact:
blank: 'Required parameter missing - registrant' blank: 'Registrant is missing'
admin_contacts:
blank: 'Required parameter missing - admin contact'
domain_contacts: domain_contacts:
not_found: 'Contact was not found' not_found: 'Contact was not found'
admin_contacts:
blank: 'Admin contact is missing'
nameservers: nameservers:
blank: 'Required parameter missing - nameserver' out_of_range: 'Nameservers count must be between %{min}-%{max}'
out_of_range: 'Domain must have %{min}-%{max} nameservers'
hostname_invalid: 'Hostname is invalid' hostname_invalid: 'Hostname is invalid'
ip_invalid: 'IP is invalid' ip_invalid: 'IP is invalid'
nameserver: nameserver:
@ -56,10 +56,16 @@ en:
invalid: 'Hostname is invalid' invalid: 'Hostname is invalid'
ip: ip:
invalid: 'IP is invalid' invalid: 'IP is invalid'
attributes:
domain:
name: 'Domain name'
name_dirty: 'Domain name'
name_puny: 'Domain name'
owner_contact: 'Registrant'
errors: errors:
messages: messages:
blank: 'is missing'
epp_domain_reserved: 'Domain name is reserved or restricted' epp_domain_reserved: 'Domain name is reserved or restricted'
epp_domain_taken: 'Domain name already exists' epp_domain_taken: 'Domain name already exists'
epp_obj_does_not_exist: 'Object does not exist' epp_obj_does_not_exist: 'Object does not exist'

View file

@ -64,19 +64,19 @@ describe 'EPP Domain', epp: true do
it 'does not create domain without contacts and registrant' do it 'does not create domain without contacts and registrant' do
response = epp_request('domains/create_wo_contacts_and_registrant.xml') response = epp_request('domains/create_wo_contacts_and_registrant.xml')
expect(response[:result_code]).to eq('2306') expect(response[:result_code]).to eq('2306')
expect(response[:msg]).to eq('Required parameter missing - registrant') expect(response[:msg]).to eq('Registrant is missing')
end end
it 'does not create domain without nameservers' do it 'does not create domain without nameservers' do
response = epp_request('domains/create_wo_nameservers.xml') response = epp_request('domains/create_wo_nameservers.xml')
expect(response[:result_code]).to eq('2306') expect(response[:result_code]).to eq('2306')
expect(response[:msg]).to eq('Required parameter missing - nameserver') expect(response[:msg]).to eq('Nameservers count must be between 1-13')
end end
it 'does not create domain with too many nameservers' do it 'does not create domain with too many nameservers' do
response = epp_request('domains/create_w_too_many_nameservers.xml') response = epp_request('domains/create_w_too_many_nameservers.xml')
expect(response[:result_code]).to eq('2306') expect(response[:result_code]).to eq('2306')
expect(response[:msg]).to eq('Domain must have 1-13 nameservers') expect(response[:msg]).to eq('Nameservers count must be between 1-13')
end end
it 'returns error when invalid nameservers are present' do it 'returns error when invalid nameservers are present' do
@ -123,7 +123,7 @@ describe 'EPP Domain', epp: true do
it 'does not create a domain without admin contact' do it 'does not create a domain without admin contact' do
response = epp_request('domains/create_wo_contacts.xml') response = epp_request('domains/create_wo_contacts.xml')
expect(response[:result_code]).to eq('2306') expect(response[:result_code]).to eq('2306')
expect(response[:msg]).to eq('Required parameter missing - admin contact') expect(response[:msg]).to eq('Admin contact is missing')
expect(response[:clTRID]).to eq('ABC-12345') expect(response[:clTRID]).to eq('ABC-12345')
expect(Domain.count).to eq 0 expect(Domain.count).to eq 0

View file

@ -39,9 +39,9 @@ describe Domain do
expect(d.valid?).to be false expect(d.valid?).to be false
expect(d.errors.messages).to match_array({ expect(d.errors.messages).to match_array({
name: ['Required parameter missing - name'], name: ['is missing'],
period: ['is not a number'], period: ['is not a number'],
owner_contact: ['Required parameter missing - registrant'] owner_contact: ["Registrant is missing"]
}) })
end end