我想定义一个错误块(或其他东西),它会以某种方式返回以JSON格式化的所有异常,并返回标准的http代码(例如404表示未找到,303表示auth错误等).
就像是:
error do
e = env['sinatra.error']
json :result => 'error', :message => e.message
end
Run Code Online (Sandbox Code Playgroud)
谢谢!
Jes*_*mer 11
这应该工作:
require 'sinatra'
require 'json'
# This is needed for testing, otherwise the default
# error handler kicks in
set :environment, :production
error do
content_type :json
status 400 # or whatever
e = env['sinatra.error']
{:result => 'error', :message => e.message}.to_json
end
get '/' do
raise 'hell'
end
Run Code Online (Sandbox Code Playgroud)
用卷曲测试它,看它是否有效.