2020-04-28 11:27:59 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class AjaxController < ApplicationController
|
|
|
|
before_action :build_response
|
|
|
|
after_action :return_response
|
|
|
|
|
|
|
|
respond_to :json
|
|
|
|
|
2020-04-29 10:54:43 -07:00
|
|
|
rescue_from(StandardError) do |e|
|
2021-12-28 09:32:03 -08:00
|
|
|
Sentry.capture_exception(e)
|
2020-04-29 10:54:43 -07:00
|
|
|
|
|
|
|
@response = {
|
|
|
|
success: false,
|
|
|
|
message: "Something went wrong",
|
2022-01-16 09:51:27 -08:00
|
|
|
status: :err
|
2020-04-29 10:54:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return_response
|
|
|
|
end
|
|
|
|
|
2022-01-16 09:38:06 -08:00
|
|
|
rescue_from(KeyError) do |e|
|
|
|
|
Sentry.capture_exception(e)
|
|
|
|
|
|
|
|
@response = {
|
|
|
|
success: false,
|
|
|
|
message: "Missing parameter: #{e.key}",
|
2022-01-16 09:51:27 -08:00
|
|
|
status: :err
|
2022-01-16 09:38:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return_response
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue_from(Dry::Types::CoercionError, Dry::Types::ConstraintError) do |e|
|
|
|
|
Sentry.capture_exception(e)
|
|
|
|
|
|
|
|
@response = {
|
|
|
|
success: false,
|
|
|
|
message: "Invalid parameter",
|
2022-01-16 09:51:27 -08:00
|
|
|
status: :err
|
2022-01-16 09:38:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return_response
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
rescue_from(ActiveRecord::RecordNotFound) do |e|
|
2021-12-28 09:32:03 -08:00
|
|
|
Sentry.capture_exception(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
|
|
|
|
@response = {
|
|
|
|
success: false,
|
|
|
|
message: "Record not found",
|
2022-01-16 09:51:27 -08:00
|
|
|
status: :not_found
|
2020-04-28 11:27:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return_response
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue_from(ActionController::ParameterMissing) do |e|
|
2021-12-28 09:32:03 -08:00
|
|
|
Sentry.capture_exception(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
|
|
|
|
@response = {
|
|
|
|
success: false,
|
2022-01-16 09:51:27 -08:00
|
|
|
message: I18n.t("messages.parameter_error", parameter: e.param.capitalize),
|
|
|
|
status: :parameter_error
|
2020-04-28 11:27:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return_response
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_active_announcements
|
|
|
|
# We do not need announcements here
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def build_response
|
|
|
|
@response = {
|
|
|
|
success: false,
|
2022-01-16 09:51:27 -08:00
|
|
|
message: "",
|
|
|
|
status: "unknown"
|
2020-04-28 11:27:59 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def return_response
|
|
|
|
# Q: Why don't we just use render(json:) here?
|
|
|
|
# A: Because otherwise Rails wants us to use views, which do not make much sense here.
|
|
|
|
#
|
|
|
|
# Q: Why do we always return 200?
|
|
|
|
# A: Because JQuery might not do things we want it to if we don't.
|
|
|
|
response.status = 200
|
|
|
|
response.headers["Content-Type"] = "application/json"
|
|
|
|
response.body = @response.to_json
|
|
|
|
end
|
|
|
|
end
|