mirror of
https://github.com/internetee/registry.git
synced 2025-07-27 21:16:12 +02:00
Fixed codeclimate issues
This commit is contained in:
parent
65ae5adbb8
commit
cbe78e505b
3 changed files with 84 additions and 74 deletions
|
@ -9,8 +9,8 @@ module EppRequestable
|
||||||
authorize! :create, Epp::Server
|
authorize! :create, Epp::Server
|
||||||
result = server.request(request_params[:payload])
|
result = server.request(request_params[:payload])
|
||||||
render_success(data: { xml: result.force_encoding('UTF-8') })
|
render_success(data: { xml: result.force_encoding('UTF-8') })
|
||||||
rescue StandardError
|
rescue StandardError => e
|
||||||
handle_non_epp_errors(nil, I18n.t('errors.messages.epp_conn_error'))
|
handle_non_epp_errors(nil, e.message.presence || I18n.t('errors.messages.epp_conn_error'))
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
80
app/controllers/concerns/error_and_log_handler.rb
Normal file
80
app/controllers/concerns/error_and_log_handler.rb
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
module ErrorAndLogHandler
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
included do
|
||||||
|
around_action :log_request
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
# rubocop:disable Metrics/MethodLength
|
||||||
|
def log_request
|
||||||
|
yield
|
||||||
|
rescue ActiveRecord::RecordNotFound
|
||||||
|
handle_record_not_found
|
||||||
|
rescue ActionController::ParameterMissing, Apipie::ParamMissing => e
|
||||||
|
handle_parameter_missing(e)
|
||||||
|
rescue Apipie::ParamInvalid => e
|
||||||
|
handle_param_invalid(e)
|
||||||
|
rescue CanCan::AccessDenied => e
|
||||||
|
handle_access_denied(e)
|
||||||
|
rescue Shunter::ThrottleError => e
|
||||||
|
handle_throttle_error(e)
|
||||||
|
ensure
|
||||||
|
create_repp_log
|
||||||
|
end
|
||||||
|
# rubocop:enable Metrics/MethodLength
|
||||||
|
|
||||||
|
def handle_record_not_found
|
||||||
|
@response = { code: 2303, message: 'Object does not exist' }
|
||||||
|
render(json: @response, status: :not_found)
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_parameter_missing(error)
|
||||||
|
@response = { code: 2003, message: error.message.gsub(/\n/, '. ') }
|
||||||
|
render(json: @response, status: :bad_request)
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_param_invalid(error)
|
||||||
|
@response = { code: 2005, message: error.message.gsub(/\n/, '. ') }
|
||||||
|
render(json: @response, status: :bad_request)
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_access_denied(error)
|
||||||
|
@response = { code: 2201, message: 'Authorization error' }
|
||||||
|
logger.error error.to_s
|
||||||
|
render(json: @response, status: :unauthorized)
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_throttle_error(error)
|
||||||
|
@response = { code: 2502, message: Shunter.default_error_message }
|
||||||
|
logger.error error.to_s unless Rails.env.test?
|
||||||
|
render(json: @response, status: :bad_request)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_repp_log
|
||||||
|
log_attributes = build_log_attributes
|
||||||
|
ApiLog::ReppLog.create(log_attributes)
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_log_attributes
|
||||||
|
{
|
||||||
|
request_path: request.path, ip: request.ip,
|
||||||
|
request_method: request.request_method,
|
||||||
|
request_params: build_request_params_json,
|
||||||
|
uuid: request.try(:uuid),
|
||||||
|
response: @response.to_json,
|
||||||
|
response_code: response.status,
|
||||||
|
api_user_name: current_user.try(:username),
|
||||||
|
api_user_registrar: current_user.try(:registrar).try(:to_s)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_request_params_json
|
||||||
|
request.params.except('route_info').to_json
|
||||||
|
end
|
||||||
|
|
||||||
|
def logger
|
||||||
|
Rails.logger
|
||||||
|
end
|
||||||
|
end
|
|
@ -3,7 +3,8 @@ module Repp
|
||||||
class BaseController < ActionController::API # rubocop:disable Metrics/ClassLength
|
class BaseController < ActionController::API # rubocop:disable Metrics/ClassLength
|
||||||
attr_reader :current_user
|
attr_reader :current_user
|
||||||
|
|
||||||
around_action :log_request
|
include ErrorAndLogHandler
|
||||||
|
|
||||||
before_action :authenticate_user
|
before_action :authenticate_user
|
||||||
before_action :set_locale
|
before_action :set_locale
|
||||||
before_action :validate_webclient_ca
|
before_action :validate_webclient_ca
|
||||||
|
@ -13,73 +14,6 @@ module Repp
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# rubocop:disable Metrics/MethodLength
|
|
||||||
def log_request
|
|
||||||
yield
|
|
||||||
rescue ActiveRecord::RecordNotFound
|
|
||||||
handle_record_not_found
|
|
||||||
rescue ActionController::ParameterMissing, Apipie::ParamMissing => e
|
|
||||||
handle_parameter_missing(e)
|
|
||||||
rescue Apipie::ParamInvalid => e
|
|
||||||
handle_param_invalid(e)
|
|
||||||
rescue CanCan::AccessDenied => e
|
|
||||||
handle_access_denied(e)
|
|
||||||
rescue Shunter::ThrottleError => e
|
|
||||||
handle_throttle_error(e)
|
|
||||||
ensure
|
|
||||||
create_repp_log
|
|
||||||
end
|
|
||||||
# rubocop:enable Metrics/MethodLength
|
|
||||||
|
|
||||||
def handle_record_not_found
|
|
||||||
@response = { code: 2303, message: 'Object does not exist' }
|
|
||||||
render(json: @response, status: :not_found)
|
|
||||||
end
|
|
||||||
|
|
||||||
def handle_parameter_missing(e)
|
|
||||||
@response = { code: 2003, message: e.message.gsub(/\n/, '. ') }
|
|
||||||
render(json: @response, status: :bad_request)
|
|
||||||
end
|
|
||||||
|
|
||||||
def handle_param_invalid(e)
|
|
||||||
@response = { code: 2005, message: e.message.gsub(/\n/, '. ') }
|
|
||||||
render(json: @response, status: :bad_request)
|
|
||||||
end
|
|
||||||
|
|
||||||
def handle_access_denied(e)
|
|
||||||
@response = { code: 2201, message: 'Authorization error' }
|
|
||||||
logger.error e.to_s
|
|
||||||
render(json: @response, status: :unauthorized)
|
|
||||||
end
|
|
||||||
|
|
||||||
def handle_throttle_error(e)
|
|
||||||
@response = { code: 2502, message: Shunter.default_error_message }
|
|
||||||
logger.error e.to_s unless Rails.env.test?
|
|
||||||
render(json: @response, status: :bad_request)
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_repp_log
|
|
||||||
log_attributes = build_log_attributes
|
|
||||||
ApiLog::ReppLog.create(log_attributes)
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_log_attributes
|
|
||||||
{
|
|
||||||
request_path: request.path, ip: request.ip,
|
|
||||||
request_method: request.request_method,
|
|
||||||
request_params: build_request_params_json,
|
|
||||||
uuid: request.try(:uuid),
|
|
||||||
response: @response.to_json,
|
|
||||||
response_code: response.status,
|
|
||||||
api_user_name: current_user.try(:username),
|
|
||||||
api_user_registrar: current_user.try(:registrar).try(:to_s)
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_request_params_json
|
|
||||||
request.params.except('route_info').to_json
|
|
||||||
end
|
|
||||||
|
|
||||||
def set_domain
|
def set_domain
|
||||||
registrar = current_user.registrar
|
registrar = current_user.registrar
|
||||||
@domain = Epp::Domain.find_by(registrar: registrar, name: params[:domain_id])
|
@domain = Epp::Domain.find_by(registrar: registrar, name: params[:domain_id])
|
||||||
|
@ -234,10 +168,6 @@ module Repp
|
||||||
request.headers['Requester'] == 'tara'
|
request.headers['Requester'] == 'tara'
|
||||||
end
|
end
|
||||||
|
|
||||||
def logger
|
|
||||||
Rails.logger
|
|
||||||
end
|
|
||||||
|
|
||||||
def auth_values_to_data(registrar:)
|
def auth_values_to_data(registrar:)
|
||||||
data = current_user.as_json(only: %i[id username roles])
|
data = current_user.as_json(only: %i[id username roles])
|
||||||
data[:registrar_name] = registrar.name
|
data[:registrar_name] = registrar.name
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue