西纳特拉的全球救援和伐木例外

发布于 2024-12-20 16:55:39 字数 58 浏览 0 评论 0原文

如何在出现异常时指定全局救援,如果您将 Sinatra 用于 API 或应用程序,您如何处理日志记录?

How do I specify a global rescue in case of an exception, and if you use Sinatra for an API or application, how do you handle logging?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深如许 2024-12-27 16:55:39

404 可以借助 not_found 方法来处理,例如:

not_found do
  'Site does not exist.'
end

500 可以通过使用块调用 error 方法来处理,例如:

error do
  "Application error. Pls try later."
end

可以通过 sinatra 访问错误的详细信息request.env 中的 .error 如下所示:

error do
  'An error occured: ' + request.env['sinatra.error'].message
end

404s can be handled with the help of the not_found method like eg:

not_found do
  'Site does not exist.'
end

500s can be handled by calling the error method with a block, eg:

error do
  "Application error. Pls try later."
end

The details of the error can be accessed via the sinatra.error in request.env like so:

error do
  'An error occured: ' + request.env['sinatra.error'].message
end
可是我不能没有你 2024-12-27 16:55:39

我很难在我的开发环境中开箱即用 - 为了让它工作,我必须在我的 sinatra 配置中将 show_exceptions 设置为 false。

class BaseApp < Sinatra::Base

  configure { set :show_exceptions, false }

  error do |err|
    raise "Error: #{err}"
  end

end

当此设置设置为 true 时,会启用在发生未处理异常时显示回溯和环境信息的错误页面,但我只能通过禁用它来触发自定义错误。

I had trouble getting this working out of the box in my dev environment - to get it to work, I had to set show_exceptions to false in my sinatra config.

class BaseApp < Sinatra::Base

  configure { set :show_exceptions, false }

  error do |err|
    raise "Error: #{err}"
  end

end

This setting, when set to true, enables error pages that show backtrace and environment information when an unhanded exception occurs, but I could only fire custom errors by disabling it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文