mirror of
https://github.com/internetee/registry.git
synced 2025-05-22 04:09:52 +02:00
Refactor epp errors to concern
This commit is contained in:
parent
b944516f17
commit
6b86af3048
4 changed files with 15 additions and 65 deletions
|
@ -32,29 +32,10 @@ module Epp::Common
|
||||||
@current_epp_user ||= EppUser.find(epp_session[:epp_user_id]) if epp_session[:epp_user_id]
|
@current_epp_user ||= EppUser.find(epp_session[:epp_user_id]) if epp_session[:epp_user_id]
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_epp_errors(error_code_map, obj)
|
def handle_errors(obj)
|
||||||
obj.errors.each do |key, msg|
|
obj.construct_epp_errors
|
||||||
if msg.is_a?(Hash)
|
@errors = obj.errors[:epp_errors]
|
||||||
epp_errors << {
|
render '/epp/error'
|
||||||
code: find_code(error_code_map, msg[:msg]),
|
|
||||||
msg: msg[:msg],
|
|
||||||
value: {obj: msg[:obj], val: msg[:val]},
|
|
||||||
}
|
|
||||||
else
|
|
||||||
next unless code = find_code(error_code_map, msg)
|
|
||||||
epp_errors << {
|
|
||||||
code: code,
|
|
||||||
msg: msg
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_code(error_code_map, msg)
|
|
||||||
error_code_map.each do |code, values|
|
|
||||||
return code if values.include?(msg)
|
|
||||||
end
|
|
||||||
nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_request
|
def validate_request
|
||||||
|
|
|
@ -6,10 +6,7 @@ module Epp::DomainsHelper
|
||||||
if @domain.save && @domain.attach_contacts(domain_contacts) && @domain.attach_nameservers(domain_nameservers)
|
if @domain.save && @domain.attach_contacts(domain_contacts) && @domain.attach_nameservers(domain_nameservers)
|
||||||
render '/epp/domains/create'
|
render '/epp/domains/create'
|
||||||
else
|
else
|
||||||
@domain.construct_epp_errors
|
handle_errors(@domain)
|
||||||
|
|
||||||
handle_errors
|
|
||||||
render '/epp/error'
|
|
||||||
raise ActiveRecord::Rollback
|
raise ActiveRecord::Rollback
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -76,13 +73,4 @@ module Epp::DomainsHelper
|
||||||
return ph[:hostAttr] if ph[:hostAttr]
|
return ph[:hostAttr] if ph[:hostAttr]
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_errors
|
|
||||||
handle_epp_errors({
|
|
||||||
'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']
|
|
||||||
}, @domain)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
module EppErrors
|
module EppErrors
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
|
||||||
EPP_CODE_MAP = {
|
EPP_CODE_MAP = {
|
||||||
'2302' => ['Domain name already exists', 'Domain name is reserved or restricted'],
|
'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'],
|
'2306' => ['Registrant is missing', 'Nameservers count must be between 1-13', 'Admin contact is missing'],
|
||||||
|
@ -18,10 +17,10 @@ module EppErrors
|
||||||
epp_errors = []
|
epp_errors = []
|
||||||
errors.messages.each do |key, values|
|
errors.messages.each do |key, values|
|
||||||
if self.class.reflect_on_association(key)
|
if self.class.reflect_on_association(key)
|
||||||
epp_errors = collect_child_errors(key, values)
|
epp_errors << collect_child_errors(key)
|
||||||
else
|
|
||||||
epp_errors = collect_parent_errors(key, values)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
epp_errors << collect_parent_errors(key, values)
|
||||||
end
|
end
|
||||||
|
|
||||||
errors[:epp_errors] = epp_errors
|
errors[:epp_errors] = epp_errors
|
||||||
|
@ -34,15 +33,19 @@ module EppErrors
|
||||||
|
|
||||||
values.each do |err|
|
values.each do |err|
|
||||||
if err.is_a?(Hash)
|
if err.is_a?(Hash)
|
||||||
epp_errors << {code: find_epp_code(err[:msg]), msg: err[:msg], value: {val: err[:val], obj: err[:obj]}}
|
next unless code = find_epp_code(err[:msg])
|
||||||
|
epp_errors << {code: code, msg: err[:msg], value: {val: err[:val], obj: err[:obj]}}
|
||||||
else
|
else
|
||||||
epp_errors << {code: find_epp_code(err), msg: err, value: {val: send(key), obj: EPP_OBJ_MAP[key]}}
|
next unless code = find_epp_code(err)
|
||||||
|
err = {code: code, msg: err}
|
||||||
|
err[:value] = {val: send(key), obj: EPP_OBJ_MAP[key]} unless self.class.reflect_on_association(key)
|
||||||
|
epp_errors << err
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
epp_errors
|
epp_errors
|
||||||
end
|
end
|
||||||
|
|
||||||
def collect_child_errors(key, values)
|
def collect_child_errors(key)
|
||||||
macro = self.class.reflect_on_association(key).macro
|
macro = self.class.reflect_on_association(key).macro
|
||||||
multi = [:has_and_belongs_to_many, :has_many]
|
multi = [:has_and_belongs_to_many, :has_many]
|
||||||
single = [:belongs_to, :has_one]
|
single = [:belongs_to, :has_one]
|
||||||
|
|
|
@ -2,8 +2,6 @@ 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
|
include EppErrors
|
||||||
|
|
||||||
belongs_to :registrar
|
belongs_to :registrar
|
||||||
|
@ -77,8 +75,6 @@ class Domain < ActiveRecord::Base
|
||||||
|
|
||||||
save
|
save
|
||||||
|
|
||||||
# add_child_collection_errors(:nameservers, :ns)
|
|
||||||
|
|
||||||
validate_nameservers_count
|
validate_nameservers_count
|
||||||
|
|
||||||
errors.empty?
|
errors.empty?
|
||||||
|
@ -102,24 +98,6 @@ class Domain < ActiveRecord::Base
|
||||||
self.nameservers.build(attrs)
|
self.nameservers.build(attrs)
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_child_collection_errors(attr_key, epp_obj_name)
|
|
||||||
send(attr_key).each do |obj|
|
|
||||||
obj.errors.keys.each do |key|
|
|
||||||
add_errors_with_value(attr_key, epp_obj_name, obj, key)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def add_errors_with_value(attr_key, epp_obj_name, obj, key)
|
|
||||||
obj.errors[key].each do |x|
|
|
||||||
errors.add(attr_key, {
|
|
||||||
obj: epp_obj_name,
|
|
||||||
val: obj.send(key),
|
|
||||||
msg: x
|
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate_nameservers_count
|
def validate_nameservers_count
|
||||||
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})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue