Sha256: e18363bfe291bc5c393c267bf1de43288415d6d5f1adb5c29601b3f0e2a67d0f

Contents?: true

Size: 1.13 KB

Versions: 15

Compression:

Stored size: 1.13 KB

Contents

module ApiErrorConcern
  extend ActiveSupport::Concern

  included do
    rescue_from "Exception" do |exception|
      logger.error exception.message
      logger.error exception.backtrace.join("\n")
      respond_api_error(:internal_server_error, message: "server_error",
                                                type: exception.class.to_s,
                                                detail: exception.message)
    end

    rescue_from "ActiveRecord::RecordNotFound" do |exception|
      respond_api_error(:not_found, message: "record_not_found",
                                    detail: exception.message)
    end

    rescue_from "ActiveModel::ForbiddenAttributesError" do |exception|
      respond_api_error(:bad_request, message: "protected_attributes",
                                      detail: exception.message)
    end

    rescue_from "ActiveRecord::RecordInvalid" do |exception|
      respond_api_error(:bad_request, message: "invalid_attributes",
                                      errors: exception.record.errors)
    end
  end

  def respond_api_error(status, error = {})
    render json: error, status: status
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
potassium-6.0.0 lib/potassium/assets/api/api_error_concern.rb
potassium-5.2.3 lib/potassium/assets/api/api_error_concern.rb
potassium-5.2.2 lib/potassium/assets/api/api_error_concern.rb
potassium-5.2.1 lib/potassium/assets/api/api_error_concern.rb
potassium-5.2.0 lib/potassium/assets/api/api_error_concern.rb
potassium-5.1.4 lib/potassium/assets/api/api_error_concern.rb
potassium-5.1.3 lib/potassium/assets/api/api_error_concern.rb
potassium-5.1.2 lib/potassium/assets/api/api_error_concern.rb
potassium-5.1.1 lib/potassium/assets/api/api_error_concern.rb
potassium-3.0.0 lib/potassium/assets/api/api_error_concern.rb
potassium-2.3.0 lib/potassium/assets/api/api_error_concern.rb
potassium-2.2.0 lib/potassium/assets/api/api_error_concern.rb
potassium-2.1.0 lib/potassium/assets/api/api_error_concern.rb
potassium-2.0.0 lib/potassium/assets/api/api_error_concern.rb
potassium-1.3.5 lib/potassium/templates/application/assets/api/api_error_concern.rb