From b15faab48618e0859a8b8d0a96daa0a7300dbe6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=A2=A6=E6=8A=80=E6=9C=AF?= <596392912@qq.com> Date: Thu, 16 Sep 2021 11:05:13 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20mica-http=20=E6=B7=BB=E5=8A=A0=20H?= =?UTF-8?q?ttpException=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/dreamlu/mica/http/AsyncCallback.java | 4 +- .../net/dreamlu/mica/http/AsyncExchange.java | 8 +-- .../java/net/dreamlu/mica/http/Exchange.java | 10 ++-- .../net/dreamlu/mica/http/HttpException.java | 56 +++++++++++++++++++ .../src/test/java/net/dreamlu/HttpTest.java | 24 ++++++++ 5 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 mica-http/src/main/java/net/dreamlu/mica/http/HttpException.java create mode 100644 mica-http/src/test/java/net/dreamlu/HttpTest.java diff --git a/mica-http/src/main/java/net/dreamlu/mica/http/AsyncCallback.java b/mica-http/src/main/java/net/dreamlu/mica/http/AsyncCallback.java index a77c266a..c33c6ff6 100644 --- a/mica-http/src/main/java/net/dreamlu/mica/http/AsyncCallback.java +++ b/mica-http/src/main/java/net/dreamlu/mica/http/AsyncCallback.java @@ -38,7 +38,7 @@ public class AsyncCallback implements Callback { @Override public void onFailure(Call call, IOException e) { - exchange.onFailure(call.request(), e); + exchange.onFailure(call.request(), new HttpException(e)); } @Override @@ -48,7 +48,7 @@ public class AsyncCallback implements Callback { if (response.isSuccessful()) { exchange.onSuccessful(httpResponse); } else { - exchange.onFailure(call.request(), new IOException(httpResponse.message())); + exchange.onFailure(call.request(), new HttpException(httpResponse)); } } } diff --git a/mica-http/src/main/java/net/dreamlu/mica/http/AsyncExchange.java b/mica-http/src/main/java/net/dreamlu/mica/http/AsyncExchange.java index b2e5b929..6648883a 100644 --- a/mica-http/src/main/java/net/dreamlu/mica/http/AsyncExchange.java +++ b/mica-http/src/main/java/net/dreamlu/mica/http/AsyncExchange.java @@ -30,11 +30,11 @@ import java.util.function.Consumer; */ public class AsyncExchange { private final static Consumer DEFAULT_CONSUMER = (r) -> {}; - private final static BiConsumer DEFAULT_FAIL_CONSUMER = (r, e) -> {}; + private final static BiConsumer DEFAULT_FAIL_CONSUMER = (r, e) -> {}; private final Call call; private Consumer successConsumer; private Consumer responseConsumer; - private BiConsumer failedBiConsumer; + private BiConsumer failedBiConsumer; AsyncExchange(Call call) { this.call = call; @@ -53,7 +53,7 @@ public class AsyncExchange { this.execute(); } - public AsyncExchange onFailed(BiConsumer biConsumer) { + public AsyncExchange onFailed(BiConsumer biConsumer) { this.failedBiConsumer = biConsumer; return this; } @@ -70,7 +70,7 @@ public class AsyncExchange { successConsumer.accept(httpResponse); } - protected void onFailure(Request request, IOException e) { + protected void onFailure(Request request, HttpException e) { failedBiConsumer.accept(request, e); } diff --git a/mica-http/src/main/java/net/dreamlu/mica/http/Exchange.java b/mica-http/src/main/java/net/dreamlu/mica/http/Exchange.java index e722c985..5a7ff95d 100644 --- a/mica-http/src/main/java/net/dreamlu/mica/http/Exchange.java +++ b/mica-http/src/main/java/net/dreamlu/mica/http/Exchange.java @@ -43,11 +43,11 @@ import java.util.function.Function; */ @RequiredArgsConstructor public class Exchange { - private BiConsumer failedBiConsumer = (r, e) -> { + private BiConsumer failedBiConsumer = (r, e) -> { }; private final Call call; - public Exchange onFailed(BiConsumer failConsumer) { + public Exchange onFailed(BiConsumer failConsumer) { this.failedBiConsumer = failConsumer; return this; } @@ -65,7 +65,7 @@ public class Exchange { try (HttpResponse response = new HttpResponse(call.execute())) { return func.apply(response); } catch (IOException e) { - failedBiConsumer.accept(call.request(), e); + failedBiConsumer.accept(call.request(), new HttpException(e)); return null; } } @@ -76,10 +76,10 @@ public class Exchange { if (response.isOk()) { return func.apply(response); } else { - failedBiConsumer.accept(call.request(), new IOException(response.toString())); + failedBiConsumer.accept(call.request(), new HttpException(response)); } } catch (IOException e) { - failedBiConsumer.accept(call.request(), e); + failedBiConsumer.accept(call.request(), new HttpException(e)); } return null; } diff --git a/mica-http/src/main/java/net/dreamlu/mica/http/HttpException.java b/mica-http/src/main/java/net/dreamlu/mica/http/HttpException.java new file mode 100644 index 00000000..5dbe94f9 --- /dev/null +++ b/mica-http/src/main/java/net/dreamlu/mica/http/HttpException.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2019-2029, Dreamlu (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.mica.http; + +import javax.annotation.Nullable; +import java.io.IOException; + +/** + * HttpException + * + * @author L.cm + */ +public class HttpException extends IOException { + @Nullable + private final ResponseSpec response; + + public HttpException(ResponseSpec response) { + super(response.toString()); + this.response = response; + } + + public HttpException(Throwable cause) { + super(cause); + this.response = null; + } + + @Nullable + public ResponseSpec getResponse() { + return response; + } + + @Override + public Throwable fillInStackTrace() { + Throwable cause = super.getCause(); + if (cause == null) { + return super.fillInStackTrace(); + } else { + return cause.fillInStackTrace(); + } + } + +} diff --git a/mica-http/src/test/java/net/dreamlu/HttpTest.java b/mica-http/src/test/java/net/dreamlu/HttpTest.java new file mode 100644 index 00000000..1c1f56d5 --- /dev/null +++ b/mica-http/src/test/java/net/dreamlu/HttpTest.java @@ -0,0 +1,24 @@ +package net.dreamlu; + +import net.dreamlu.mica.http.HttpRequest; +import net.dreamlu.mica.http.LogLevel; +import net.dreamlu.mica.http.ResponseSpec; + +public class HttpTest { + + public static void main(String[] args) { + String html = HttpRequest.get("https://wwww.baiduxxx.com/123123") + .useSlf4jLog(LogLevel.NONE) + .execute() + .onFailed((request, e) -> { + e.printStackTrace(); + ResponseSpec response = e.getResponse(); + if (response != null) { + System.out.println(response.asString()); + } + }) + .onSuccessful(ResponseSpec::asString); + System.out.println(html); + } + +} -- GitLab