[英]Rails - Rubocop - Begin + Rescue syntax
I have the following code: 我有以下代码:
def payload
begin
@payload ||= Warden::JWTAuth::TokenDecoder.new.call(token)
rescue JWT::ExpiredSignature => e
Rollbar.warning(e)
end
end
From brief reading of a few blogs I'm supposed to be using begin rescue and end to handle the error as I'm doing above, however I'm getting a redundant 'begin' rubocop warning. 从简短的阅读几个博客,我应该使用开始救援和结束处理错误,因为我正在上面做,但是我得到一个多余的'开始'rubocop警告。
Is begin only used when specifying a bit of code that may cause an error within a larger block? 是否仅在指定可能导致较大块内的错误的代码时使用? And is it therefore redundant here?
因此它在这里是多余的吗?
Thanks in advance 提前致谢
EDIT: And if I don't need it, is it written as 编辑:如果我不需要它,它是否写成
def payload
@payload ||= Warden::JWTAuth::TokenDecoder.new.call(token)
rescue JWT::ExpiredSignature => e
Rollbar.warning(e)
end
? ?
Do this when the begin would be the first thing in your method 当开始是你方法中的第一件事时,这样做
def payload
@payload ||= Warden::JWTAuth::TokenDecoder.new.call(token)
rescue JWT::ExpiredSignature => e
Rollbar.warning(e)
end
Method bodies, block bodies, and lambda bodies are implicit exception blocks. 方法体,块体和lambda体是隐式异常块。 You don't need to wrap the entire code of a method body, block body, or lambda body in a
begin
/ rescue
/ else
/ ensure
/ end
exception block, since it is already implicitly one. 您不需要在
begin
/ rescue
/ else
/ ensure
/ end
异常块中包装方法体,块体或lambda体的整个代码 ,因为它已经是隐式的。 So, whenever you have something like 所以,每当你有类似的东西时
def foo
begin
rescue
end
end
or 要么
foo do
begin
rescue
end
end
or 要么
-> do
begin
rescue
end
end
you can replace it with just 你可以用它替换它
def foo
rescue
end
or the equivalent for blocks and lambdas. 或块和lambdas的等价物。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.