-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Comments
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 If this is suitable we can discuss a DSL where this is actually a first class citizen in Grape. |
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 Rails handle it a little different. In the early days it used the return value |
Whats the state of this? |
No progress as you can see @hakunin. |
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 Grapebefore
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!
orresponse!
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.The text was updated successfully, but these errors were encountered: