Sha256: c6a989d2abec37e2fc1234d3060a2a3840629fffaed58bc812f540531eae9d6b

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

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
        error_message = "#{e.class}: #{e.message}"
        $stdout.sync = true
        $stdout.puts error_message
        headers = { 'Content-Type' => 'application/json' }
        if e.respond_to?(:http_status_code)
          status = e.http_status_code
          body = [json_error(e)]
        else
          status = 500
          error = if ENV['RACK_ENV'] == 'production'
                    Hyperdrive::Errors::UnknownError.new
                  else
                    Hyperdrive::Errors::UnknownError.new(error_message)
                  end
          body = [json_error(error)]
        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

1 entries across 1 versions & 1 rubygems

Version Path
hyperdrive-0.0.25 lib/hyperdrive/middleware/error.rb