akonsu
akonsu

Reputation: 29566

how to halt from inside stream block in sinatra?

I am trying to respond with a HTTP error code from my streaming block, but the web server throws an exception. what is the proper way to do it in this context?

/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:803:in `throw':
uncaught throw :halt (ArgumentError)

my code:

require 'sinatra/base'
class App < Sinatra::Base
  get '/' do
    stream :keep_open do |out|
      error 401
    end
  end
  run! if app_file == $0
end

Upvotes: 3

Views: 476

Answers (1)

jgnagy
jgnagy

Reputation: 171

Based on my understanding of #stream(), the response Headers have already been sent. While you can continue streaming data (the body), and even close the connection, I don't think you can modify the header after they've already been sent. I'm digging through Sinatra YARD docs to verify, but I'm pretty sure that is your issue.

Upvotes: 0

Related Questions