Skip to content

Commit 6bf2683

Browse files
authored
Merge pull request #1544 from marklogic/feature/185-connection-drop
DEVEXP-185 Better error messages for IO errors
2 parents 5583ed9 + 6f52afc commit 6bf2683

File tree

3 files changed

+23
-46
lines changed

3 files changed

+23
-46
lines changed

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/fastfunctest/TestDatabaseClientConnection.java

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.marklogic.client.DatabaseClientFactory;
2121
import com.marklogic.client.DatabaseClientFactory.SSLHostnameVerifier;
2222
import com.marklogic.client.DatabaseClientFactory.SecurityContext;
23+
import com.marklogic.client.MarkLogicIOException;
2324
import com.marklogic.client.ResourceNotFoundException;
2425
import com.marklogic.client.Transaction;
2526
import com.marklogic.client.alerting.RuleDefinition;
@@ -46,6 +47,7 @@
4647
import com.marklogic.client.query.ValuesDefinition;
4748
import com.marklogic.client.query.ValuesListDefinition;
4849
import org.custommonkey.xmlunit.exceptions.XpathException;
50+
import org.junit.jupiter.api.Assertions;
4951
import org.junit.jupiter.api.Test;
5052
import org.w3c.dom.Document;
5153
import org.xml.sax.SAXException;
@@ -167,35 +169,18 @@ public X509Certificate[] getAcceptedIssuers() {
167169
}
168170

169171

170-
@Test
171-
public void testDatabaseClientConnectionInvalidPort() throws IOException
172-
{
173-
System.out.println("Running testDatabaseClientConnectionInvalidPort");
174-
175-
String filename = "facebook-10443244874876159931";
176-
177-
DatabaseClient client = newDatabaseClientBuilder().withPort(8033).build();
178-
179-
String expectedException = null;
180-
String exception = "";
181-
if (IsSecurityEnabled())
182-
expectedException = "Failed to connect";
183-
else
184-
expectedException = "com.marklogic.client.MarkLogicIOException";
185-
186-
// write doc
187-
try {
188-
writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text");
189-
} catch (Exception e) {
190-
exception = e.toString();
191-
System.out.println("Exception is " + exception);
192-
}
193-
194-
assertTrue(exception.contains(expectedException));
172+
@Test
173+
void invalidPort() {
174+
int assumedInvalidPort = 60123;
175+
DatabaseClient client = newDatabaseClientBuilder().withPort(assumedInvalidPort).build();
195176

196-
// release client
197-
client.release();
198-
}
177+
MarkLogicIOException ex = Assertions.assertThrows(MarkLogicIOException.class, () -> client.checkConnection());
178+
String expected = "Error occurred while calling http://localhost:60123/v1/ping; java.net.ConnectException: " +
179+
"Failed to connect to localhost/127.0.0.1:60123 ; possible reasons for the error include " +
180+
"that a MarkLogic app server may not be listening on the port, or MarkLogic was stopped " +
181+
"or restarted during the request; check the MarkLogic server logs for more information.";
182+
assertEquals(expected, ex.getMessage());
183+
}
199184

200185
@Test
201186
public void testDatabaseClientConnectionInvalidUser() throws IOException, KeyManagementException, NoSuchAlgorithmException

marklogic-client-api/src/main/java/com/marklogic/client/impl/OkHttpServices.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,13 @@ private Response sendRequestOnce(Request request) {
503503
try {
504504
return getConnection().newCall(request).execute();
505505
} catch (IOException e) {
506-
throw new MarkLogicIOException(e);
506+
String message = String.format(
507+
"Error occurred while calling %s; %s: %s " +
508+
"; possible reasons for the error include that a MarkLogic app server may not be listening on the port, " +
509+
"or MarkLogic was stopped or restarted during the request; check the MarkLogic server logs for more information.",
510+
request.url(), e.getClass().getName(), e.getMessage()
511+
);
512+
throw new MarkLogicIOException(message, e);
507513
}
508514
}
509515

marklogic-client-api/src/test/java/com/marklogic/client/test/SSLTest.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,9 @@ public void testSSLAuth() throws NoSuchAlgorithmException, KeyManagementExceptio
8282
assertEquals(handle.get(), "A simple text document by SSL connection");
8383
docMgr.delete(docId);
8484
} catch (MarkLogicIOException e) {
85-
String exception = e.toString();
86-
String message = exception.toLowerCase();
87-
88-
boolean foundExpected = false;
89-
90-
String[] expectedClasses = {"javax.net.ssl.SSLException", "java.net.UnknownServiceException"};
91-
String[] expectedMessages = {"unrecognized ssl message", "unable to find acceptable protocols"};
92-
for (int i=0; i < expectedClasses.length; i++) {
93-
String expectedException = "com.marklogic.client.MarkLogicIOException: " + expectedClasses[i] +": ";
94-
if (exception.startsWith(expectedException) && message.contains(expectedMessages[i])) {
95-
foundExpected = true;
96-
break;
97-
}
98-
}
99-
if (!foundExpected) {
100-
fail("unexpected exception for SSL over HTTPS or HTTP connection:\n"+exception);
101-
}
85+
String message = e.getMessage();
86+
assertTrue(message.contains("java.net.UnknownServiceException: Unable to find acceptable protocols"),
87+
"Unexpected message: " + message);
10288
}
10389
}
10490

0 commit comments

Comments
 (0)