mirror of
https://github.com/internetee/registry.git
synced 2025-07-27 04:58:29 +02:00
New errors refactor
This commit is contained in:
parent
a8e0cba9d3
commit
b944516f17
3 changed files with 73 additions and 53 deletions
66
app/models/concerns/epp_errors.rb
Normal file
66
app/models/concerns/epp_errors.rb
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
module EppErrors
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
|
||||||
|
EPP_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']
|
||||||
|
}
|
||||||
|
|
||||||
|
EPP_OBJ_MAP = {
|
||||||
|
hostname: 'ns',
|
||||||
|
name_dirty: 'domain'
|
||||||
|
}
|
||||||
|
|
||||||
|
def construct_epp_errors
|
||||||
|
epp_errors = []
|
||||||
|
errors.messages.each do |key, values|
|
||||||
|
if self.class.reflect_on_association(key)
|
||||||
|
epp_errors = collect_child_errors(key, values)
|
||||||
|
else
|
||||||
|
epp_errors = collect_parent_errors(key, values)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
errors[:epp_errors] = epp_errors
|
||||||
|
errors[:epp_errors].flatten!
|
||||||
|
end
|
||||||
|
|
||||||
|
def collect_parent_errors(key, values)
|
||||||
|
epp_errors = []
|
||||||
|
values = [values] if values.is_a?(String)
|
||||||
|
|
||||||
|
values.each do |err|
|
||||||
|
if err.is_a?(Hash)
|
||||||
|
epp_errors << {code: find_epp_code(err[:msg]), msg: err[:msg], value: {val: err[:val], obj: err[:obj]}}
|
||||||
|
else
|
||||||
|
epp_errors << {code: find_epp_code(err), msg: err, value: {val: send(key), obj: EPP_OBJ_MAP[key]}}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
epp_errors
|
||||||
|
end
|
||||||
|
|
||||||
|
def collect_child_errors(key, values)
|
||||||
|
macro = self.class.reflect_on_association(key).macro
|
||||||
|
multi = [:has_and_belongs_to_many, :has_many]
|
||||||
|
single = [:belongs_to, :has_one]
|
||||||
|
|
||||||
|
epp_errors = []
|
||||||
|
send(key).each do |x|
|
||||||
|
x.errors.messages.each do |key, values|
|
||||||
|
epp_errors << x.collect_parent_errors(key, values)
|
||||||
|
end
|
||||||
|
end if multi.include?(macro)
|
||||||
|
|
||||||
|
epp_errors
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_epp_code(msg)
|
||||||
|
EPP_CODE_MAP.each do |code, values|
|
||||||
|
return code if values.include?(msg)
|
||||||
|
end
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
|
@ -2,6 +2,10 @@ class Domain < ActiveRecord::Base
|
||||||
#TODO whois requests ip whitelist for full info for own domains and partial info for other domains
|
#TODO whois requests ip whitelist for full info for own domains and partial info for other domains
|
||||||
#TODO most inputs should be trimmed before validatation, probably some global logic?
|
#TODO most inputs should be trimmed before validatation, probably some global logic?
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
include EppErrors
|
||||||
|
|
||||||
belongs_to :registrar
|
belongs_to :registrar
|
||||||
belongs_to :owner_contact, class_name: 'Contact'
|
belongs_to :owner_contact, class_name: 'Contact'
|
||||||
|
|
||||||
|
@ -26,58 +30,6 @@ class Domain < ActiveRecord::Base
|
||||||
|
|
||||||
attr_accessor :epp_errors
|
attr_accessor :epp_errors
|
||||||
|
|
||||||
EPP_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']
|
|
||||||
}
|
|
||||||
|
|
||||||
EPP_OBJ_MAP = {
|
|
||||||
nameservers: 'ns',
|
|
||||||
name_dirty: 'domain'
|
|
||||||
}
|
|
||||||
|
|
||||||
def construct_epp_errors
|
|
||||||
epp_errors = []
|
|
||||||
errors.messages.each do |key, values|
|
|
||||||
if self.class.reflect_on_association(key)
|
|
||||||
epp_errors = collect_child_errors
|
|
||||||
else
|
|
||||||
epp_errors = collect_parent_errors(key, values)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
errors[:epp_errors] = epp_errors
|
|
||||||
errors[:epp_errors].flatten!
|
|
||||||
# binding.pry
|
|
||||||
end
|
|
||||||
|
|
||||||
def collect_parent_errors(key, values)
|
|
||||||
epp_errors = []
|
|
||||||
values = [values] if values.is_a?(String)
|
|
||||||
|
|
||||||
values.each do |err|
|
|
||||||
if err.is_a?(Hash)
|
|
||||||
epp_errors << {code: find_epp_code(err[:msg]), msg: err[:msg], value: {val: err[:val], obj: err[:obj]}}
|
|
||||||
else
|
|
||||||
epp_errors << {code: find_epp_code(err), msg: err, value: {val: send(key), obj: EPP_OBJ_MAP[key]}}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
epp_errors
|
|
||||||
end
|
|
||||||
|
|
||||||
def collect_child_errors
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_epp_code(msg)
|
|
||||||
EPP_CODE_MAP.each do |code, values|
|
|
||||||
return code if values.include?(msg)
|
|
||||||
end
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def name=(value)
|
def name=(value)
|
||||||
value.strip!
|
value.strip!
|
||||||
write_attribute(:name, SimpleIDN.to_unicode(value))
|
write_attribute(:name, SimpleIDN.to_unicode(value))
|
||||||
|
@ -125,7 +77,7 @@ class Domain < ActiveRecord::Base
|
||||||
|
|
||||||
save
|
save
|
||||||
|
|
||||||
add_child_collection_errors(:nameservers, :ns)
|
# add_child_collection_errors(:nameservers, :ns)
|
||||||
|
|
||||||
validate_nameservers_count
|
validate_nameservers_count
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
class Nameserver < ActiveRecord::Base
|
class Nameserver < ActiveRecord::Base
|
||||||
|
include EppErrors
|
||||||
|
|
||||||
belongs_to :registrar
|
belongs_to :registrar
|
||||||
has_and_belongs_to_many :domains
|
has_and_belongs_to_many :domains
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue