Sha256: 391fb79b43bff01c9de1597e5d3aee23e374bd487b24605ae439e98de487560e

Contents?: true

Size: 861 Bytes

Versions: 7

Compression:

Stored size: 861 Bytes

Contents

# encoding: utf-8

module Hyperdrive
  module Middleware
    class Error
      include Hyperdrive::Values

      def initialize(app)
        @app = app
      end

      def call(env)
        @app.call(env)
      rescue => e
        headers = { 'Content-Type' => 'application/json' }
        if e.respond_to?(:http_status_code)
          status = e.http_status_code
          body = [json_error(e)]
        else
          puts e.message
          status = 500
          body = json_error(Hyperdrive::Errors::UnknownError.new)
        end
        [status, headers, body]
      end

      private

      def json_error(e)
        MultiJson.dump({
          _links: { root: { href: '/', title: 'API Root' } },
          error: {
            type: "#{e.class.to_s.split('::').last}",
            message: e.message
          }
        })
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hyperdrive-0.0.17 lib/hyperdrive/middleware/error.rb
hyperdrive-0.0.16 lib/hyperdrive/middleware/error.rb
hyperdrive-0.0.15 lib/hyperdrive/middleware/error.rb
hyperdrive-0.0.14 lib/hyperdrive/middleware/error.rb
hyperdrive-0.0.13 lib/hyperdrive/middleware/error.rb
hyperdrive-0.0.12 lib/hyperdrive/middleware/error.rb
hyperdrive-0.0.10 lib/hyperdrive/middleware/error.rb