Skip to content

Commit

Permalink
remove customized enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhun Xu committed Oct 29, 2019
1 parent 9dfa6ea commit e770626
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public ResponseEntity<String> handleUnauthorizedException(APIException exception
throws InvalidProtocolBufferException {

Status.Builder statusBuilder = Status.newBuilder();
statusBuilder.setCode(exception.getApiExceptionType().getId());
statusBuilder.setReason(exception.getApiExceptionType().getMessage());
statusBuilder.setCode(exception.getId());
statusBuilder.setReason(exception.getMessage());
statusBuilder.setInfo(exception.getInfo());
statusBuilder.setStatus(Status.StatusFlag.FAILURE);

SeldonMessage message = SeldonMessage.newBuilder().setStatus(statusBuilder.build()).build();
String json;
json = ProtoBufUtils.toJson(message);
return new ResponseEntity<String>(
json, HttpStatus.valueOf(exception.getApiExceptionType().getHttpCode()));
json, HttpStatus.valueOf(exception.getHttpCode()));
}
}
35 changes: 28 additions & 7 deletions engine/src/main/java/io/seldon/engine/exception/APIException.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public enum ApiExceptionType {
ENGINE_INTERRUPTED(205, "API call interrupted", 500),
ENGINE_EXECUTION_FAILURE(206, "Execution failure", 500),
ENGINE_INVALID_ROUTING(207, "Invalid Routing", 500),
REQUEST_IO_EXCEPTION(208, "IO Exception", 500),
CUSTOMIZED_EXCEPTION(209, "Customized Exception", 400);
REQUEST_IO_EXCEPTION(208, "IO Exception", 500);

int id;
String message;
Expand All @@ -56,28 +55,50 @@ public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public int getHttpCode() {
return httpCode;
}
};

ApiExceptionType apiExceptionType;
private ApiExceptionType apiExceptionType;
int id;
String message;
private int httpCode;
String info;
private Boolean usingCustomizedException;

public APIException(ApiExceptionType apiExceptionType, String info) {
super();
this.apiExceptionType = apiExceptionType;
this.info = info;
this.usingCustomizedException = false;
}

public APIException(int id, String message, int httpCode, String info) {
super();
this.id = id;
this.message = message;
this.httpCode = httpCode;
this.info = info;
this.usingCustomizedException = true;
}

public ApiExceptionType getApiExceptionType() {
return apiExceptionType;
}

public int getId() {
return usingCustomizedException ? id : getApiExceptionType().getId();
}

public String getMessage() {
return usingCustomizedException ? message : getApiExceptionType().getMessage();
}

public int getHttpCode() {
return usingCustomizedException ? httpCode : getApiExceptionType().getHttpCode();
}

public void setApiExceptionType(ApiExceptionType apiExceptionType) {
this.apiExceptionType = apiExceptionType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,10 @@ private SeldonMessage queryREST(
}
else
{
APIException.ApiExceptionType exceptionType =
APIException.ApiExceptionType.CUSTOMIZED_EXCEPTION;
exceptionType.setMessage(seldonMessageStatus.getReason());
throw new APIException(exceptionType, seldonMessageStatus.getInfo());
throw new APIException(seldonMessageStatus.getCode(),
seldonMessageStatus.getReason(),
200,
seldonMessageStatus.getInfo());
}
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,34 @@
public class ExceptionControllerAdviceTest {
@Test
public void testApiExceptionType() throws Exception {
APIException exception = new APIException(
ApiExceptionType.ENGINE_MICROSERVICE_ERROR, "info");
ResponseEntity<String> responseEntity = new io.seldon.engine.ExceptionControllerAdvice()
.handleUnauthorizedException(new APIException(
ApiExceptionType.ENGINE_MICROSERVICE_ERROR, "info"));
validateSeldonMessage(responseEntity, ApiExceptionType.ENGINE_MICROSERVICE_ERROR);
.handleUnauthorizedException(exception);
validateSeldonMessage(responseEntity, exception);
}

@Test
public void testCustomizedExceptionType() throws Exception {
ApiExceptionType exceptionType =
ApiExceptionType.CUSTOMIZED_EXCEPTION;
exceptionType.setMessage("exception msg in test");
APIException exception = new APIException(400,
"test in message",
200,
"info");
ResponseEntity<String> responseEntity = new io.seldon.engine.ExceptionControllerAdvice()
.handleUnauthorizedException(new APIException(exceptionType, "info"));
validateSeldonMessage(responseEntity, exceptionType);
.handleUnauthorizedException(exception);
validateSeldonMessage(responseEntity, exception);
}

private void validateSeldonMessage(
ResponseEntity<String> httpResponse, ApiExceptionType exceptionType) throws Exception {
ResponseEntity<String> httpResponse, APIException exception) throws Exception {
String response = httpResponse.getBody();
SeldonMessage.Builder builder = SeldonMessage.newBuilder();
JsonFormat.parser().ignoringUnknownFields().merge(response, builder);
SeldonMessage seldonMessage = builder.build();

Assert.assertEquals(exceptionType.getHttpCode(), httpResponse.getStatusCodeValue());
Assert.assertEquals(exceptionType.getId(), seldonMessage.getStatus().getCode());
Assert.assertEquals(exceptionType.getMessage(), seldonMessage.getStatus().getReason());
Assert.assertEquals(exception.getHttpCode(), httpResponse.getStatusCodeValue());
Assert.assertEquals(exception.getId(), seldonMessage.getStatus().getCode());
Assert.assertEquals(exception.getMessage(), seldonMessage.getStatus().getReason());
Assert.assertEquals("info", seldonMessage.getStatus().getInfo());
Assert.assertEquals(Status.StatusFlag.FAILURE, seldonMessage.getStatus().getStatus());
}
Expand Down

0 comments on commit e770626

Please sign in to comment.