From 47b29fd376bee2f9af2f734dd1adb02085125641 Mon Sep 17 00:00:00 2001 From: xunyulin Date: Wed, 9 Jun 2021 15:46:20 +0800 Subject: [PATCH] throw unchecked excetpion on error happened when iterate files --- .../qiniu/common/UncheckedQiniuException.java | 75 +++++++++++++++++++ .../java/com/qiniu/storage/BucketManager.java | 18 ++--- 2 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/qiniu/common/UncheckedQiniuException.java diff --git a/src/main/java/com/qiniu/common/UncheckedQiniuException.java b/src/main/java/com/qiniu/common/UncheckedQiniuException.java new file mode 100644 index 00000000..be7492b3 --- /dev/null +++ b/src/main/java/com/qiniu/common/UncheckedQiniuException.java @@ -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; + } +} diff --git a/src/main/java/com/qiniu/storage/BucketManager.java b/src/main/java/com/qiniu/storage/BucketManager.java index 60ecbc0a..58659d19 100644 --- a/src/main/java/com/qiniu/storage/BucketManager.java +++ b/src/main/java/com/qiniu/storage/BucketManager.java @@ -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.*; @@ -1239,11 +1240,10 @@ public String execBucket() { */ public class FileListIterator implements Iterator { 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) { @@ -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 @@ -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()); } }