lib/angelo/responder.rb in angelo-0.1.14 vs lib/angelo/responder.rb in angelo-0.1.15

- old
+ new

@@ -30,22 +30,18 @@ Hash.new {|hash,key| hash[key.to_s] if Symbol === key } end end - attr_writer :connection + attr_accessor :connection attr_reader :request + attr_writer :base def initialize &block @response_handler = Base.compile! :request_handler, &block end - def base= base - @base = base - @base.responder = self - end - def request= request @params = nil @redirect = nil @body = nil @request = request @@ -54,26 +50,36 @@ def handle_request if @response_handler @base.before if @base.respond_to? :before @body = catch(:halt) { @response_handler.bind(@base).call || EMPTY_STRING } - @base.after if @base.respond_to? :after + + # TODO any real reason not to run afters with SSE? + case @body + when HALT_STRUCT + if @body.body != :sse and @base.respond_to? :after + @base.after + end + else + @base.after if @base.respond_to? :after + end + respond else raise NotImplementedError end rescue JSON::ParserError => jpe - handle_error jpe, :bad_request, false + handle_error jpe, :bad_request rescue FormEncodingError => fee - handle_error fee, :bad_request, false + handle_error fee, :bad_request rescue RequestError => re - handle_error re, re.type, false + handle_error re, re.type rescue => e handle_error e end - def handle_error _error, type = :internal_server_error, report = true + def handle_error _error, type = :internal_server_error, report = @base.report_errors? err_msg = error_message _error Angelo.log @connection, @request, nil, type, err_msg.size @connection.respond type, headers, err_msg @connection.close if report @@ -128,10 +134,11 @@ status = nil case @body when HALT_STRUCT status = @body.status @body = @body.body + @body = nil if @body == :sse if Hash === @body @body = {error: @body} if status != :ok or status < 200 && status >= 300 @body = @body.to_json if respond_with? :json end @@ -146,14 +153,15 @@ @body = EMPTY_STRING end status ||= @redirect.nil? ? :ok : :moved_permanently headers LOCATION_HEADER_KEY => @redirect if @redirect + size = @body.nil? ? 0 : @body.size - Angelo.log @connection, @request, nil, status, @body.size - @connection.respond status, headers, @body + Angelo.log @connection, @request, nil, status, size + @request.respond status, headers, @body rescue => e - handle_error e, :internal_server_error, false + handle_error e, :internal_server_error end def redirect url @redirect = url end