19
19
20
20
import java .io .BufferedInputStream ;
21
21
import java .io .IOException ;
22
+ import java .io .InputStream ;
22
23
import java .io .OutputStream ;
23
24
import java .net .HttpURLConnection ;
24
25
import java .net .URL ;
@@ -67,7 +68,7 @@ String postJson(String endpoint, String rawJson) throws SimpleRestException {
67
68
int status = con .getResponseCode ();
68
69
69
70
if (status != 200 ) {
70
- throw new SimpleRestResponseException (status , con . getResponseMessage ( ), "Unexpected response status code." );
71
+ throw new SimpleRestResponseException (status , readError ( con ), "Unexpected response status code at " + endpoint );
71
72
}
72
73
73
74
String response = readResponse (con );
@@ -77,8 +78,16 @@ String postJson(String endpoint, String rawJson) throws SimpleRestException {
77
78
}
78
79
}
79
80
81
+ private String readError (HttpURLConnection con ) throws IOException {
82
+ return readInputStream (con .getErrorStream ());
83
+ }
84
+
80
85
private String readResponse (HttpURLConnection con ) throws IOException {
81
- try (BufferedInputStream input = new BufferedInputStream (con .getInputStream ())) {
86
+ return readInputStream (con .getInputStream ());
87
+ }
88
+
89
+ private String readInputStream (InputStream inputStream ) throws IOException {
90
+ try (BufferedInputStream input = new BufferedInputStream (inputStream )) {
82
91
return NpmResourceHelper .readUtf8StringFromInputStream (input );
83
92
}
84
93
}
@@ -121,7 +130,7 @@ public String getExceptionMessage() {
121
130
122
131
@ Override
123
132
public String getMessage () {
124
- return String .format ("%s: %s (%s)" , getStatusCode (), getResponseMessage (), getExceptionMessage ());
133
+ return String .format ("%s [HTTP %s] -- (%s)" , getExceptionMessage (), getStatusCode (), getResponseMessage ());
125
134
}
126
135
}
127
136
0 commit comments