Skip to content

Commit b27ea19

Browse files
committed
Update error message handling.
1 parent af33f6d commit b27ea19

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ public interface ResponseHandler<T> {
360360
}
361361
```
362362

363-
If a service returns an error response, a `WebServiceException` will be thrown. If the content type of the response is "text/plain", the body of the response will be provided in the exception message.
363+
If a service returns an error response, a `WebServiceException` will be thrown. If the content type of the error response is "text/*", the deserialized response body will be provided in the exception message.
364364

365365
The following code snippet demonstrates how `WebServiceProxy` might be used to access the operations of the simple math service discussed earlier. `listOf()`, `mapOf()`, and `entry()` are static utility methods provided by the `org.httprpc.util.Collections` class that can be used to declaratively create immutable collection instances:
366366

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
subprojects {
1616
group = 'org.httprpc'
17-
version = '8.1'
17+
version = '8.1.1'
1818

1919
apply plugin: 'java-library'
2020
apply plugin: 'maven-publish'

httprpc-client/src/main/java/org/httprpc/WebServiceProxy.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ public <T> T invoke() throws IOException {
458458
* The result type.
459459
*
460460
* @param responseHandler
461-
* The response handler, or <code>null</code> for no response handler.
461+
* The response handler.
462462
*
463463
* @return
464464
* The result of the operation.
@@ -467,6 +467,10 @@ public <T> T invoke() throws IOException {
467467
* If an exception occurs while executing the operation.
468468
*/
469469
public <T> T invoke(ResponseHandler<T> responseHandler) throws IOException {
470+
if (responseHandler == null) {
471+
throw new IllegalArgumentException();
472+
}
473+
470474
URL url;
471475
RequestHandler requestHandler;
472476
if (method.equalsIgnoreCase("POST") && body == null && this.requestHandler == null) {
@@ -588,7 +592,7 @@ public void encodeRequest(OutputStream outputStream) throws IOException {
588592

589593
T result;
590594
if (responseCode / 100 == 2) {
591-
if (responseCode % 100 < 4 && responseHandler != null) {
595+
if (responseCode % 100 < 4) {
592596
Map<String, String> headers = new HashMap<>();
593597

594598
for (Map.Entry<String, List<String>> entry : connection.getHeaderFields().entrySet()) {
@@ -611,16 +615,14 @@ public void encodeRequest(OutputStream outputStream) throws IOException {
611615
}
612616
} else {
613617
String message;
614-
if (contentType != null && contentType.startsWith("text/plain")) {
618+
if (contentType != null && contentType.startsWith("text/")) {
615619
TextDecoder textDecoder = new TextDecoder();
616620

617621
try (InputStream inputStream = connection.getErrorStream()) {
618622
message = textDecoder.read(inputStream);
619623
}
620624
} else {
621-
String responseMessage = connection.getResponseMessage();
622-
623-
message = (responseMessage == null || responseMessage.isEmpty()) ? String.valueOf(responseCode) : responseMessage;
625+
message = null;
624626
}
625627

626628
throw new WebServiceException(message, responseCode);

0 commit comments

Comments
 (0)