diff --git a/src/main/java/org/folio/rest/exception/HttpException.java b/src/main/java/org/folio/rest/exception/HttpException.java index 3d3ca912..bad85b45 100644 --- a/src/main/java/org/folio/rest/exception/HttpException.java +++ b/src/main/java/org/folio/rest/exception/HttpException.java @@ -1,11 +1,15 @@ package org.folio.rest.exception; +import static org.folio.rest.util.ErrorCodes.CONFLICT; +import static org.folio.rest.util.ErrorCodes.GENERIC_ERROR_CODE; + import org.apache.commons.lang3.StringUtils; import org.folio.rest.jaxrs.model.Error; import org.folio.rest.jaxrs.model.Errors; import org.folio.rest.jaxrs.model.Parameter; import org.folio.rest.util.ErrorCodes; +import java.util.Collections; import java.util.List; public class HttpException extends RuntimeException { @@ -17,17 +21,19 @@ public class HttpException extends RuntimeException { public HttpException(int code, String message) { super(StringUtils.isNotEmpty(message) ? message : ErrorCodes.GENERIC_ERROR_CODE.getDescription()); this.code = code; + var ec = code == 409 ? CONFLICT : GENERIC_ERROR_CODE; this.errors = new Errors() - .withErrors(List.of(new Error().withCode(ErrorCodes.GENERIC_ERROR_CODE.getCode()).withMessage(message))) + .withErrors(Collections.singletonList(new Error().withCode(ec.getCode()).withMessage(message))) .withTotalRecords(1); } public HttpException(int code, String message, Throwable cause) { super(message, cause); this.code = code; + var ec = code == 409 ? CONFLICT : GENERIC_ERROR_CODE; Parameter causeParam = new Parameter().withKey("cause").withValue(cause.getMessage()); Error error = new Error() - .withCode(ErrorCodes.GENERIC_ERROR_CODE.getCode()) + .withCode(ec.getCode()) .withMessage(message) .withParameters(List.of(causeParam)); this.errors = new Errors() @@ -38,17 +44,10 @@ public HttpException(int code, String message, Throwable cause) { public HttpException(int code, Throwable cause) { super(cause.getMessage(), cause); this.code = code; + var ec = code == 409 ? CONFLICT : GENERIC_ERROR_CODE; this.errors = new Errors() - .withErrors(List.of(new Error().withCode(ErrorCodes.GENERIC_ERROR_CODE.getCode()).withMessage(cause.getMessage()))) - .withTotalRecords(1); - } - - public HttpException(int code, ErrorCodes errCodes) { - super(errCodes.getDescription()); - this.errors = new Errors() - .withErrors(List.of(new Error().withCode(errCodes.getCode()).withMessage(errCodes.getDescription()))) + .withErrors(List.of(new Error().withCode(ec.getCode()).withMessage(cause.getMessage()))) .withTotalRecords(1); - this.code = code; } public HttpException(int code, Error error) {