Skip to content

Commit 19f9e7e

Browse files
committed
本地检测不通过、程序抛异常, 状态码设置为 0
1 parent aebc4ac commit 19f9e7e

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed

src/main/java/com/qiniu/api/config/Config.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
public class Config {
88
public static final String CHARSET = "utf-8";
9+
/**本地检测不通过、程序抛异常,设置 CallRet 的 statusCode 为此错误码*/
10+
public static final int ERROR_CODE = 0;
911

1012
public static String USER_AGENT="qiniu java-sdk v6.0.0";
1113

src/main/java/com/qiniu/api/io/IoApi.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private static PutRet put(String uptoken, String key, File file,
3030
PutExtra extra) {
3131

3232
if (!file.exists() || !file.canRead()) {
33-
return new PutRet(new CallRet(400, new Exception(
33+
return new PutRet(new CallRet(Config.ERROR_CODE, new Exception(
3434
"File does not exist or not readable.")));
3535
}
3636
MultipartEntity requestEntity = new MultipartEntity();
@@ -42,7 +42,7 @@ private static PutRet put(String uptoken, String key, File file,
4242
setParam(requestEntity, extra.params);
4343
if (extra.checkCrc != NO_CRC32) {
4444
if (extra.crc32 == 0) {
45-
return new PutRet(new CallRet(400, new Exception("no crc32 specified!")));
45+
return new PutRet(new CallRet(Config.ERROR_CODE, new Exception("no crc32 specified!")));
4646
}
4747
requestEntity.addPart("crc32", new StringBody(extra.crc32 + ""));
4848
}
@@ -54,7 +54,7 @@ private static PutRet put(String uptoken, String key, File file,
5454
}
5555
} catch (Exception e) {
5656
e.printStackTrace();
57-
return new PutRet(new CallRet(400, e));
57+
return new PutRet(new CallRet(Config.ERROR_CODE, e));
5858
}
5959

6060
String url = Config.UP_HOST;
@@ -95,13 +95,13 @@ private static PutRet putStream(String uptoken, String key, InputStream reader,P
9595
setParam(requestEntity, extra.params);
9696
if (extra.checkCrc != NO_CRC32) {
9797
if (extra.crc32 == 0) {
98-
return new PutRet(new CallRet(400, new Exception("no crc32 specified!")));
98+
return new PutRet(new CallRet(Config.ERROR_CODE, new Exception("no crc32 specified!")));
9999
}
100100
requestEntity.addPart("crc32", new StringBody(extra.crc32 + ""));
101101
}
102102
} catch (Exception e) {
103103
e.printStackTrace();
104-
return new PutRet(new CallRet(400, e));
104+
return new PutRet(new CallRet(Config.ERROR_CODE, e));
105105
}
106106

107107
String url = Config.UP_HOST;
@@ -138,7 +138,7 @@ public static PutRet putFile(String uptoken, String key, String fileName, PutExt
138138
try {
139139
extra.crc32 = getCRC32(file);
140140
} catch (Exception e) {
141-
return new PutRet(new CallRet(400, e));
141+
return new PutRet(new CallRet(Config.ERROR_CODE, e));
142142
}
143143
}
144144
return put(uptoken, key, file, extra);

src/main/java/com/qiniu/api/net/Client.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public CallRet call(String url) {
5151
return handleResult(response);
5252
} catch (Exception e) {
5353
e.printStackTrace();
54-
return new CallRet(400, e);
54+
return new CallRet(Config.ERROR_CODE, e);
5555
}
5656
}
5757

@@ -78,7 +78,7 @@ public CallRet call(String url, List<NameValuePair> nvps) {
7878
return handleResult(response);
7979
} catch (Exception e) {
8080
e.printStackTrace();
81-
return new CallRet(400, e);
81+
return new CallRet(Config.ERROR_CODE, e);
8282
}
8383
}
8484

@@ -99,7 +99,7 @@ public CallRet callWithBinary(String url, AbstractHttpEntity entity) {
9999
return handleResult(response);
100100
} catch (Exception e) {
101101
e.printStackTrace();
102-
return new CallRet(400, e);
102+
return new CallRet(Config.ERROR_CODE, e);
103103
}
104104
}
105105

@@ -142,7 +142,7 @@ public CallRet callWithMultiPart(String url, MultipartEntity requestEntity) {
142142
return handleResult(response);
143143
} catch (Exception e) {
144144
e.printStackTrace();
145-
return new CallRet(400, e);
145+
return new CallRet(Config.ERROR_CODE, e);
146146
}
147147
}
148148

@@ -155,19 +155,19 @@ public CallRet callWithMultiPart(String url, MultipartEntity requestEntity) {
155155
*/
156156
private CallRet handleResult(HttpResponse response) {
157157
if (response == null || response.getStatusLine() == null) {
158-
return new CallRet(400, "No response");
158+
return new CallRet(Config.ERROR_CODE, "No response");
159159
}
160160

161161
String responseBody;
162162
try {
163163
responseBody = EntityUtils.toString(response.getEntity(),"UTF-8");
164164
} catch (Exception e) {
165165
e.printStackTrace();
166-
return new CallRet(400, e);
166+
return new CallRet(Config.ERROR_CODE, e);
167167
}
168168

169169
StatusLine status = response.getStatusLine();
170-
int statusCode = (status == null) ? 400 : status.getStatusCode();
170+
int statusCode = (status == null) ? Config.ERROR_CODE : status.getStatusCode();
171171
return new CallRet(statusCode, responseBody);
172172
}
173173

src/main/java/com/qiniu/api/resumableio/UploadBlock.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.apache.http.client.HttpClient;
66
import org.apache.http.client.methods.HttpPost;
77

8+
import com.qiniu.api.config.Config;
9+
810

911
public abstract class UploadBlock {
1012
public static int CHUNK_SIZE = 1024 * 256;
@@ -78,7 +80,7 @@ private ChunkUploadCallRet upload(String url, int start,int len, int time) {
7880

7981
return checkAndRetryUpload(url, start, len, time, ret);
8082
} catch (Exception e) {
81-
return new ChunkUploadCallRet(400, e);
83+
return new ChunkUploadCallRet(Config.ERROR_CODE, e);
8284
}
8385
}
8486

src/main/java/com/qiniu/api/resumableio/Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static CallRet handleResult(HttpResponse response) {
2626
response.getEntity(), "utf-8");
2727
return new CallRet(statusCode, responseBody);
2828
} catch (Exception e) {
29-
CallRet ret = new CallRet(400, "can not load response.");
29+
CallRet ret = new CallRet(Config.ERROR_CODE, "can not load response.");
3030
ret.exception = e;
3131
return ret;
3232
}

0 commit comments

Comments
 (0)