Sha256: 1c6954f7d087e975defd724278d6b5f1079cd786cdb5f84f967a37672a18cdb2

Contents?: true

Size: 1006 Bytes

Versions: 8

Compression:

Stored size: 1006 Bytes

Contents

module Gris
  class Middleware
    class ErrorHandlers
      def initialize(app)
        @app = app
      end

      def call(env)
        response = @app.call env
        case response[0]
        when 400..500
          format_error_response response
        else
          response
        end
      rescue RuntimeError => e
        error = { status: 500, message: e.message }
        error_response(error.to_json, 500)
      rescue ::ActiveRecord::RecordNotFound => e
        error = { status: 404, message: e.message }
        error_response(error.to_json, 404)
      end

      private

      def format_error_response(response)
        if response[2].respond_to? :body
          response
        else
          error = { status: response[0], message: response[2] }
          error_response(error.to_json, response[0], response[1])
        end
      end

      def error_response(message, status, headers = {})
        Rack::Response.new([message], status, headers).finish
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gris-0.6.0 lib/gris/middleware/error_handlers.rb
gris-0.5.9 lib/gris/middleware/error_handlers.rb
gris-0.5.8 lib/gris/middleware/error_handlers.rb
gris-0.5.7 lib/gris/middleware/error_handlers.rb
gris-0.5.6 lib/gris/middleware/error_handlers.rb
gris-0.5.5 lib/gris/middleware/error_handlers.rb
gris-0.5.4 lib/gris/middleware/error_handlers.rb
gris-0.5.3 lib/gris/middleware/error_handlers.rb