如何在 Sinatra 中定义 JSON 的全局错误处理程序
我想定义一个错误块(或其他东西),它将返回所有以JSON格式格式化的异常加上返回标准http代码(例如404表示未找到,303表示身份验证错误等)。
比如:
error do
e = env['sinatra.error']
json :result => 'error', :message => e.message
end
谢谢!
I would like to define an error block (or something) that would return all exceptions formatted in JSON somehow plus returning the standard http code (e.g. 404 for not found, 303 for auth errors etc).
Something like:
error do
e = env['sinatra.error']
json :result => 'error', :message => e.message
end
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该有效:
使用curl 测试它以查看它是否有效。
This should work:
Test it with curl to see that it works.