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

Allow response directly from callback #747

Open
Galmido opened this issue Aug 30, 2014 · 4 comments
Open

Allow response directly from callback #747

Galmido opened this issue Aug 30, 2014 · 4 comments

Comments

@Galmido
Copy link

Galmido commented Aug 30, 2014

I use Grape based application for PXE bare-metal server provisioning software. The main reason for which I use Grape for this is a very fast response time, resources lightweight, fault tolerance and REST API abilities which allows console client to communicate directly to provisioning server, without using another HTTP server library.

My application contains a large number of machine status checks, boot stage verification, connectivity checks and many other features for which I use mainly a Grape before callback. In this family of software machine communicates with Grape application multiple times during boot and it's a high chance that something will go wrong, eg. machine reboots or hangs between requests, caused by insufficient amount of RAM memory to boot received system image. For this exact example I use Grape before callback which ensures that the machine don't "go back" in boot process. In this and many another cases I need to return a response without executing requested HTTP function and without raising any exception in the application itself.

From what I see, Grape does not have this functionality, which forced me to make verification inside before callback block, and then return proper response in each HTTP method, depending on whether the verification was successful.

I very like to see function like return! or response! working in all callbacks, which will break code execution and immediately returns given response text, without calling any other callbacks. What do you think? This would help me a further development of the applications and greatly enhanced the readability of the code while reducing its vulnerability to coding errors.

@dblock
Copy link
Member

dblock commented Aug 30, 2014

Before we discuss whether this should be a feature in Grape, maybe you can try to see if monkey patching https://github.com/intridea/grape/blob/master/lib/grape/middleware/base.rb would be enough. You could rewrite it like so:

class ExecutionAbortedError < StandardError
end

module Grape
  module Middleware
    class Base
      def call!(env)
        @env = env
        before
        @app_response = @app.call(@env)
        after || @app_response
      rescue ExecutionAbortedError => e
        { } # return whatever you want here
      end
    end
 end
end

You can raise ExecutionAbortedError inside a before block.

If this is suitable we can discuss a DSL where this is actually a first class citizen in Grape.

@dspaeth-faber
Copy link
Contributor

In my point of view Exception should be used carefully. With the gem virtus I ran into some performance issues because it uses exceptions for flow control.

Befor and After Filter seem's to me like additional middleware so I would propose to use X-Cascade header in before filters. When a filter sets X-Cascade to pass the next filter in chain is called, if it is the last one app is called. If not stop and exit the hole middleware. pass would be the default and can be changed for instance with abort_chain .

Rails handle it a little different. In the early days it used the return value false to abort a chain.
Today it stops a chain when render or redirect is called.

@hakunin
Copy link

hakunin commented Aug 29, 2017

Whats the state of this?
I'd like to return response from before as well.

@dblock
Copy link
Member

dblock commented Aug 29, 2017

No progress as you can see @hakunin.

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

4 participants