Sha256: 31df682fb8428e5110303cca3242836806a1095f95382753cb243a6ae272ae05

Contents?: true

Size: 682 Bytes

Versions: 159

Compression:

Stored size: 682 Bytes

Contents

module WithErrorsFilter
  extend ActiveSupport::Concern

  included do
    around_action :catch_exceptions
    rescue_from Exception do |e| catch_unhandled_errors(e) end
  end

  def catch_exceptions
    yield
  rescue ActiveRecord::RecordInvalid => e
    render! e.record.errors
  end

  def catch_unhandled_errors(e)
    summary = {
        errors: {
            :exception => "#{e.class.name} : #{e.message}"
        }
    }
    summary[:trace] = e.backtrace[0, 10] if Rails.env.development?

    render json: summary, status: :internal_server_error
  end

  private

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

Version data entries

159 entries across 159 versions & 1 rubygems

Version Path
mumuki-laboratory-9.23.0 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.22.0 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.21.0 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.20.1 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.20.0 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.19.0 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.18.1 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.18.0 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.17.0 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.16.0 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.15.0 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.14.1 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.14.0 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.13.2 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.13.1 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.13.0 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.12.1 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.12.0 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.11.0 app/controllers/concerns/with_errors_filter.rb
mumuki-laboratory-9.10.0 app/controllers/concerns/with_errors_filter.rb