Skip to content

Commit 96831f4

Browse files
committed
♻️ 重构,添加自定义异常,方便捕获
1 parent 2eef6d5 commit 96831f4

File tree

8 files changed

+69
-10
lines changed

8 files changed

+69
-10
lines changed

src/main/java/com/xkcoding/http/HttpUtil.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.xkcoding.http;
1818

19+
import com.xkcoding.http.exception.SimpleHttpException;
1920
import com.xkcoding.http.support.Http;
2021
import com.xkcoding.http.support.HttpHeader;
2122
import com.xkcoding.http.support.httpclient.HttpClientImpl;
@@ -63,6 +64,12 @@ else if (ClassUtil.isPresent("cn.hutool.http.HttpRequest", classLoader)) {
6364
public void setHttp(Http http) {
6465
proxy = http;
6566
}
67+
68+
private void checkHttpNotNull(Http proxy) {
69+
if (null == proxy) {
70+
throw new SimpleHttpException("HTTP 实现类未指定!");
71+
}
72+
}
6673

6774
/**
6875
* GET 请求
@@ -71,6 +78,7 @@ public void setHttp(Http http) {
7178
* @return 结果
7279
*/
7380
public String get(String url) {
81+
checkHttpNotNull(proxy);
7482
return proxy.get(url);
7583
}
7684

@@ -83,6 +91,7 @@ public String get(String url) {
8391
* @return 结果
8492
*/
8593
public String get(String url, Map<String, String> params, boolean encode) {
94+
checkHttpNotNull(proxy);
8695
return proxy.get(url, params, encode);
8796
}
8897

@@ -96,6 +105,7 @@ public String get(String url, Map<String, String> params, boolean encode) {
96105
* @return 结果
97106
*/
98107
public String get(String url, Map<String, String> params, HttpHeader header, boolean encode) {
108+
checkHttpNotNull(proxy);
99109
return proxy.get(url, params, header, encode);
100110
}
101111

@@ -106,6 +116,7 @@ public String get(String url, Map<String, String> params, HttpHeader header, boo
106116
* @return 结果
107117
*/
108118
public String post(String url) {
119+
checkHttpNotNull(proxy);
109120
return proxy.post(url);
110121
}
111122

@@ -117,6 +128,7 @@ public String post(String url) {
117128
* @return 结果
118129
*/
119130
public String post(String url, String data) {
131+
checkHttpNotNull(proxy);
120132
return proxy.post(url, data);
121133
}
122134

@@ -129,6 +141,7 @@ public String post(String url, String data) {
129141
* @return 结果
130142
*/
131143
public String post(String url, String data, HttpHeader header) {
144+
checkHttpNotNull(proxy);
132145
return proxy.post(url, data, header);
133146
}
134147

@@ -141,6 +154,7 @@ public String post(String url, String data, HttpHeader header) {
141154
* @return 结果
142155
*/
143156
public String post(String url, Map<String, String> params, boolean encode) {
157+
checkHttpNotNull(proxy);
144158
return proxy.post(url, params, encode);
145159
}
146160

@@ -154,6 +168,7 @@ public String post(String url, Map<String, String> params, boolean encode) {
154168
* @return 结果
155169
*/
156170
public String post(String url, Map<String, String> params, HttpHeader header, boolean encode) {
171+
checkHttpNotNull(proxy);
157172
return proxy.post(url, params, header, encode);
158173
}
159174
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com).
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.xkcoding.http.exception;
18+
19+
/**
20+
* <p>
21+
* 自定义异常
22+
* </p>
23+
*
24+
* @author yangkai.shen
25+
* @date Created in 2019/12/25 17:40
26+
*/
27+
public class SimpleHttpException extends RuntimeException {
28+
public SimpleHttpException(Throwable cause) {
29+
super(cause);
30+
}
31+
32+
public SimpleHttpException(String message) {
33+
super(message);
34+
}
35+
36+
public SimpleHttpException(String message, Throwable cause) {
37+
super(message, cause);
38+
}
39+
}

src/main/java/com/xkcoding/http/support/httpclient/HttpClientImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.xkcoding.http.support.httpclient;
1818

1919
import com.xkcoding.http.constants.Constants;
20+
import com.xkcoding.http.exception.SimpleHttpException;
2021
import com.xkcoding.http.support.Http;
2122
import com.xkcoding.http.support.HttpHeader;
2223
import com.xkcoding.http.util.MapUtil;
@@ -68,7 +69,7 @@ private String exec(HttpRequestBase request) {
6869

6970
try (CloseableHttpResponse response = this.httpClient.execute(request)) {
7071
if (!isSuccess(response)) {
71-
throw new RuntimeException("Unexpected code " + response);
72+
throw new SimpleHttpException("Unexpected code " + response);
7273
}
7374

7475
StringBuffer body = new StringBuffer();
@@ -78,7 +79,7 @@ private String exec(HttpRequestBase request) {
7879

7980
return body.toString();
8081
} catch (IOException e) {
81-
throw new RuntimeException(e);
82+
throw new SimpleHttpException(e);
8283
}
8384
}
8485

src/main/java/com/xkcoding/http/support/hutool/HutoolImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import cn.hutool.http.HttpRequest;
2020
import cn.hutool.http.HttpResponse;
2121
import com.xkcoding.http.constants.Constants;
22+
import com.xkcoding.http.exception.SimpleHttpException;
2223
import com.xkcoding.http.support.Http;
2324
import com.xkcoding.http.support.HttpHeader;
2425
import com.xkcoding.http.util.MapUtil;
@@ -40,7 +41,7 @@ private String exec(HttpRequest request) {
4041
request = request.timeout(Constants.TIMEOUT);
4142
try (HttpResponse response = request.execute()) {
4243
if (!response.isOk()) {
43-
throw new RuntimeException("Unexpected code " + response);
44+
throw new SimpleHttpException("Unexpected code " + response);
4445
}
4546

4647
return response.body();

src/main/java/com/xkcoding/http/support/java11/HttpClientImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.xkcoding.http.support.java11;
1818

1919
import com.xkcoding.http.constants.Constants;
20+
import com.xkcoding.http.exception.SimpleHttpException;
2021
import com.xkcoding.http.support.Http;
2122
import com.xkcoding.http.support.HttpHeader;
2223
import com.xkcoding.http.util.MapUtil;
@@ -50,7 +51,7 @@ private String exec(HttpRequest request) {
5051
try {
5152
return client.send(request, HttpResponse.BodyHandlers.ofString()).body();
5253
} catch (IOException | InterruptedException e) {
53-
throw new RuntimeException(e);
54+
throw new SimpleHttpException(e);
5455
}
5556
}
5657

src/main/java/com/xkcoding/http/support/okhttp3/OkHttp3Impl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.xkcoding.http.support.okhttp3;
1818

1919
import com.xkcoding.http.constants.Constants;
20+
import com.xkcoding.http.exception.SimpleHttpException;
2021
import com.xkcoding.http.support.Http;
2122
import com.xkcoding.http.support.HttpHeader;
2223
import com.xkcoding.http.util.MapUtil;
@@ -55,12 +56,12 @@ public OkHttp3Impl(OkHttpClient httpClient) {
5556
private String exec(Request request) {
5657
try (Response response = httpClient.newCall(request).execute()) {
5758
if (!response.isSuccessful()) {
58-
throw new RuntimeException("Unexpected code " + response);
59+
throw new SimpleHttpException("Unexpected code " + response);
5960
}
6061

6162
return response.body().string();
6263
} catch (IOException e) {
63-
throw new RuntimeException(e);
64+
throw new SimpleHttpException(e);
6465
}
6566
}
6667

src/main/java/com/xkcoding/http/util/StringUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
@UtilityClass
3030
public class StringUtil {
3131

32-
public static boolean isEmpty(String str) {
32+
public boolean isEmpty(String str) {
3333
return null == str || str.trim().isEmpty();
3434
}
3535

36-
public static boolean isNotEmpty(String str) {
36+
public boolean isNotEmpty(String str) {
3737
return !isEmpty(str);
3838
}
3939

src/main/java/com/xkcoding/http/util/UrlUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import cn.hutool.core.exceptions.UtilException;
2020
import com.xkcoding.http.constants.Constants;
21+
import com.xkcoding.http.exception.SimpleHttpException;
2122
import lombok.experimental.UtilityClass;
2223

2324
import java.io.UnsupportedEncodingException;
@@ -48,7 +49,7 @@ public String urlEncode(String value) {
4849
String encoded = URLEncoder.encode(value, Constants.DEFAULT_ENCODING.displayName());
4950
return encoded.replace("+", "%20").replace("*", "%2A").replace("~", "%7E").replace("/", "%2F");
5051
} catch (UnsupportedEncodingException e) {
51-
throw new RuntimeException("Failed To Encode Uri", e);
52+
throw new SimpleHttpException("Failed To Encode Uri", e);
5253
}
5354
}
5455

@@ -67,7 +68,7 @@ public String urlDecode(String url) throws UtilException {
6768
try {
6869
return URLDecoder.decode(url, Constants.DEFAULT_ENCODING.displayName());
6970
} catch (UnsupportedEncodingException e) {
70-
throw new RuntimeException("Unsupported encoding", e);
71+
throw new SimpleHttpException("Unsupported encoding", e);
7172
}
7273
}
7374
}

0 commit comments

Comments
 (0)