Skip to content

Commit 8617c00

Browse files
committed
fix #434 - pass thru full response body when an error message doesn't conform to expected formats
(cherry picked from commit e6daf26)
1 parent a464200 commit 8617c00

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/main/java/com/marklogic/client/impl/JerseyServices.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
import java.io.Closeable;
1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.io.ByteArrayInputStream;
2122
import java.io.InputStream;
2223
import java.io.PrintStream;
2324
import java.io.Reader;
2425
import java.io.UnsupportedEncodingException;
26+
import java.nio.charset.StandardCharsets;
2527
import java.math.BigDecimal;
2628
import java.net.URLEncoder;
2729
import java.net.URI;
@@ -221,13 +223,23 @@ public JerseyServices() {
221223

222224
private FailedRequest extractErrorFields(ClientResponse response) {
223225
if ( response == null ) return null;
224-
InputStream is = response.getEntityInputStream();
226+
if ( response.getStatus() == 401 ) {
227+
FailedRequest failure = new FailedRequest();
228+
failure.setMessageString("Unauthorized");
229+
failure.setStatusString("Failed Auth");
230+
return failure;
231+
}
232+
String responseBody = response.getEntity(String.class);
225233
try {
234+
InputStream is = new ByteArrayInputStream(responseBody.getBytes("UTF-8"));
226235
FailedRequest handler = FailedRequest.getFailedRequest(
227236
response.getStatus(), response.getType(), is);
237+
if ( handler.getMessage() == null ) {
238+
handler.setMessageString(responseBody);
239+
}
228240
return handler;
229-
} catch (RuntimeException e) {
230-
throw (e);
241+
} catch (UnsupportedEncodingException e) {
242+
throw new IllegalStateException("UTF-8 is unsupported", e);
231243
} finally {
232244
response.close();
233245
}

src/test/java/com/marklogic/client/test/InvalidUserTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.marklogic.client.DatabaseClient;
2323
import com.marklogic.client.DatabaseClientFactory;
24+
import com.marklogic.client.FailedRequestException;
2425
import com.marklogic.client.DatabaseClientFactory.Authentication;
2526
import com.marklogic.client.document.TextDocumentManager;
2627
import com.marklogic.client.io.StringHandle;
@@ -29,6 +30,7 @@ public class InvalidUserTest {
2930
@Test
3031
public void testInvalidUserAuth() {
3132

33+
//System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
3234
// create the client
3335
DatabaseClient client = DatabaseClientFactory.newClient(
3436
"localhost", 8012, "MyFooUser", "x", Authentication.DIGEST);
@@ -48,7 +50,7 @@ public void testInvalidUserAuth() {
4850
// the next line will only run if write doesn't throw an exception
4951
docMgr.delete(docId);
5052
}
51-
catch (Exception e) {
53+
catch (FailedRequestException e) {
5254
exception = e.toString();
5355
} finally {
5456
client.release();

0 commit comments

Comments
 (0)