diff --git a/src/main/java/com/qiniu/api/config/Config.java b/src/main/java/com/qiniu/api/config/Config.java index 1e8922ed4..3ccbefd57 100644 --- a/src/main/java/com/qiniu/api/config/Config.java +++ b/src/main/java/com/qiniu/api/config/Config.java @@ -6,6 +6,8 @@ */ public class Config { public static final String CHARSET = "utf-8"; + /**本地检测不通过、程序抛异常,设置 CallRet 的 statusCode 为此错误码*/ + public static final int ERROR_CODE = 0; public static String USER_AGENT="qiniu java-sdk v6.0.0"; diff --git a/src/main/java/com/qiniu/api/io/IoApi.java b/src/main/java/com/qiniu/api/io/IoApi.java index fbd099045..ad5169af7 100644 --- a/src/main/java/com/qiniu/api/io/IoApi.java +++ b/src/main/java/com/qiniu/api/io/IoApi.java @@ -30,7 +30,7 @@ private static PutRet put(String uptoken, String key, File file, PutExtra extra) { if (!file.exists() || !file.canRead()) { - return new PutRet(new CallRet(400, new Exception( + return new PutRet(new CallRet(Config.ERROR_CODE, new Exception( "File does not exist or not readable."))); } MultipartEntity requestEntity = new MultipartEntity(); @@ -42,7 +42,7 @@ private static PutRet put(String uptoken, String key, File file, setParam(requestEntity, extra.params); if (extra.checkCrc != NO_CRC32) { if (extra.crc32 == 0) { - return new PutRet(new CallRet(400, new Exception("no crc32 specified!"))); + return new PutRet(new CallRet(Config.ERROR_CODE, new Exception("no crc32 specified!"))); } requestEntity.addPart("crc32", new StringBody(extra.crc32 + "")); } @@ -54,7 +54,7 @@ private static PutRet put(String uptoken, String key, File file, } } catch (Exception e) { e.printStackTrace(); - return new PutRet(new CallRet(400, e)); + return new PutRet(new CallRet(Config.ERROR_CODE, e)); } String url = Config.UP_HOST; @@ -95,13 +95,13 @@ private static PutRet putStream(String uptoken, String key, InputStream reader,P setParam(requestEntity, extra.params); if (extra.checkCrc != NO_CRC32) { if (extra.crc32 == 0) { - return new PutRet(new CallRet(400, new Exception("no crc32 specified!"))); + return new PutRet(new CallRet(Config.ERROR_CODE, new Exception("no crc32 specified!"))); } requestEntity.addPart("crc32", new StringBody(extra.crc32 + "")); } } catch (Exception e) { e.printStackTrace(); - return new PutRet(new CallRet(400, e)); + return new PutRet(new CallRet(Config.ERROR_CODE, e)); } String url = Config.UP_HOST; @@ -138,7 +138,7 @@ public static PutRet putFile(String uptoken, String key, String fileName, PutExt try { extra.crc32 = getCRC32(file); } catch (Exception e) { - return new PutRet(new CallRet(400, e)); + return new PutRet(new CallRet(Config.ERROR_CODE, e)); } } return put(uptoken, key, file, extra); diff --git a/src/main/java/com/qiniu/api/net/Client.java b/src/main/java/com/qiniu/api/net/Client.java index 77bc7b8f3..0d20a9ff3 100644 --- a/src/main/java/com/qiniu/api/net/Client.java +++ b/src/main/java/com/qiniu/api/net/Client.java @@ -51,7 +51,7 @@ public CallRet call(String url) { return handleResult(response); } catch (Exception e) { e.printStackTrace(); - return new CallRet(400, e); + return new CallRet(Config.ERROR_CODE, e); } } @@ -78,7 +78,7 @@ public CallRet call(String url, List nvps) { return handleResult(response); } catch (Exception e) { e.printStackTrace(); - return new CallRet(400, e); + return new CallRet(Config.ERROR_CODE, e); } } @@ -99,7 +99,7 @@ public CallRet callWithBinary(String url, AbstractHttpEntity entity) { return handleResult(response); } catch (Exception e) { e.printStackTrace(); - return new CallRet(400, e); + return new CallRet(Config.ERROR_CODE, e); } } @@ -142,7 +142,7 @@ public CallRet callWithMultiPart(String url, MultipartEntity requestEntity) { return handleResult(response); } catch (Exception e) { e.printStackTrace(); - return new CallRet(400, e); + return new CallRet(Config.ERROR_CODE, e); } } @@ -155,7 +155,7 @@ public CallRet callWithMultiPart(String url, MultipartEntity requestEntity) { */ private CallRet handleResult(HttpResponse response) { if (response == null || response.getStatusLine() == null) { - return new CallRet(400, "No response"); + return new CallRet(Config.ERROR_CODE, "No response"); } String responseBody; @@ -163,11 +163,11 @@ private CallRet handleResult(HttpResponse response) { responseBody = EntityUtils.toString(response.getEntity(),"UTF-8"); } catch (Exception e) { e.printStackTrace(); - return new CallRet(400, e); + return new CallRet(Config.ERROR_CODE, e); } StatusLine status = response.getStatusLine(); - int statusCode = (status == null) ? 400 : status.getStatusCode(); + int statusCode = (status == null) ? Config.ERROR_CODE : status.getStatusCode(); return new CallRet(statusCode, responseBody); } diff --git a/src/main/java/com/qiniu/api/resumableio/UploadBlock.java b/src/main/java/com/qiniu/api/resumableio/UploadBlock.java index 77d6357c1..a4e9b965c 100644 --- a/src/main/java/com/qiniu/api/resumableio/UploadBlock.java +++ b/src/main/java/com/qiniu/api/resumableio/UploadBlock.java @@ -5,6 +5,8 @@ import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; +import com.qiniu.api.config.Config; + public abstract class UploadBlock { public static int CHUNK_SIZE = 1024 * 256; @@ -78,7 +80,7 @@ private ChunkUploadCallRet upload(String url, int start,int len, int time) { return checkAndRetryUpload(url, start, len, time, ret); } catch (Exception e) { - return new ChunkUploadCallRet(400, e); + return new ChunkUploadCallRet(Config.ERROR_CODE, e); } } diff --git a/src/main/java/com/qiniu/api/resumableio/Util.java b/src/main/java/com/qiniu/api/resumableio/Util.java index e477a70e2..0d1f77ede 100644 --- a/src/main/java/com/qiniu/api/resumableio/Util.java +++ b/src/main/java/com/qiniu/api/resumableio/Util.java @@ -26,7 +26,7 @@ public static CallRet handleResult(HttpResponse response) { response.getEntity(), "utf-8"); return new CallRet(statusCode, responseBody); } catch (Exception e) { - CallRet ret = new CallRet(400, "can not load response."); + CallRet ret = new CallRet(Config.ERROR_CODE, "can not load response."); ret.exception = e; return ret; }