-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapi_controller.rb
More file actions
64 lines (50 loc) · 1.65 KB
/
Copy pathapi_controller.rb
File metadata and controls
64 lines (50 loc) · 1.65 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
class ApiController < ActionController::API
class ::ParameterError < StandardError; end
include Identifiable
rescue_from StandardError, with: :internal_server_error
rescue_from ActionController::ParameterMissing, with: :bad_request
rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from CanCan::AccessDenied, with: :denied
rescue_from ParameterError, with: :unprocessable
before_action :set_paper_trail_whodunnit
private
def bad_request(exception)
render_error_as_json(exception, :bad_request)
end
def authorize_user
render json: { error: 'Unauthorized' }, status: :unauthorized unless current_user
end
def denied(_exception)
if current_user
render json: { error: 'Forbidden' }, status: :forbidden
else
render json: { error: 'Unauthorized' }, status: :unauthorized
end
end
def not_found(exception)
render_error_as_json(exception, :not_found)
end
def unprocessable(exception)
render_error_as_json(exception, :unprocessable_entity)
end
def internal_server_error(exception)
Sentry.capture_exception(exception)
render_error_as_json(exception, :internal_server_error)
end
def render_error_as_json(exception, status)
render json: { error: "#{exception.class}: #{exception.message}" }, status:
end
def track_event(name, properties = {})
EventTracker.track!(user_id: current_user.id, name:, properties:)
end
def track_project_event(name, project, user_role: nil, student_id: nil)
EventTracker.track_project_event!(
name:,
user_id: current_user.id,
project:,
user_role:,
student_id:
)
end
end