Sha256: bdd82e706d9320fc2105d14ae4669343b8274a57c3af6d63e2ce176581b38413

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

module Radriar::API
  module ExceptionHandling
    module Grape
      extend ActiveSupport::Concern

      included do
        if defined?(Mongoid)
          rescue_from Mongoid::Errors::DocumentNotFound do |e|
            error_response(message: e.message, status: 404)
          end
        end

        rescue_from Grape::Exceptions::ValidationErrors do |e|
          Rack::Response.new({
            status: 422,
            message: e.message,
            errors: e.errors
          }.to_json, 422, { "Content-Type" => "application/json"})
        end

        rescue_from Mongoid::Errors::Validations do |e|
          Rack::Response.new({
            message: "Validation failed",
            status: 422,
            errors: e.document.errors.messages
          }.to_json, 422, {"Content-Type" => "application/json"})
        end

        rescue_from Mongoid::Errors::DocumentNotFound do |e|
          error_response(
            message: "There was a problem finding a requested or associated record",
            status: 404
          )
        end

        rescue_from :all do |e|
          if Rails.env.development? || Rails.env.test?
            raise e
          else
            Bugsnag.notify(e) if defined?(Bugsnag)
            error_response(message: "Internal server error", status: 500)
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
radriar-0.1.0.alpha.3 lib/radriar/api/exception_handling.rb
radriar-0.1.0.alpha.2 lib/radriar/api/exception_handling.rb
radriar-0.1.0.alpha.1 lib/radriar/api/exception_handling.rb