Skip to content

Commit

Permalink
Merge pull request #286 from madgik/bug/293_exareme_returns_details_o…
Browse files Browse the repository at this point in the history
…f_an_unexpected_error

Bug/293 exareme returns details of an unexpected error
  • Loading branch information
ThanKarab authored Oct 13, 2020
2 parents e47ce16 + a6918cd commit c4d713a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.*;

/**
Expand All @@ -24,6 +25,8 @@ public class ConsulNodesPathologiesAndDatasetsInfo {
private final HashMap<String, String> nodeIPsToNames;
private String masterNodeIP;

private static final CloseableHttpClient httpClient = HttpClients.createDefault();

/**
* Fetches the node information from CONSUL.
*
Expand Down Expand Up @@ -162,7 +165,10 @@ public static class NodeData {
pathologyDatasets.put(pathology, new ArrayList<>(Arrays.asList(nodePathologyDatasets)));
}
} catch (JsonSyntaxException e) {
throw new ConsulException("There was a problem contacting consul: " + e.getMessage());
throw new ConsulException("There was a problem parsing the response from consul: " + e.getMessage());
} catch (ConsulException e) {
// The node is up but the data are not added yet.
// continue;
}
}
}
Expand All @@ -181,29 +187,33 @@ private static String searchConsul(String query) throws ConsulException {
log.debug("Consul Query: " + query);

String consulURL = getConsulUrl();
if (!consulURL.startsWith("http://")) {
consulURL = "http://" + consulURL;
}

HttpGet request = new HttpGet(consulURL + "/v1/kv/" + query);
try {
CloseableHttpClient httpclient = HttpClients.createDefault();

HttpGet httpGet = new HttpGet(consulURL + "/v1/kv/" + query);
CloseableHttpResponse response = httpclient.execute(httpGet);
CloseableHttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() != 200) {
return null;
log.error("Failed consul query: " + consulURL + "/v1/kv/" + query);
throw new ConsulException(
"There was an error contacting consul. StatusCode: " + response.getStatusLine().getStatusCode());
}

return EntityUtils.toString(response.getEntity());

} catch (Exception e) {
return null;
} catch (IOException e) {
log.error("Failed consul query: " + consulURL + "/v1/kv/" + query);
throw new ConsulException(
"An exception occurred while contacting Consul. Exception: " + e.getMessage());
} finally {
request.releaseConnection();
}
}

private static String getConsulUrl() throws ConsulException {
String consulURL = System.getenv("CONSULURL");
if (consulURL == null) throw new ConsulException("CONSULURL environment variable is not set.");

if (!consulURL.startsWith("http://")) {
consulURL = "http://" + consulURL;
}

return consulURL;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ public class HBPQueryConstants {
public static String pathologyNotProvided = "Please provide a pathology.";
public static String datasetXDoesNotExistInPathologyY = "Dataset(s) %s does not exist in pathology %s.";

public static String datasetsXYZAreInactive = "The following datasets %s are currently unavailable. Please try again later.";
public static String datasetsXYZAreInactive =
"The following datasets %s are currently unavailable. Please try again later.";

public static String nodesUnavailable = "Some nodes are unavailable. Please try again later.";

public static String serverErrorOccurred =
"Something went wrong. Please consult the system administrator or try again later.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Map;

import static madgik.exareme.master.gateway.GatewayConstants.COOKIE_ALGORITHM_EXECUTION_ID;
import static madgik.exareme.master.gateway.async.handler.HBP.HBPQueryConstants.serverErrorOccurred;

public class HBPQueryHandler implements HttpAsyncRequestHandler<HttpRequest> {

Expand Down Expand Up @@ -176,18 +177,18 @@ private void handleHBPAlgorithmExecution(HttpRequest request, HttpResponse respo
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
response.setEntity(createErrorResponseEntity(e.getMessage(), errorType));

} catch (JsonSyntaxException e) {
log.error("Could not parse the algorithms properly.");
String errorType = HBPQueryHelper.ErrorResponse.ErrorResponseTypes.error;
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
response.setEntity(createErrorResponseEntity("Could not parse the algorithms properly.", errorType));

} catch (UserException e) {
log.error(e.getMessage());
String errorType = HBPQueryHelper.ErrorResponse.ErrorResponseTypes.user_error;
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
response.setEntity(createErrorResponseEntity(e.getMessage(), errorType));

} catch (JsonSyntaxException e) {
log.error("Could not parse the algorithms properly.");
String errorType = HBPQueryHelper.ErrorResponse.ErrorResponseTypes.error;
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
response.setEntity(createErrorResponseEntity(serverErrorOccurred, errorType));

} catch (Exception e) {
log.error(e.getMessage());
for (StackTraceElement stack : e.getStackTrace()) {
Expand All @@ -197,7 +198,7 @@ private void handleHBPAlgorithmExecution(HttpRequest request, HttpResponse respo
log.error(e.getStackTrace());
String errorType = HBPQueryHelper.ErrorResponse.ErrorResponseTypes.error;
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
response.setEntity(createErrorResponseEntity(e.getMessage(), errorType));
response.setEntity(createErrorResponseEntity(serverErrorOccurred, errorType));
}
}

Expand Down

0 comments on commit c4d713a

Please sign in to comment.