gpt4 book ai didi

ruby - Ruby 中的 Sinatra 错误处理

转载 作者:数据小太阳 更新时间:2023-10-29 06:45:56 25 4
gpt4 key购买 nike

我有一个简单的 Sinatra 休息,但我无法捕获错误。我也承认我对 Ruby 和 Sinatra 还很陌生。

当我在 post 端点中引发错误时,我想报告传入的文档。我需要 1) 处理发布结果中的错误(我可以访问 @incoming)或 2) 将传入文档传递给错误并在那里报告。

什么是更好的选择,选项 1 还是选项 2?

  • 如果我坚持使用选项 1,我该如何防止错误发现错误(就像它现在所做的那样)
  • 如果我转到选项 2,我如何将 incoming 传递给 error?

下面是我的代码示例:

post ('/result') do 

begin
@incoming = JSON.parse(request.body.read)

//do something that causes an error
rescue
e = env['sinatra.error']
url = request.url
ip = request.ip

@actlogpassblock = { :message => e.message,
:path => url,
:ip => ip,
:timestamp => Time.new,
:type => "500",
:sub => "RES",
:payload => @incoming
}
action_log.insert(@actlogpassblock)
status 500
end
end

error do
status 500
e = env['sinatra.error']
url = request.url
ip = request.ip
backtrace = "Application error\n#{e}\n#{e.backtrace.join("\n")}"

@actlogpassblock = { :message => e.message,
:path => url,
:ip => ip,
:timestamp => Time.new,
:type => "500",
:backtrace => backtrace
}
action_log.insert(@actlogpassblock)
{:result => 'Ah Shucks! Something went wrong'}.to_json
end

最佳答案

If I stay with option 1, how do I prevent error from picking up the error (as it seems to be doing now)

根据文档:

The error handler is invoked any time an exception is raised from a route block...

但是,这仅适用于未捕获 异常。试试这个:

require 'sinatra'
set :show_exceptions, false

get '/' do
begin
raise ZeroDivisionError
rescue ZeroDivisionError
"rescue clause"
end
end

error do
"sinatra error handler"
end

然后试试这个:

get '/' do
raise ZeroDivisionError
end

error do
"sinatra error handler"
end

此外,您可以定制错误处理程序以仅捕获某些异常,从而避免某些异常,例如

error IndexError do ...

error MyCustomException do ...

error 400..510 do

但对于捕获所有版本:

error do

当路由 block 中发生未捕获的异常时,您不能阻止它执行...除非:

The error handlers will only be invoked, however, if both the Sinatra :raise_errors and :show_exceptions configuration options have been set to false...

:raise_errors defaults to true in the "test" environment and to false on other environments.

:show_exceptions value defaults to true in the "development" environment and to false on other environments

The author of Sintra has said: "This [behavior] is intentional. The idea is that error blocks will hide the issue and you usually don't want to do this in development mode.

https://github.com/sul-dlss/sdr-services-app/blob/master/Sinatra-error-handling.md

If I go to option 2, how do I pass incoming to error?

可以在错误 block 中看到在路由 block 内创建的@variable。试试这个:

require 'sinatra'
set :show_exceptions, false

get '/' do
@incoming = "hello world" #create @variable
raise ZeroDivisionError
end

error ZeroDivisionError do
@incoming #retrieve @variable
end

在浏览器中输入 url http://localhost:4567 后,您应该会在返回的网页上看到“hello world”。

之所以起作用,是因为 @variable 在创建 @variable 时将其自身附加到任何对象本身;同样,当调用 @variable 时,它​​会从当时是 self 的任何对象中检索出来。当 Sinatra 执行路由 block 或错误 block 时,它会将 self 设置为同一个对象。

选项 2 看起来不错,因为它将错误处理代码与应用程序代码分开。

关于ruby - Ruby 中的 Sinatra 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25299186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com