Sha256: 56d06aa38d7d2fca59b8a7dd4d54744b15d73d0e756448ff683a64c9dd2b4030

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 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

5 entries across 5 versions & 1 rubygems

Version Path
potassium-1.3.4 lib/potassium/templates/application/assets/api/api_error_concern.rb
potassium-1.3.3 lib/potassium/templates/application/assets/api/api_error_concern.rb
potassium-1.3.2 lib/potassium/templates/application/assets/api/api_error_concern.rb
potassium-1.3.0 lib/potassium/templates/application/assets/api/api_error_concern.rb
potassium-1.2.4 lib/potassium/templates/application/assets/api/api_error_concern.rb