开发者

How do I pass data to an error handler?

开发者 https://www.devze.com 2023-03-09 12:12 出处:网络
I want to create an error handler that sets the body of the response depending on the nature of the error. Something like this:

I want to create an error handler that sets the body of the response depending on the nature of the error. Something like this:

@the_error = Hash.new

get '/' do
 @the_error[:message] = "error message"
 400
end

error 400 do
 @the_error[:message]
end

But this doesn't work as expected (the @the_error variable is Nil when in the get handler). What am I doing wrong, or perhaps there's a be开发者_开发技巧tter way to do this?


Use built-in settings for this:

require 'sinatra'

set :the_error, Hash.new

get '/' do
  options.the_error[:message] = "error message"
  400
end

error 400 do
  options.the_error[:message]
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号