From dc8551807a24f6d4d6550c8933e60206c9f4e76d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Wed, 7 Oct 2020 14:17:53 +0300 Subject: [PATCH] Add error structuring to Grape API --- app/api/repp/api.rb | 8 ++++++ app/api/repp/contact_v1.rb | 31 ++++++++++++++++------ app/controllers/epp/contacts_controller.rb | 1 - 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/api/repp/api.rb b/app/api/repp/api.rb index af6864cfa..f1908f81e 100644 --- a/app/api/repp/api.rb +++ b/app/api/repp/api.rb @@ -3,6 +3,14 @@ module Repp format :json prefix :repp + rescue_from Grape::Exceptions::ValidationErrors do |e| + messages = e.full_messages + errors = [] + messages.each { |m| errors << { code: 2003, message: m } } + + error!({ errors: errors }, 400) + end + http_basic do |username, password| @current_user ||= ApiUser.find_by(username: username, plain_text_password: password) if @current_user diff --git a/app/api/repp/contact_v1.rb b/app/api/repp/contact_v1.rb index 6a3cae33b..85065f5a0 100644 --- a/app/api/repp/contact_v1.rb +++ b/app/api/repp/contact_v1.rb @@ -36,15 +36,18 @@ module Repp requires :contact, type: Hash, allow_blank: false do # Contact info requires :name, type: String, desc: 'Full name of contact' - requires :phone, type: String, desc: 'Phone number of contact. In format of +country_prefix.number' + requires :phone, type: String, + desc: 'Phone number of contact. In format of +country_prefix.number' requires :email, type: String, desc: 'Email address of contact' optional :fax, type: String, allow_blank: true, desc: 'Fax number of contact' # Ident requires :ident, type: Hash do - requires :ident, type: String, allow_blank: false, desc: 'Government identifier of contact' + requires :ident, type: String, allow_blank: false, + desc: 'Government identifier of contact' requires :ident_type, type: String, allow_blank: false, desc: 'Type of contact ident' - requires :ident_country_code, type: String, allow_blank: false, desc: 'Ident country code' + requires :ident_country_code, type: String, allow_blank: false, + desc: 'Ident country code' end # Physical address @@ -72,18 +75,29 @@ module Repp @contact_params.delete(:ident) # Address + address_present = params[:contact][:addr].keys.any? + %w[city street zip country_code].each { |k| @contact_params[k] = @contact_params[:addr][k] } @contact_params.delete(:addr) @contact = Epp::Contact.new(@contact_params, current_user.registrar, epp: false) - puts "#{params[:contact]}" action = Actions::ContactCreate.new(@contact, @legal_doc, @ident) if action.call - @response = { contact: { id: @contact.code } } + if !Contact.address_processing? && address_present + @response_code = 1100 + @response_description = I18n.t('epp.contacts.completed_without_address') + else + @response_code = 1000 + @response_description = I18n.t('epp.contacts.completed') + end + + @response = { code: @response_code, + description: @response_description, + data: { contact: { id: @contact.code } } } else - status :bad_request + status(:bad_request) @response = { errors: @contact.errors } end end @@ -98,7 +112,8 @@ module Repp end optional :name, type: String, desc: 'Full name of contact' optional :country_code, type: String, desc: 'Address country' - optional :phone, type: String, desc: 'Phone number of contact. In format of +country_prefix.number' + optional :phone, type: String, + desc: 'Phone number of contact. In format of +country_prefix.number' optional :email, type: String, desc: 'Email address of contact' optional :fax, type: String, desc: 'Fax number of contact' optional :street, type: String, desc: 'Address street' @@ -125,7 +140,7 @@ module Repp if action.call @response = {} else - status :bad_request + status(:bad_request) @response = { errors: @contact.errors } end end diff --git a/app/controllers/epp/contacts_controller.rb b/app/controllers/epp/contacts_controller.rb index cfaaeb89e..b674d0919 100644 --- a/app/controllers/epp/contacts_controller.rb +++ b/app/controllers/epp/contacts_controller.rb @@ -24,7 +24,6 @@ module Epp @contact = Epp::Contact.new(params[:parsed_frame], current_user.registrar) collected_data = ::Deserializers::Xml::ContactCreate.new(params[:parsed_frame]) - action = Actions::ContactCreate.new(@contact, collected_data.legal_document, collected_data.ident) if action.call