Add error structuring to Grape API

This commit is contained in:
Karl Erik Õunapuu 2020-10-07 14:17:53 +03:00
parent 01004b8ed4
commit dc8551807a
No known key found for this signature in database
GPG key ID: C9DD647298A34764
3 changed files with 31 additions and 9 deletions

View file

@ -3,6 +3,14 @@ module Repp
format :json format :json
prefix :repp 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| http_basic do |username, password|
@current_user ||= ApiUser.find_by(username: username, plain_text_password: password) @current_user ||= ApiUser.find_by(username: username, plain_text_password: password)
if @current_user if @current_user

View file

@ -36,15 +36,18 @@ module Repp
requires :contact, type: Hash, allow_blank: false do requires :contact, type: Hash, allow_blank: false do
# Contact info # Contact info
requires :name, type: String, desc: 'Full name of contact' 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' requires :email, type: String, desc: 'Email address of contact'
optional :fax, type: String, allow_blank: true, desc: 'Fax number of contact' optional :fax, type: String, allow_blank: true, desc: 'Fax number of contact'
# Ident # Ident
requires :ident, type: Hash do 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_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 end
# Physical address # Physical address
@ -72,18 +75,29 @@ module Repp
@contact_params.delete(:ident) @contact_params.delete(:ident)
# Address # Address
address_present = params[:contact][:addr].keys.any?
%w[city street zip country_code].each { |k| @contact_params[k] = @contact_params[:addr][k] } %w[city street zip country_code].each { |k| @contact_params[k] = @contact_params[:addr][k] }
@contact_params.delete(:addr) @contact_params.delete(:addr)
@contact = Epp::Contact.new(@contact_params, current_user.registrar, epp: false) @contact = Epp::Contact.new(@contact_params, current_user.registrar, epp: false)
puts "#{params[:contact]}"
action = Actions::ContactCreate.new(@contact, @legal_doc, @ident) action = Actions::ContactCreate.new(@contact, @legal_doc, @ident)
if action.call 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 else
status :bad_request @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)
@response = { errors: @contact.errors } @response = { errors: @contact.errors }
end end
end end
@ -98,7 +112,8 @@ module Repp
end end
optional :name, type: String, desc: 'Full name of contact' optional :name, type: String, desc: 'Full name of contact'
optional :country_code, type: String, desc: 'Address country' 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 :email, type: String, desc: 'Email address of contact'
optional :fax, type: String, desc: 'Fax number of contact' optional :fax, type: String, desc: 'Fax number of contact'
optional :street, type: String, desc: 'Address street' optional :street, type: String, desc: 'Address street'
@ -125,7 +140,7 @@ module Repp
if action.call if action.call
@response = {} @response = {}
else else
status :bad_request status(:bad_request)
@response = { errors: @contact.errors } @response = { errors: @contact.errors }
end end
end end

View file

@ -24,7 +24,6 @@ module Epp
@contact = Epp::Contact.new(params[:parsed_frame], current_user.registrar) @contact = Epp::Contact.new(params[:parsed_frame], current_user.registrar)
collected_data = ::Deserializers::Xml::ContactCreate.new(params[:parsed_frame]) collected_data = ::Deserializers::Xml::ContactCreate.new(params[:parsed_frame])
action = Actions::ContactCreate.new(@contact, collected_data.legal_document, collected_data.ident) action = Actions::ContactCreate.new(@contact, collected_data.legal_document, collected_data.ident)
if action.call if action.call