diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/exception/RMException.java b/io/src/main/java/org/gcube/resourcemanagement/manager/exception/CodeException.java similarity index 76% rename from io/src/main/java/org/gcube/resourcemanagement/manager/exception/RMException.java rename to io/src/main/java/org/gcube/resourcemanagement/manager/exception/CodeException.java index 466f9ad..4adcca3 100644 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/exception/RMException.java +++ b/io/src/main/java/org/gcube/resourcemanagement/manager/exception/CodeException.java @@ -6,15 +6,16 @@ package org.gcube.resourcemanagement.manager.exception; * @author Manuele Simi (ISTI CNR) * */ -public class RMException extends Exception { +public class CodeException extends Exception { private static final long serialVersionUID = 333945715086602250L; private final int errorCode; private final String errorMsg; - public RMException(ExceptionCode code) { - this.errorMsg = code.getMsg(); + public CodeException(RMCode code) { + super(); + this.errorMsg = code.getMessage(); this.errorCode = code.getId(); } diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/exception/ErrorCode.java b/io/src/main/java/org/gcube/resourcemanagement/manager/exception/ErrorCode.java index 58398d5..88f64ca 100644 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/exception/ErrorCode.java +++ b/io/src/main/java/org/gcube/resourcemanagement/manager/exception/ErrorCode.java @@ -1,6 +1,3 @@ -/** - * - */ package org.gcube.resourcemanagement.manager.exception; /** @@ -10,7 +7,15 @@ package org.gcube.resourcemanagement.manager.exception; * */ public interface ErrorCode { + + /** + * Identifier of the code. + */ public int getId(); - public String getMsg(); + /** + * The message associated to the code + * @return the message + */ + public String getMessage(); } diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/exception/ExceptionCode.java b/io/src/main/java/org/gcube/resourcemanagement/manager/exception/ExceptionCode.java deleted file mode 100644 index ce28bd9..0000000 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/exception/ExceptionCode.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.gcube.resourcemanagement.manager.exception; - -/** - * Exception codes handled with {@link RMException}. - * - * @author Manuele Simi (ISTI CNR) - * - */ -public enum ExceptionCode implements ErrorCode { - - INVALID_REQUEST(0, "The request is invalid"), - MISSING_PARAMETER(1, "Required query parameter is missing"), - MISSING_HEADER(2, "Required header is missing"), - CONTEXT_ALREADY_EXIST(3, "Required header is missing"); - - private final int id; - private final String msg; - - ExceptionCode(int id, String msg) { - this.id = id; - this.msg = msg; - } - - public int getId() { - return this.id; - } - - public String getMsg() { - return this.msg; - } -} diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/exception/RMCode.java b/io/src/main/java/org/gcube/resourcemanagement/manager/exception/RMCode.java new file mode 100644 index 0000000..8026df6 --- /dev/null +++ b/io/src/main/java/org/gcube/resourcemanagement/manager/exception/RMCode.java @@ -0,0 +1,42 @@ +package org.gcube.resourcemanagement.manager.exception; + +import java.util.Arrays; + +/** + * Exception codes handled with {@link CodeException}. + * + * @author Manuele Simi (ISTI CNR) + * + */ +public enum RMCode implements ErrorCode { + + INVALID_REQUEST(0, "The request is invalid"), + MISSING_PARAMETER(1,"Required query parameter is missing"), + MISSING_HEADER(2, "Required header is missing"), + CONTEXT_ALREADY_EXIST(3, "Required header is missing"), + INVALID_REQUEST_FOR_RR(4, "Failed to validate the request. The request was not submitted to the Resource Registry."), + GENERIC_ERROR_FROM_RR(5, "The Resource Registry returned an error."); + + + private final int id; + private final String msg; + + RMCode(int id, String msg) { + if (exists(id)) + throw new IllegalArgumentException(String.format("Code %d already exists", id)); + this.id = id; + this.msg = msg; + } + + public int getId() { + return this.id; + } + + public String getMessage() { + return this.msg; + } + + protected static boolean exists(int id) { + return Arrays.stream(RMCode.values()).filter(v -> v.id == id).count() > 0; + } +}