Sha256: 1fc39c6f52f23449cd3f15faca3a3473d58d001abb9b0f8dc3e34652be821d59

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

module Rao
  module Api
    module ResourceController::ExceptionHandlingConcern
      extend ActiveSupport::Concern

      included do
        rescue_from Exception do |exception|
          handle_exception(exception)
        end

        rescue_from ActiveRecord::RecordNotFound do |exception|
          handle_404(exception)
        end
      end

      private

      def handle_404(exception = nil)
        respond_to do |format|
          format.json { render json: { error: (exception.try(:message) || 'Not found') }, status: 404 }
        end
      end

      def handle_exception(exception)
        if Rails.env.development? || Rails.env.test?
          error = { message: exception.message }

          error[:application_trace] = Rails.backtrace_cleaner.clean(exception.backtrace)
          error[:full_trace] = exception.backtrace

          respond_to do |format|
            format.json { render json: error, status: 500 }
          end
        else
          respond_to do |format|
            format.json { render json: { error: 'Internal server error.' }, status: 500 }
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rao-api-resources_controller-0.0.52.pre app/concerns/rao/api/resource_controller/exception_handling_concern.rb
rao-api-resources_controller-0.0.51.pre app/concerns/rao/api/resource_controller/exception_handling_concern.rb
rao-api-resources_controller-0.0.50.pre app/concerns/rao/api/resource_controller/exception_handling_concern.rb
rao-api-resources_controller-0.0.49.pre app/concerns/rao/api/resource_controller/exception_handling_concern.rb