Sha256: c65dad73d6aa91e24e562a5de15d87c8f3583aed00cbb7c8afdfe2d947ede888

Contents?: true

Size: 840 Bytes

Versions: 18

Compression:

Stored size: 840 Bytes

Contents

module WithApiErrors
  # TODO we should extend DynamiceErrors
  extend ActiveSupport::Concern

  included do
    unless Rails.application.config.consider_all_requests_local
      rescue_from ActionController::RoutingError, with: :not_found!
    end

    rescue_from ActiveRecord::RecordNotFound, with: :not_found!
    rescue_from Mumukit::Auth::UnauthorizedAccessError, with: :forbidden!
    rescue_from ActiveRecord::RecordInvalid, with: :bad_record!
  end

  private

  def bad_record!(e)
    render_errors! e.record.errors, 400
  end

  def not_found!(e)
    render_error! e, 404
  end

  def forbidden!(e)
    render_error! e, 403
  end

  def render_error!(e, status)
    render_errors! [e.message], status
  end

  def render_errors!(errors, status)
    summary = { errors: errors }
    render json: summary, status: status
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
mumuki-laboratory-5.10.0 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.9.1 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.9.0 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.8.3 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.8.1 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.8.0 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.7.0 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.6.3 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.6.2 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.6.1 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.6.0 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.5.0 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.4.0 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.3.0 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.2.1 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.2.0 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.1.1 app/controllers/concerns/with_api_errors.rb
mumuki-laboratory-5.1.0 app/controllers/concerns/with_api_errors.rb