Skip to content

Commit

Permalink
[MODFISTO-513] - Use correct error code for conflict exception (#442)
Browse files Browse the repository at this point in the history
* [MODFISTO-513] - Use correct error code for conflict exception

* [MODFISTO-513] - Used similar logic in other methods
  • Loading branch information
azizbekxm authored Dec 24, 2024
1 parent 8086d14 commit 1a4ec01
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/main/java/org/folio/rest/exception/HttpException.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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()
Expand All @@ -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) {
Expand Down

0 comments on commit 1a4ec01

Please sign in to comment.