/
application_controller.rb
52 lines (44 loc) · 1.49 KB
/
application_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true
# General controller to include all-encompassing behavior
class ApplicationController < ActionController::API
include ActionController::Helpers
include DefaultHeaders
include Pundit
include Authentication
include ExceptionNotifierCustomData
include Metadata
include Pagination
include Search
include Rendering
include Parameters
def openapi
send_file Rails.root.join('swagger/v1/openapi.v3.yaml')
end
def pundit_scope
Pundit.policy_scope(current_user, resource)
end
rescue_from ActiveRecord::RecordNotUnique do |error|
render json: {
errors: "Duplicate record: #{error.message[/Key \(.+\).+\./]}"
}, status: :conflict
end
rescue_from Pundit::NotAuthorizedError do
render json: { errors: 'You are not authorized to access this action.' },
status: :forbidden
end
rescue_from ActiveRecord::RecordNotFound do |error|
logger.info "#{error.message} (#{error.class})"
render json: { errors: "#{error.model} not found with ID #{error.id}" },
status: :not_found
end
rescue_from ActionController::ParameterMissing do |error|
logger.info "#{error.message} (#{error.class})"
render json: { errors: "Parameter missing: #{error.message}" },
status: :unprocessable_entity
end
rescue_from StrongerParameters::InvalidParameter do |error|
logger.info "#{error.message} (#{error.class})"
render json: { errors: error.message },
status: :unprocessable_entity
end
end