“之后”中的 Sinatra 响应状态过滤器
由于某种原因,在 Sinatra“之后”过滤器中,我似乎无法访问当前状态代码
require 'rubygems'
require 'sinatra'
after do
puts "After hook with code: #{response.status}"
end
get '/hi' do
halt(401, "wtf?")
end
运行curl 127.0.0.1:4567/hi 时,它会导致:
After hook for code: 200
For some reason, in a Sinatra "after" filter I can't seem to access the current status code
require 'rubygems'
require 'sinatra'
after do
puts "After hook with code: #{response.status}"
end
get '/hi' do
halt(401, "wtf?")
end
When running curl 127.0.0.1:4567/hi, it results in:
After hook for code: 200
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它基本上是 Sinatra 中如何实现这些方法的函数。我们需要关注的方法有
call!
、invoke
和dispatch!
,都是Sinatra::Base (从 v1.3.2 开始)。call!
是顶层方法,在其中,它调用以下代码行:现在,
invoke
看起来像这样:它实际上在基础上设置响应代码你用
:halt
抛出
的东西。dispatch!
看起来像:看到那个
ensure
块了吗?当被抛出
的:halt
符号沿着堆栈跟踪向上运行时,它就会运行。至关重要的是,这是在状态设置代码运行之前。It's basically a function of how the methods are implemented in Sinatra. The methods we need to pay attention to are
call!
,invoke
anddispatch!
, all methods in Sinatra::Base (as of v1.3.2).call!
is the top level method, and in there, it calls the following line of code:Now,
invoke
looks like this:It actually sets the response code on the basis of the thing you
throw
with:halt
. Anddispatch!
looks like:See that
ensure
block? That gets run as the:halt
symbol that has beenthrown
sails up the stack trace. Crucially, this is before the status setting code is run.