Skip to content

Commit 1156872

Browse files
author
Gabor Bernat
committed
for http client we should parse response when the server returns 500
1 parent 3c66bc1 commit 1156872

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ JSON-RPC for the java programming language. jsonrpc4j uses the
66
objects to and from json objects (and other things related to
77
JSON-RPC).
88

9-
[![Javadoc](https://img.shields.io/badge/javadoc-OK-blue.svg)](http://briandilley.github.io/jsonrpc4j/javadoc/1.3.2/)
9+
[![Javadoc](https://img.shields.io/badge/javadoc-OK-blue.svg)](http://briandilley.github.io/jsonrpc4j/javadoc/1.3.3/)
1010
[ ![Download](https://api.bintray.com/packages/gaborbernat/maven/com.github.briandilley.jsonrpc4j%3Ajsonrpc4j/images/download.svg) ](https://bintray.com/gaborbernat/maven/com.github.briandilley.jsonrpc4j%3Ajsonrpc4j/_latestVersion)
1111
[![Travis CI](https://travis-ci.org/gaborbernat/jsonrpc4j.svg?branch=master)](https://travis-ci.org/gaborbernat/jsonrpc4j)
12-
[![GitHub commits](https://img.shields.io/github/commits-since/briandilley/jsonrpc4j/1.3.2.svg)](https://github.com/briandilley/jsonrpc4j/compare/1.3.2...master)
12+
[![GitHub commits](https://img.shields.io/github/commits-since/briandilley/jsonrpc4j/1.3.3.svg)](https://github.com/briandilley/jsonrpc4j/compare/1.3.3...master)
1313
[![Maintenance](https://img.shields.io/maintenance/yes/2016.svg)](https://github.com/briandilley/jsonrpc4j/commits/master)
1414

1515
## Features Include:
@@ -43,7 +43,7 @@ In `<dependencies>`:
4343
<dependency>
4444
<groupId>com.github.briandilley.jsonrpc4j</groupId>
4545
<artifactId>jsonrpc4j</artifactId>
46-
<version>1.3.2</version>
46+
<version>1.3.3</version>
4747
</dependency>
4848

4949
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ plugins {
1616
}
1717

1818
description = 'This project aims to provide the facility to easily implement JSON-RPC for the java programming language.'
19-
version = '1.3.3-RC0'
19+
version = '1.3.3'
2020
group = 'com.github.briandilley.jsonrpc4j'
2121

2222
sourceCompatibility = 1.7

src/main/java/com/googlecode/jsonrpc4j/JsonRpcClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ private void notifyAnswerListener(ObjectNode jsonObject) {
239239
}
240240
}
241241

242-
private void handleErrorResponse(ObjectNode jsonObject) throws Throwable {
242+
protected void handleErrorResponse(ObjectNode jsonObject) throws Throwable {
243243
if (hasError(jsonObject)) {
244244
// resolve and throw the exception
245245
if (exceptionResolver == null) {
@@ -297,7 +297,7 @@ private boolean isIdValueNotCorrect(String id, ObjectNode jsonObject) {
297297
return !jsonObject.has(ID) || jsonObject.get(ID) == null || !jsonObject.get(ID).asText().equals(id);
298298
}
299299

300-
private boolean hasError(ObjectNode jsonObject) {
300+
protected boolean hasError(ObjectNode jsonObject) {
301301
return jsonObject.has(ERROR) && jsonObject.get(ERROR) != null && !jsonObject.get(ERROR).isNull();
302302
}
303303

src/main/java/com/googlecode/jsonrpc4j/JsonRpcHttpClient.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,18 @@ public Object invoke(String methodName, Object argument, Type returnType, Map<St
103103
try (OutputStream send = connection.getOutputStream()) {
104104
super.invoke(methodName, argument, send);
105105
}
106+
final boolean useGzip = useGzip(connection);
106107
// read and return value
107108
try {
108-
try (InputStream answer = getAnswerStream(connection, useGzip(connection))) {
109+
try (InputStream answer = getStream(connection.getInputStream(), useGzip)) {
109110
return super.readResponse(returnType, answer);
110111
}
111112
} catch (IOException e) {
112-
// in case of error try to read response body and return it in exception
113-
throw new HttpException(readErrorString(connection), e);
113+
try (InputStream answer = getStream(connection.getErrorStream(), useGzip)) {
114+
return super.readResponse(returnType, answer);
115+
} catch (IOException ef) {
116+
throw new HttpException(readErrorString(connection), ef);
117+
}
114118
}
115119
} finally {
116120
connection.disconnect();
@@ -162,10 +166,8 @@ private HttpURLConnection prepareConnection(Map<String, String> extraHeaders) th
162166
return connection;
163167
}
164168

165-
private InputStream getAnswerStream(final HttpURLConnection connection, final boolean useGzip) throws IOException {
166-
InputStream inputStream = connection.getInputStream();
167-
if (useGzip) return new GZIPInputStream(inputStream);
168-
return inputStream;
169+
private InputStream getStream(final InputStream inputStream, final boolean useGzip) throws IOException {
170+
return useGzip ? new GZIPInputStream(inputStream) : inputStream;
169171
}
170172

171173
private boolean useGzip(final HttpURLConnection connection) {

0 commit comments

Comments
 (0)