Improve CodeExceptions.
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@161895 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
7b31392c6f
commit
9a9e0a0a06
|
@ -6,15 +6,16 @@ package org.gcube.resourcemanagement.manager.exception;
|
||||||
* @author Manuele Simi (ISTI CNR)
|
* @author Manuele Simi (ISTI CNR)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class RMException extends Exception {
|
public class CodeException extends Exception {
|
||||||
|
|
||||||
private static final long serialVersionUID = 333945715086602250L;
|
private static final long serialVersionUID = 333945715086602250L;
|
||||||
|
|
||||||
private final int errorCode;
|
private final int errorCode;
|
||||||
private final String errorMsg;
|
private final String errorMsg;
|
||||||
|
|
||||||
public RMException(ExceptionCode code) {
|
public CodeException(RMCode code) {
|
||||||
this.errorMsg = code.getMsg();
|
super();
|
||||||
|
this.errorMsg = code.getMessage();
|
||||||
this.errorCode = code.getId();
|
this.errorCode = code.getId();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,3 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.gcube.resourcemanagement.manager.exception;
|
package org.gcube.resourcemanagement.manager.exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -10,7 +7,15 @@ package org.gcube.resourcemanagement.manager.exception;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface ErrorCode {
|
public interface ErrorCode {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identifier of the code.
|
||||||
|
*/
|
||||||
public int getId();
|
public int getId();
|
||||||
|
|
||||||
public String getMsg();
|
/**
|
||||||
|
* The message associated to the code
|
||||||
|
* @return the message
|
||||||
|
*/
|
||||||
|
public String getMessage();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue