Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

本地检测不通过、程序抛异常, 状态码设置为 0 #117

Merged
merged 2 commits into from
Jun 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/com/qiniu/api/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/qiniu/api/io/IoApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 + ""));
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/qiniu/api/net/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -78,7 +78,7 @@ public CallRet call(String url, List<NameValuePair> nvps) {
return handleResult(response);
} catch (Exception e) {
e.printStackTrace();
return new CallRet(400, e);
return new CallRet(Config.ERROR_CODE, e);
}
}

Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -155,19 +155,19 @@ 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;
try {
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);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/qiniu/api/resumableio/UploadBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/api/resumableio/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down