application_controller.rb 745 B

123456789101112131415161718192021222324252627
  1. # frozen_string_literal: true
  2. require './lib/api_error/base_error'
  3. class API::V1::ApplicationController < ActionController::API
  4. before_action :set_default_response_format
  5. rescue_from StandardError, with: :handle_unhandled_api_error
  6. def not_found
  7. @error = APIError::BaseError.new("The requested resource was not found")
  8. render partial: 'error', status: :not_found
  9. end
  10. private
  11. def handle_unhandled_api_error(e)
  12. Rails.logger.error("Unhandled API Error: #{e}\n#{e.backtrace.join("\n")}")
  13. @error = APIError::BaseError.new("An unhandled exception occurred.", internal_error: e)
  14. render partial: 'error', status: :internal_server_error
  15. end
  16. def set_default_response_format
  17. request.format = :json
  18. end
  19. end