Java 调试改造错误消息的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21071952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The Best Way to Debug Retrofit Error Messages
提问by user3186731
I'm new to Retrofit. I have used Volley, and I kind of like Retrofit. I was just about to select Retrofit when I ran into this very non-descriptiveerror message when trying to do a POST.
我是Retrofit 的新手。我用过 Volley,我有点喜欢 Retrofit。当我在尝试执行 POST时遇到这个非常非描述性的错误消息时,我正准备选择 Retrofit 。
Exception in thread "main" retrofit.RetrofitError
at retrofit.RetrofitError.httpError(RetrofitError.java:37)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:413)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:282)
at myapi.api.$Proxy7.logon(Unknown Source)
at myapi.api.TestDriver.main(TestDriver.java:94)
Well, I must say that this type of error message is about as useful as a warm Hymanet in the Sahara.
好吧,我必须说,这种类型的错误信息与撒哈拉沙漠中的保暖夹克一样有用。
Does anyone even know where to begin with debugging this type of message? I really am not about to delegate to a REST api that does not provide useful error messages.
有谁知道从哪里开始调试这种类型的消息?我真的不打算委托给不提供有用错误消息的 REST api。
采纳答案by Jesse Wilson
You probably want to add a catch clause to TestDriver.main
:
您可能想在以下位置添加一个 catch 子句TestDriver.main
:
try {
service.logon();
} catch (RetrofitError e) {
System.out.println(e.getResponse().getStatus());
}
回答by Joshua Pinter
Create a custom ErrorHandler
for Retrofit.
ErrorHandler
为 Retrofit创建自定义。
I found that catching the error didn't provide a whole lot of extra information but creating a custom ErrorHandler
for Retrofit allowed me to dig deeper into the actual error, like so:
我发现捕获错误并没有提供大量额外信息,但是ErrorHandler
为 Retrofit创建自定义允许我更深入地挖掘实际错误,如下所示:
class MyErrorHandler implements ErrorHandler {
@Override public Throwable handleError(RetrofitError cause) {
Response r = cause.getResponse();
if (r != null && r.getStatus() == 401) {
return new UnauthorizedException(cause);
}
return cause;
}
}
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.github.com")
.setErrorHandler(new MyErrorHandler())
.setLogLevel(RestAdapter.LogLevel.FULL) // Do this for development too.
.build();
From the Custom Synchronous Error Handlingsection on Retrofit Page.
来自Retrofit Page上的自定义同步错误处理部分。
Set the Log Level to FULLas well, as shown in above config code.
也将日志级别设置为FULL,如上面的配置代码所示。
回答by Kalel Wade
Unfortunately RetrofitError (1.6.1) was tricky. 401's made getResponse() always return null which made it hard to tell if its a connection issue or authentication issue. At least for me, I had to look at the message to get the 401 error. Hopefully this helps someone else trying to do something similar.
不幸的是 RetrofitError (1.6.1) 很棘手。401 的 getResponse() 总是返回 null,这使得很难判断它是连接问题还是身份验证问题。至少对我来说,我必须查看消息才能获得 401 错误。希望这有助于其他人尝试做类似的事情。
public class RetrofitErrorHandler implements ErrorHandler {
@Override
public Throwable handleError(RetrofitError cause) {
if (cause.isNetworkError()) {
if(cause.getMessage().contains("authentication")){
//401 errors
return new Exception("Invalid credentials. Please verify login info.");
}else if (cause.getCause() instanceof SocketTimeoutException) {
//Socket Timeout
return new SocketTimeoutException("Connection Timeout. " +
"Please verify your internet connection.");
} else {
//No Connection
return new ConnectException("No Connection. " +
"Please verify your internet connection.");
}
} else {
return cause;
}
}
}