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
def handle_epp_errors(error_code_map, obj)
obj.errors.each do |key, err|
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)
obj.errors.each do |key, msg|
if msg.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
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|
return code if values.include?(msg)
end
nil
end
def validate_request
type = OBJECT_TYPES[params_hash['epp']['xmlns:ns2']]
return unless type

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