Skip to content

Commit

Permalink
better error message for responses
Browse files Browse the repository at this point in the history
  • Loading branch information
nkpng2k committed Jan 7, 2022
1 parent b7610f1 commit e4cd94d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
8 changes: 8 additions & 0 deletions common/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ definitions:
type: array
items:
$ref: '#/definitions/ContributionGroup'
message:
description: >
Any message related to the scoring result. Typically used for propagating error messages.
type: string
ContributionGroup:
type: object
properties:
Expand Down Expand Up @@ -309,6 +313,10 @@ definitions:
An object with features and shapley values that was requested by the client.
This field will not be populated if the Shapley values are not available for a model.
$ref: '#/definitions/ContributionResponse'
message:
description: >
Any message related to the scoring result. Typically used for propagating error messages.
type: string
DataField:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public MojoScorer(
* @param request {@link ScoreRequest}
* @return response {@link ScoreResponse}
*/
public ScoreResponse score(ScoreRequest request) {
public ScoreResponse score(ScoreRequest request) throws ShapleyScoreException {
MojoFrame requestFrame = scoreRequestConverter
.apply(request, pipeline.getInputFrameBuilder());
MojoFrame responseFrame = doScore(requestFrame);
Expand Down Expand Up @@ -133,6 +133,7 @@ public ScoreResponse score(ScoreRequest request) {
} catch (Exception e) {
log.info("Failed shapley contribution due to: {}", e.getMessage());
log.debug(" - failure cause: ", e);
throw new ShapleyScoreException(e.getMessage());
}
return response;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ai.h2o.mojos.deploy.common.transform;

public class ShapleyScoreException extends Exception {
public ShapleyScoreException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ai.h2o.mojos.deploy.common.rest.model.ScoreResponse;
import ai.h2o.mojos.deploy.common.transform.MojoScorer;
import ai.h2o.mojos.deploy.common.transform.SampleRequestBuilder;
import ai.h2o.mojos.deploy.common.transform.ShapleyScoreException;
import com.google.common.base.Strings;
import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -67,10 +68,20 @@ public ResponseEntity<ScoreResponse> getScore(ScoreRequest request) {
log.info("Got scoring request");
ScoreResponse scoreResponse = scorer.score(request);
return ResponseEntity.ok(scoreResponse);
} catch (ShapleyScoreException se) {
String message = String.format(
"Failed scoring Shapley values in scoring request: %s, due to: %s",
request,
se.getMessage());
log.info(message);
log.debug(" - failure cause: ", se);
return scoreResponseFailure(HttpStatus.BAD_REQUEST, message);
} catch (Exception e) {
log.info("Failed scoring request: {}, due to: {}", request, e.getMessage());
String message = String.format(
"Failed scoring request: %s, due to: %s", request, e.getMessage());
log.info(message);
log.debug(" - failure cause: ", e);
return ResponseEntity.badRequest().build();
return scoreResponseFailure(HttpStatus.BAD_REQUEST, message);
}
}

Expand All @@ -84,13 +95,17 @@ public ResponseEntity<ScoreResponse> getScoreByFile(String file) {
log.info("Got scoring request for CSV");
return ResponseEntity.ok(scorer.scoreCsv(file));
} catch (IOException e) {
log.info("Failed loading CSV file: {}, due to: {}", file, e.getMessage());
String message = String.format(
"Failed loading CSV file: %s, due to: %s", file, e.getMessage());
log.info(message);
log.debug(" - failure cause: ", e);
return ResponseEntity.badRequest().build();
return scoreResponseFailure(HttpStatus.BAD_REQUEST, message);
} catch (Exception e) {
log.info("Failed scoring CSV file: {}, due to: {}", file, e.getMessage());
String message = String.format(
"Failed scoring CSV file: %s, due to: %s", file, e.getMessage());
log.info(message);
log.debug(" - failure cause: ", e);
return ResponseEntity.badRequest().build();
return scoreResponseFailure(HttpStatus.BAD_REQUEST, message);
}
}

Expand All @@ -103,17 +118,36 @@ public ResponseEntity<ContributionResponse> getContribution(
= scorer.computeContribution(request);
return ResponseEntity.ok(contributionResponse);
} catch (UnsupportedOperationException e) {
log.info("Unsupported operation due to: {}", e.getMessage());
return ResponseEntity.status(HttpStatus.NOT_IMPLEMENTED).build();
String message = String.format("Unsupported operation due to: %s", e.getMessage());
log.info(message);
return scoreContributionResponseFailure(HttpStatus.NOT_IMPLEMENTED, message);
} catch (Exception e) {
log.info("Failed shapley contribution request due to: {}", e.getMessage());
String message = String.format(
"Failed shapley contribution request due to: %s", e.getMessage());
log.info(message);
log.debug(" - failure cause: ", e);
return ResponseEntity.badRequest().build();
return scoreContributionResponseFailure(HttpStatus.BAD_REQUEST, message);
}
}

@Override
public ResponseEntity<ScoreRequest> getSampleRequest() {
return ResponseEntity.ok(sampleRequestBuilder.build(scorer.getPipeline().getInputMeta()));
}

private ResponseEntity<ScoreResponse> scoreResponseFailure(
HttpStatus status,
String message) {
ScoreResponse response = new ScoreResponse();
response.setMessage(message);
return ResponseEntity.status(status).body(response);
}

private ResponseEntity<ContributionResponse> scoreContributionResponseFailure(
HttpStatus status,
String message) {
ContributionResponse response = new ContributionResponse();
response.setMessage(message);
return ResponseEntity.status(status).body(response);
}
}

0 comments on commit e4cd94d

Please sign in to comment.