use of retrofit2.HttpException in project BaseProject by fly803.
the class ExceptionEngine method handleException.
public static ApiException handleException(Throwable e) {
ApiException ex;
if (e instanceof HttpException) {
// HTTP错误
String httpExceptionMessage = "";
HttpException httpException = (HttpException) e;
ex = new ApiException(e, ERROR.HTTP_ERROR);
switch(httpException.code()) {
case UNAUTHORIZED:
httpExceptionMessage = UNAUTHORIZED + " " + "UNAUTHORIZED";
break;
case FORBIDDEN:
httpExceptionMessage = FORBIDDEN + " " + "FORBIDDEN";
break;
case NOT_FOUND:
httpExceptionMessage = NOT_FOUND + " " + "NOT_FOUND";
break;
case REQUEST_TIMEOUT:
httpExceptionMessage = REQUEST_TIMEOUT + " " + "REQUEST_TIMEOUT";
break;
case GATEWAY_TIMEOUT:
httpExceptionMessage = GATEWAY_TIMEOUT + " " + "GATEWAY_TIMEOUT";
break;
case INTERNAL_SERVER_ERROR:
httpExceptionMessage = INTERNAL_SERVER_ERROR + " " + "INTERNAL_SERVER_ERROR";
break;
case BAD_GATEWAY:
httpExceptionMessage = BAD_GATEWAY + " " + "BAD_GATEWAY";
break;
case SERVICE_UNAVAILABLE:
httpExceptionMessage = SERVICE_UNAVAILABLE + " " + "SERVICE_UNAVAILABLE";
break;
default:
httpExceptionMessage = "其他";
break;
}
// 均视为网络错误
ex.setMessage("网络错误:" + httpExceptionMessage);
return ex;
} else if (e instanceof ServerException) {
// 服务器返回的错误
ServerException resultException = (ServerException) e;
ex = new ApiException(resultException, resultException.getCode());
ex.setMessage(resultException.getMsg());
return ex;
} else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
ex = new ApiException(e, ERROR.PARSE_ERROR);
// 均视为解析错误
ex.setMessage("ParseException解析错误");
return ex;
} else if (e instanceof ConnectException) {
ex = new ApiException(e, ERROR.NETWORD_ERROR);
// 均视为网络错误
ex.setMessage("连接失败");
return ex;
} else if (e instanceof javax.net.ssl.SSLHandshakeException) {
ex = new ApiException(e, ERROR.SSL_ERROR);
ex.setMessage("证书验证失败");
return ex;
} else {
ex = new ApiException(e, ERROR.UNKNOWN);
// 未知错误
ex.setMessage("ExceptionEngine 未知错误");
return ex;
}
}
Aggregations