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

throw unchecked exception on error happened when iterate files #525

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
75 changes: 75 additions & 0 deletions src/main/java/com/qiniu/common/UncheckedQiniuException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.qiniu.common;

import com.qiniu.http.Error;
import com.qiniu.http.Response;


/**
* 七牛SDK异常封装类,封装了http响应数据
*/
public final class UncheckedQiniuException extends RuntimeException {

public final Response response;
private String error;
private boolean isUnrecoverable = false;


public UncheckedQiniuException(Response response) {
super(response != null ? response.getInfo() : null);
this.response = response;
if (response != null) {
response.close();
}
}

public static UncheckedQiniuException unrecoverable(Exception e) {
UncheckedQiniuException exception = new UncheckedQiniuException(e);
exception.isUnrecoverable = true;
return exception;
}

public static UncheckedQiniuException unrecoverable(String msg) {
UncheckedQiniuException exception = new UncheckedQiniuException(null, msg);
exception.isUnrecoverable = true;
return exception;
}

public UncheckedQiniuException(Exception e) {
this(e, null);
}

public UncheckedQiniuException(Exception e, String msg) {
super(msg != null ? msg : (e != null ? e.getMessage() : null), e);
this.response = null;
this.error = msg;
}

public String url() {
return response != null ? response.url() : "";
}

public int code() {
return response != null ? response.statusCode : Response.NetworkError;
}

public boolean isUnrecoverable() {
return isUnrecoverable;
}

public String error() {
if (error != null) {
return error;
}
if (response == null || response.statusCode / 100 == 2 || !response.isJson()) {
return null;
}
Error e = null;
try {
e = response.jsonToObject(Error.class);
} catch (QiniuException e1) {
e1.printStackTrace();
}
error = e == null ? "" : e.error;
return error;
}
}
18 changes: 7 additions & 11 deletions src/main/java/com/qiniu/storage/BucketManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.gson.JsonObject;
import com.qiniu.common.Constants;
import com.qiniu.common.QiniuException;
import com.qiniu.common.UncheckedQiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.storage.model.*;
Expand Down Expand Up @@ -1239,11 +1240,10 @@ public String execBucket() {
*/
public class FileListIterator implements Iterator<FileInfo[]> {
private String marker = null;
private String bucket;
private String delimiter;
private int limit;
private String prefix;
private QiniuException exception = null;
private final String bucket;
private final String delimiter;
private final int limit;
private final String prefix;

public FileListIterator(String bucket, String prefix, int limit, String delimiter) {
if (limit <= 0) {
Expand All @@ -1258,13 +1258,10 @@ public FileListIterator(String bucket, String prefix, int limit, String delimite
this.delimiter = delimiter;
}

public QiniuException error() {
return exception;
}

@Override
public boolean hasNext() {
return exception == null && !"".equals(marker);
return "".equals(marker);
}

@Override
Expand All @@ -1274,8 +1271,7 @@ public FileInfo[] next() {
this.marker = f.marker == null ? "" : f.marker;
return f.items;
} catch (QiniuException e) {
this.exception = e;
return null;
throw new UncheckedQiniuException(e, e.getMessage());
}
}

Expand Down