我想知道是否有一个库将从云端点抛出的异常映射回客户端的Exception
对象。服务器端抛出的异常表示为GoogleJsonResponseException
并包含真正原因的完整包名称:
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
"code": 401,
"errors": [
{
"domain": "global",
"location": "Authorization",
"locationType": "header",
"message": "com.google.appengine.api.oauth.OAuthRequestException: User not logged in",
"reason": "required"
}
],
"message": "com.google.appengine.api.oauth.OAuthRequestException: User not logged in"
}
我想写这样的客户端代码:
try {
// call endpoint
} catch (OAuthRequestException e) {
// handle exception
}
我认为可以编写这样的库。使用代码处理器和注释,生成具有适当异常的客户端库是可行的。也许客户端代码看起来不像下面的代码,但是像这样:
try {
Library.mapExceptions(/* call endpoint */);
} catch (OAuthRequestException e) {
// handle exception
}
那里有什么东西可以完成这项工作吗?
答案 0 :(得分:1)
我不知道这种类型的库,但您可以随时执行:
try {
...
} catch (IOException) {
if (e instanceof GoogleJsonResponseException){
GoogleJsonResponseException ex = e;
switch (ex.getStatusCode()){
case 400:
...
case 404:
...
/*and the rest of codes available through endpoints*/
}
} else {
/*Manage other exceptions, maybe connection issues?*/
}
}