Sha256: 8afaeb36c5f689a470fa9ccedb20bf8945e18e1ff7423a481dcd29893593d719

Contents?: true

Size: 977 Bytes

Versions: 2

Compression:

Stored size: 977 Bytes

Contents

module Jersey::Middleware
  class ErrorHandler
    def initialize(app, options = {})
      @app = app
      ib = options[:include_backtrace]
      @include_backtrace = (ib.nil? && ENV['RACK_ENV'] == 'development') || ib
    end

    def call(env)
      begin
        @app.call(env)
      rescue => e
        # get status code from Jersey Errors
        if e.class.const_defined?(:STATUS_CODE)
          status = e.class::STATUS_CODE
        elsif e.respond_to?(:status_code)
          status = e.status_code
        else
          status = 500
        end

        headers = {
          'Content-Type' => 'application/json'
        }

        body = {error: {
          type: e.class.name.split('::').last,
          request_id: env['REQUEST_ID'] || env['HTTP_REQUEST_ID'],
          message: e.message
        }}

        if @include_backtrace
          body[:error][:backtrace] = e.backtrace
        end

        [status, headers, [body.to_json]]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jersey-0.2.0 lib/jersey/middleware/error_handler.rb
jersey-0.1.0 lib/jersey/middleware/error_handler.rb