Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect does not terminate the response, which may be confusing #725

Open
dblock opened this issue Aug 18, 2014 · 5 comments
Open

Redirect does not terminate the response, which may be confusing #725

dblock opened this issue Aug 18, 2014 · 5 comments

Comments

@dblock
Copy link
Member

dblock commented Aug 18, 2014

I got caught by surprise that redirect doesn't terminate the response. Added myself a redirect! like so, but maybe this needs to at least be documented or an option added to stop the response:

module Grape
  module Extensions
    module RedirectExtension
      def redirect!(url, options = {})
        redirect url, options
        throw :error, status: @status, headers: @header
      end

      Grape::Endpoint.send(:include, self)
    end
  end
end
@dspaeth-faber
Copy link
Contributor

I think not redirect instead error! misbehaves ;). Serious.
Sinatra expect from you that your method return the body as String, an Array, an Object or a Fixnum.
Rails expect that you call render or redirect only once and return from your action. I think grape should
handle it similar.

Exceptions for flow control are very expensive.

I would like it more this way:

error() and return if ...
present(...) and return if ...

@dblock
Copy link
Member Author

dblock commented Aug 27, 2014

I concur with @dspaeth-faber, Rails and others all make redirect behave in this way.

@ekampp
Copy link
Contributor

ekampp commented Jun 23, 2015

Additionally: Not setting the format results in a xml rendering exception. Presumably Grape defaults to xml unless the format is set. And presumably since redirect doesn't halt the chain, xml attempts to render nothing:

This fails:

get '/' do
  redirect '/v1'
end

With this xml error:

<error>
<message>cannot convert String to xml</message>
</error>

This redirects correctly:

format :json
get '/' do
  redirect '/v1'
end

@dblock
Copy link
Member Author

dblock commented Jun 23, 2015

Grame really defaults (should default :)) to JSON, not sure what the deal here is, you might want to dig a bit deeper?

@ekampp
Copy link
Contributor

ekampp commented Jun 23, 2015

Might be grape-roar interfering and defaulting to xml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants