Skip to content

Commit 1bd84f0

Browse files
authored
🎨 格式化代码/异常代码改为500/返回异常信息 (#12)
* 🎨 格式化代码/异常代码改为500/返回异常信息
1 parent 0313341 commit 1bd84f0

File tree

7 files changed

+25
-32
lines changed

7 files changed

+25
-32
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<dependency>
1616
<groupId>com.xkcoding.http</groupId>
1717
<artifactId>simple-http</artifactId>
18-
<version>1.0.4</version>
18+
<version>1.0.5</version>
1919
</dependency>
2020
```
2121

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.xkcoding.http</groupId>
88
<artifactId>simple-http</artifactId>
9-
<version>1.0.4</version>
9+
<version>1.0.5</version>
1010

1111
<name>${project.artifactId}</name>
1212
<url>https://github.com/xkcoding/simple-http</url>

src/main/java/com/xkcoding/http/support/SimpleHttpResponse.java

+1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ public class SimpleHttpResponse {
2323
private int code;
2424
private Map<String, List<String>> headers;
2525
private String body;
26+
private String error;
2627
}

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

+6-9
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616

1717
package com.xkcoding.http.support.httpclient;
1818

19-
import com.xkcoding.http.support.SimpleHttpResponse;
2019
import com.xkcoding.http.config.HttpConfig;
2120
import com.xkcoding.http.constants.Constants;
22-
import com.xkcoding.http.exception.SimpleHttpException;
2321
import com.xkcoding.http.support.AbstractHttp;
2422
import com.xkcoding.http.support.HttpHeader;
23+
import com.xkcoding.http.support.SimpleHttpResponse;
2524
import com.xkcoding.http.util.MapUtil;
2625
import com.xkcoding.http.util.StringUtil;
2726
import org.apache.http.Header;
28-
import org.apache.http.HeaderElement;
2927
import org.apache.http.HttpHost;
3028
import org.apache.http.NameValuePair;
3129
import org.apache.http.client.config.RequestConfig;
@@ -40,7 +38,6 @@
4038
import org.apache.http.message.BasicNameValuePair;
4139
import org.apache.http.util.EntityUtils;
4240

43-
import java.io.IOException;
4441
import java.net.InetSocketAddress;
4542
import java.net.Proxy;
4643
import java.util.ArrayList;
@@ -95,16 +92,16 @@ private SimpleHttpResponse exec(HttpRequestBase request) {
9592
}
9693

9794
int code = response.getStatusLine().getStatusCode();
98-
boolean success = isSuccess(response);
95+
boolean successful = isSuccess(response);
9996
Map<String, List<String>> headers = Arrays.stream(response.getAllHeaders()).collect(Collectors.toMap(Header::getName, (value) -> {
10097
ArrayList<String> headerValue = new ArrayList<>();
10198
headerValue.add(value.getValue());
10299
return headerValue;
103-
},(oldValue,newValue)->newValue));
104-
return new SimpleHttpResponse(success,code,headers, body.toString());
105-
} catch (IOException e) {
100+
}, (oldValue, newValue) -> newValue));
101+
return new SimpleHttpResponse(successful, code, headers, body.toString(), null);
102+
} catch (Exception e) {
106103
e.printStackTrace();
107-
return new SimpleHttpResponse(false,400,null,null);
104+
return new SimpleHttpResponse(false, 500, null, null, e.getMessage());
108105
}
109106
}
110107

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818

1919
import cn.hutool.http.HttpRequest;
2020
import cn.hutool.http.HttpResponse;
21-
import com.xkcoding.http.exception.SimpleHttpException;
22-
import com.xkcoding.http.support.SimpleHttpResponse;
2321
import com.xkcoding.http.config.HttpConfig;
2422
import com.xkcoding.http.support.AbstractHttp;
2523
import com.xkcoding.http.support.HttpHeader;
24+
import com.xkcoding.http.support.SimpleHttpResponse;
2625
import com.xkcoding.http.util.MapUtil;
2726
import com.xkcoding.http.util.StringUtil;
2827
import com.xkcoding.http.util.UrlUtil;
2928

30-
import java.io.IOException;
3129
import java.util.List;
3230
import java.util.Map;
3331

@@ -61,10 +59,10 @@ private SimpleHttpResponse exec(HttpRequest request) {
6159
boolean successful = response.isOk();
6260
String body = response.body();
6361
Map<String, List<String>> headers = response.headers();
64-
return new SimpleHttpResponse(successful, code, headers, body);
65-
}catch (RuntimeException e){
62+
return new SimpleHttpResponse(successful, code, headers, body, null);
63+
} catch (Exception e) {
6664
e.printStackTrace();
67-
return new SimpleHttpResponse(false,400,null,null);
65+
return new SimpleHttpResponse(false, 500, null, null, e.getMessage());
6866
}
6967
}
7068

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

+6-8
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616

1717
package com.xkcoding.http.support.java11;
1818

19-
import com.xkcoding.http.support.SimpleHttpResponse;
2019
import com.xkcoding.http.config.HttpConfig;
2120
import com.xkcoding.http.constants.Constants;
22-
import com.xkcoding.http.exception.SimpleHttpException;
2321
import com.xkcoding.http.support.AbstractHttp;
2422
import com.xkcoding.http.support.HttpHeader;
23+
import com.xkcoding.http.support.SimpleHttpResponse;
2524
import com.xkcoding.http.util.MapUtil;
2625
import com.xkcoding.http.util.StringUtil;
2726

28-
import java.io.IOException;
2927
import java.net.URI;
3028
import java.net.http.HttpClient;
3129
import java.net.http.HttpRequest;
@@ -69,17 +67,17 @@ private SimpleHttpResponse exec(HttpRequest.Builder builder) {
6967
HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
7068

7169
int code = httpResponse.statusCode();
72-
boolean success = isSuccess(httpResponse);
70+
boolean successful = isSuccess(httpResponse);
7371
Map<String, List<String>> headers = httpResponse.headers().map();
7472
String body = httpResponse.body();
75-
return new SimpleHttpResponse(success,code,headers,body);
76-
} catch (IOException | InterruptedException e) {
73+
return new SimpleHttpResponse(successful, code, headers, body, null);
74+
} catch (Exception e) {
7775
e.printStackTrace();
78-
return new SimpleHttpResponse(false,400,null,null);
76+
return new SimpleHttpResponse(false, 500, null, null, e.getMessage());
7977
}
8078
}
8179

82-
private boolean isSuccess(HttpResponse response) {
80+
private boolean isSuccess(HttpResponse<String> response) {
8381
if (response == null) {
8482
return false;
8583
}

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,15 @@
1616

1717
package com.xkcoding.http.support.okhttp3;
1818

19-
import com.xkcoding.http.support.SimpleHttpResponse;
2019
import com.xkcoding.http.config.HttpConfig;
2120
import com.xkcoding.http.constants.Constants;
22-
import com.xkcoding.http.exception.SimpleHttpException;
2321
import com.xkcoding.http.support.AbstractHttp;
2422
import com.xkcoding.http.support.HttpHeader;
23+
import com.xkcoding.http.support.SimpleHttpResponse;
2524
import com.xkcoding.http.util.MapUtil;
2625
import com.xkcoding.http.util.StringUtil;
2726
import okhttp3.*;
2827

29-
import java.io.IOException;
3028
import java.time.Duration;
3129
import java.util.List;
3230
import java.util.Map;
@@ -73,11 +71,12 @@ private SimpleHttpResponse exec(Request.Builder requestBuilder) {
7371
int code = response.code();
7472
boolean successful = response.isSuccessful();
7573
Map<String, List<String>> headers = response.headers().toMultimap();
76-
String body = response.body().string();
77-
return new SimpleHttpResponse(successful,code,headers,body);
78-
} catch (IOException e) {
74+
ResponseBody responseBody = response.body();
75+
String body = null == responseBody ? null : responseBody.string();
76+
return new SimpleHttpResponse(successful, code, headers, body, null);
77+
} catch (Exception e) {
7978
e.printStackTrace();
80-
return new SimpleHttpResponse(false,400,null,null);
79+
return new SimpleHttpResponse(false, 500, null, null, e.getMessage());
8180
}
8281
}
8382

0 commit comments

Comments
 (0)