Rails 中的错误模式,引发“文本评估运行时错误”或引发 MyModule::CustomError?
问:标题可能是一个太大的问题,答案可能是“视情况而定”?然而,提供一些实际案例/示例应该可以帮助像我这样的开发人员认识何时应用什么。我将从我的具体情况开始。您是否会使用自定义错误类?为什么/为什么不呢?
欢迎使用下面的其他示例,例如当您使用自己的错误类时。我真的很想知道。
例如:我正在使用 httparty 来查询我们的 Rails Web 服务应用程序中的一些内容数据。它使用基本身份验证。我将粘贴测试代码和实现。我的测试应该期待什么,RuntimeError 或 SomeCustomError?
class MyIntegrationTest < Test::Unit::TestCase
context "connecting to someapp web service" do
should "raise not authorized if username is wrong" do
#get default MyWebserviceInterface instance, overriding username setting
ws_endpoint = build_integration_object(:username => 'wrong_username')
assert_raises RuntimeError do #TODO error design pattern?
ws_endpoint.get
end
end
end
end
实施:
class MyWebserviceInterface
include HTTParty
#Basic authentication and configurable base_uri
def initialize(u, p, uri)
@auth = {:username => u, :password => p}
@uri = uri
end
def base_uri
HTTParty.normalize_base_uri(@uri)
end
def get(path = '/somepath.xml', query_params = {})
opts = {:base_uri => base_uri, :query => query_params, :basic_auth => @auth}
response = self.class.get(path, opts)
evaluate_get_response(response)
response.parsed_response
end
def evaluate_get_response(response)
code = response.code
body = response.body
if code == 200
logger.debug "OK - CREATED code #{code}"
else
logger.error "expected code 200, got code #{code}. Response body: #{body}"
#TODO error design pattern? raise the above logged msg or a custom error?
raise SomeAppIntegration::Error(code, body)
end
end
Q: The title is perhaps too big of question and the answer probably is "it depends"? However, providing some practical cases/examples should help developers, like myself, recognize when to apply what. I'll start out with my particular situation. Would you or would you not use custom error classes? Why/why not?
Other examples as the one below are welcomed, like when you would use your own error classes. I really am wondering.
Ex: I'm using httparty to query our rails web service app for some data. It uses basic authentication. I'll paste both test code and implementation. What should my test expect, RuntimeError or SomeCustomError?
class MyIntegrationTest < Test::Unit::TestCase
context "connecting to someapp web service" do
should "raise not authorized if username is wrong" do
#get default MyWebserviceInterface instance, overriding username setting
ws_endpoint = build_integration_object(:username => 'wrong_username')
assert_raises RuntimeError do #TODO error design pattern?
ws_endpoint.get
end
end
end
end
The implementation:
class MyWebserviceInterface
include HTTParty
#Basic authentication and configurable base_uri
def initialize(u, p, uri)
@auth = {:username => u, :password => p}
@uri = uri
end
def base_uri
HTTParty.normalize_base_uri(@uri)
end
def get(path = '/somepath.xml', query_params = {})
opts = {:base_uri => base_uri, :query => query_params, :basic_auth => @auth}
response = self.class.get(path, opts)
evaluate_get_response(response)
response.parsed_response
end
def evaluate_get_response(response)
code = response.code
body = response.body
if code == 200
logger.debug "OK - CREATED code #{code}"
else
logger.error "expected code 200, got code #{code}. Response body: #{body}"
#TODO error design pattern? raise the above logged msg or a custom error?
raise SomeAppIntegration::Error(code, body)
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在大多数情况下,我永远不会拯救或引发
RuntimeError
。这可能与您的代码完全无关。最好使用自定义异常。一般来说,您可以根据需要调用任何错误,只要您在库常量中命名它们即可。例如,如果有人的用户名错误,您可以将
YourApp::InvalidUsername
作为异常对象,其定义如下:end
当您
raise YourApp::InvalidUsername
时,您将会看到该消息出现。In most cases, I would never rescue from or raise
RuntimeError
. That could be something completely unrelated to your code. It's best to use a custom exception.Generally, you can call errors whatever you want as long as you namespace them within your library's constant. For example, if someone gets their username wrong you could have
YourApp::InvalidUsername
as an exception object which would be defined like this:end
When you
raise YourApp::InvalidUsername
you'll see that message appear.