Skip to content

Commit

Permalink
Merge pull request #307 from madgik/fix/bad_user_input_status_code
Browse files Browse the repository at this point in the history
BadUserInput results to 400 response code.
  • Loading branch information
ThanKarab authored Aug 9, 2021
2 parents 858dacc + b5be38e commit 965c29a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package madgik.exareme.master.gateway.async.handler.HBP.Exceptions;

public class RequestException extends Exception {
public RequestException(String algorithmName, String message) {
public class BadRequestException extends Exception {
public BadRequestException(String algorithmName, String message) {
super(message + " Algorithm: " + algorithmName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import madgik.exareme.master.engine.iterations.handler.NIterativeAlgorithmResultEntity;
import madgik.exareme.master.engine.iterations.state.IterativeAlgorithmState;
import madgik.exareme.master.gateway.ExaremeGatewayUtils;
import madgik.exareme.master.gateway.async.handler.HBP.Exceptions.BadRequestException;
import madgik.exareme.master.gateway.async.handler.HBP.Exceptions.BadUserInputException;
import madgik.exareme.master.gateway.async.handler.HBP.Exceptions.RequestException;
import madgik.exareme.master.gateway.async.handler.entity.NQueryResultEntity;
import madgik.exareme.master.queryProcessor.HBP.AlgorithmProperties;
import madgik.exareme.master.queryProcessor.HBP.Algorithms;
Expand Down Expand Up @@ -101,7 +101,7 @@ private void handleHBPAlgorithmExecution(HttpRequest request, HttpResponse respo
String algorithmName = getAlgorithmName(request);
AlgorithmProperties algorithmProperties = Algorithms.getInstance().getAlgorithmProperties(algorithmName);
if (algorithmProperties == null)
throw new RequestException(algorithmName, "The algorithm '" + algorithmName + "' does not exist.");
throw new BadRequestException(algorithmName, "The algorithm '" + algorithmName + "' does not exist.");

String algorithmKey = algorithmName + "_" + System.currentTimeMillis();

Expand Down Expand Up @@ -168,13 +168,7 @@ private void handleHBPAlgorithmExecution(HttpRequest request, HttpResponse respo
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(entity);
}
} catch (BadUserInputException e) {
log.error(e.getMessage());
String errorType = HBPQueryHelper.ErrorResponse.ErrorResponseTypes.user_error;
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(createErrorResponseEntity(e.getMessage(), errorType));

} catch (RequestException e) {
} catch (BadUserInputException | BadRequestException e) {
log.error(e.getMessage());
String errorType = HBPQueryHelper.ErrorResponse.ErrorResponseTypes.user_error;
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import madgik.exareme.master.engine.AdpDBManager;
import madgik.exareme.master.engine.AdpDBManagerLocator;
import madgik.exareme.master.gateway.ExaremeGatewayUtils;
import madgik.exareme.master.gateway.async.handler.HBP.Exceptions.RequestException;
import madgik.exareme.master.gateway.async.handler.HBP.Exceptions.BadRequestException;
import madgik.exareme.master.gateway.async.handler.HBP.Exceptions.BadUserInputException;
import madgik.exareme.master.gateway.async.handler.entity.NQueryResultEntity;
import madgik.exareme.master.queryProcessor.HBP.AlgorithmProperties;
Expand Down Expand Up @@ -55,7 +55,7 @@ public void handle(
HttpResponse response = httpexchange.getResponse();
try {
handleInternal(request, response, context);
} catch (AlgorithmException | CDEsMetadataException | ComposerException | BadUserInputException | RequestException e) {
} catch (AlgorithmException | CDEsMetadataException | ComposerException | BadUserInputException | BadRequestException e) {
e.printStackTrace();
}
httpexchange.submitResponse(new BasicAsyncResponseProducer(response));
Expand All @@ -65,7 +65,7 @@ private void handleInternal(
final HttpRequest request,
final HttpResponse response,
final HttpContext context
) throws HttpException, IOException, AlgorithmException, CDEsMetadataException, ComposerException, BadUserInputException, RequestException {
) throws HttpException, IOException, AlgorithmException, CDEsMetadataException, ComposerException, BadUserInputException, BadRequestException {

String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
Expand Down Expand Up @@ -97,7 +97,7 @@ private void handleInternal(
AlgorithmProperties algorithmProperties = Algorithms.getInstance().getAlgorithmProperties(algorithmName);

if (algorithmProperties == null)
throw new RequestException(algorithmName, "The algorithm does not exist.");
throw new BadRequestException(algorithmName, "The algorithm does not exist.");

algorithmProperties.mergeWithAlgorithmParameters(inputContent);

Expand Down

0 comments on commit 965c29a

Please sign in to comment.