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,31 +33,28 @@ module Epp::Common
end
def handle_epp_errors(error_code_map, obj)
obj.errors.each do |key, err|
obj.errors.each do |key, msg|
if msg.is_a?(Hash)
epp_errors << {
code: find_code(msg[:msg]),
msg: msg[:msg],
value: {obj: msg[:obj], val: msg[:val]},
}
else
next unless code = find_code(msg)
epp_errors << {
code: code,
msg: msg
}
end
end
end
def find_code(msg)
error_code_map.each do |code, values|
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 << {
code: code,
msg: err[:msg],
value: {obj: err[:obj], val: err[:val]},
} and break if values.any? {|x| obj.errors.generate_message(key, x) == err[:msg]}
else
epp_errors << {
code: code,
msg: err,
} and break if values.any? {|x| has_error.call(x)}
end
end
return code if values.include?(msg)
end
nil
end
def validate_request

View file

@ -60,7 +60,7 @@ module Epp::DomainsHelper
def handle_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}]],
'2303' => [:not_found],
'2005' => [:hostname_invalid, :ip_invalid]
@ -68,4 +68,13 @@ module Epp::DomainsHelper
)
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

View file

@ -17,7 +17,7 @@ class Domain < ActiveRecord::Base
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 :name, :owner_contact, presence: true
validates_associated :nameservers
@ -113,8 +113,6 @@ class Domain < ActiveRecord::Base
end
def validate_nameservers_count
errors.add(:nameservers, :blank) if nameservers.empty?
unless nameservers.count.between?(1, 13)
errors.add(:nameservers, :out_of_range, {min: 1, max: 13})
end

View file

@ -10,7 +10,7 @@ class DomainNameValidator < ActiveModel::EachValidator
if !self.class.validate_format(value)
record.errors[attribute] << (options[:message] || 'invalid format')
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

View file

@ -37,17 +37,17 @@ en:
blank: "Required parameter missing - ident"
domain:
attributes:
name:
blank: 'Required parameter missing - name'
name_dirty:
reserved: 'Domain name is reserved or restricted'
taken: 'Domain name already exists'
owner_contact:
blank: 'Required parameter missing - registrant'
admin_contacts:
blank: 'Required parameter missing - admin contact'
blank: 'Registrant is missing'
domain_contacts:
not_found: 'Contact was not found'
admin_contacts:
blank: 'Admin contact is missing'
nameservers:
blank: 'Required parameter missing - nameserver'
out_of_range: 'Domain must have %{min}-%{max} nameservers'
out_of_range: 'Nameservers count must be between %{min}-%{max}'
hostname_invalid: 'Hostname is invalid'
ip_invalid: 'IP is invalid'
nameserver:
@ -56,10 +56,16 @@ en:
invalid: 'Hostname is invalid'
ip:
invalid: 'IP is invalid'
attributes:
domain:
name: 'Domain name'
name_dirty: 'Domain name'
name_puny: 'Domain name'
owner_contact: 'Registrant'
errors:
messages:
blank: 'is missing'
epp_domain_reserved: 'Domain name is reserved or restricted'
epp_domain_taken: 'Domain name already exists'
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
response = epp_request('domains/create_wo_contacts_and_registrant.xml')
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
it 'does not create domain without nameservers' do
response = epp_request('domains/create_wo_nameservers.xml')
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
it 'does not create domain with too many nameservers' do
response = epp_request('domains/create_w_too_many_nameservers.xml')
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
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
response = epp_request('domains/create_wo_contacts.xml')
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(Domain.count).to eq 0

View file

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