Sha256: 947f246e3f3672724655ab29fffd7b10076cec3f1a7d1ca2eec47dfc6241c90c

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

require "json"

module BetterErrors
  class Middleware
    def initialize(app, handler = ErrorPage)
      @app      = app
      @handler  = handler
    end
    
    def call(env)
      case env["PATH_INFO"]
      when %r{\A/__better_errors/(?<oid>\d+)/(?<method>\w+)\z}
        internal_call env, $~
      when %r{\A/__better_errors/?\z}
        show_error_page env
      else
        app_call env
      end
    end
    
  private
    def app_call(env)
      @app.call env
    rescue Exception => ex
      @error_page = @handler.new ex, env
      log_exception
      show_error_page(env)
    end
    
    def show_error_page(env)
      [500, { "Content-Type" => "text/html; charset=utf-8" }, [@error_page.render]]
    end
    
    def log_exception
      return unless BetterErrors.logger
      
      message = "\n#{@error_page.exception.class} - #{@error_page.exception.message}:\n"
      @error_page.backtrace_frames.each do |frame|
        message << "  #{frame}\n"
      end
      
      BetterErrors.logger.fatal message
    end
  
    def internal_call(env, opts)
      if opts[:oid].to_i != @error_page.object_id
        return [200, { "Content-Type" => "text/plain; charset=utf-8" }, [JSON.dump(error: "Session expired")]]
      end
      
      response = @error_page.send("do_#{opts[:method]}", JSON.parse(env["rack.input"].read))
      [200, { "Content-Type" => "text/plain; charset=utf-8" }, [JSON.dump(response)]]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
better_errors-0.2.0 lib/better_errors/middleware.rb
better_errors-0.1.1 lib/better_errors/middleware.rb
better_errors-0.1.0 lib/better_errors/middleware.rb