Added Response codes to WebExceptions to get rid of an instanceof chain.

This commit is contained in:
Rsl1122 2018-05-25 12:12:14 +03:00
parent d9fdad0086
commit f2cdd3804b
11 changed files with 100 additions and 46 deletions

View File

@ -4,6 +4,8 @@
*/ */
package com.djrapitops.plan.api.exceptions.connection; package com.djrapitops.plan.api.exceptions.connection;
import com.djrapitops.plan.system.webserver.response.ResponseCode;
/** /**
* Thrown when connection is returned 401 Bad Request. * Thrown when connection is returned 401 Bad Request.
* *
@ -12,6 +14,6 @@ package com.djrapitops.plan.api.exceptions.connection;
public class BadRequestException extends WebException { public class BadRequestException extends WebException {
public BadRequestException(String message) { public BadRequestException(String message) {
super(message); super(message, ResponseCode.BAD_REQUEST);
} }
} }

View File

@ -4,6 +4,8 @@
*/ */
package com.djrapitops.plan.api.exceptions.connection; package com.djrapitops.plan.api.exceptions.connection;
import com.djrapitops.plan.system.webserver.response.ResponseCode;
/** /**
* Thrown when Connection fails to connect to an address. * Thrown when Connection fails to connect to an address.
* *
@ -12,10 +14,10 @@ package com.djrapitops.plan.api.exceptions.connection;
public class ConnectionFailException extends WebException { public class ConnectionFailException extends WebException {
public ConnectionFailException(String message, Throwable cause) { public ConnectionFailException(String message, Throwable cause) {
super(message, cause); super(message, cause, ResponseCode.CONNECTION_REFUSED);
} }
public ConnectionFailException(Throwable cause) { public ConnectionFailException(Throwable cause) {
super(cause); super(cause, ResponseCode.CONNECTION_REFUSED);
} }
} }

View File

@ -4,6 +4,8 @@
*/ */
package com.djrapitops.plan.api.exceptions.connection; package com.djrapitops.plan.api.exceptions.connection;
import com.djrapitops.plan.system.webserver.response.ResponseCode;
/** /**
* Thrown when Connection gets a 403 response. * Thrown when Connection gets a 403 response.
* *
@ -11,6 +13,6 @@ package com.djrapitops.plan.api.exceptions.connection;
*/ */
public class ForbiddenException extends WebFailException { public class ForbiddenException extends WebFailException {
public ForbiddenException(String url) { public ForbiddenException(String url) {
super("Forbidden: " + url); super("Forbidden: " + url, ResponseCode.FORBIDDEN);
} }
} }

View File

@ -4,6 +4,8 @@
*/ */
package com.djrapitops.plan.api.exceptions.connection; package com.djrapitops.plan.api.exceptions.connection;
import com.djrapitops.plan.system.webserver.response.ResponseCode;
/** /**
* Thrown when InfoRequest can not connect to the other server. * Thrown when InfoRequest can not connect to the other server.
* *
@ -12,14 +14,6 @@ package com.djrapitops.plan.api.exceptions.connection;
public class GatewayException extends WebException { public class GatewayException extends WebException {
public GatewayException(String message) { public GatewayException(String message) {
super(message); super(message, ResponseCode.GATEWAY_ERROR);
}
public GatewayException(String message, Throwable cause) {
super(message, cause);
}
public GatewayException(Throwable cause) {
super(cause);
} }
} }

View File

@ -4,6 +4,8 @@
*/ */
package com.djrapitops.plan.api.exceptions.connection; package com.djrapitops.plan.api.exceptions.connection;
import com.djrapitops.plan.system.webserver.response.ResponseCode;
/** /**
* Thrown when Connection returns 500. * Thrown when Connection returns 500.
* *
@ -11,10 +13,10 @@ package com.djrapitops.plan.api.exceptions.connection;
*/ */
public class InternalErrorException extends WebFailException { public class InternalErrorException extends WebFailException {
public InternalErrorException() { public InternalErrorException() {
super("Internal Error occurred on receiving server"); super("Internal Error occurred on receiving server", ResponseCode.INTERNAL_ERROR);
} }
public InternalErrorException(String message, Throwable cause) { public InternalErrorException(String message, Throwable cause) {
super(message, cause); super(message, cause, ResponseCode.INTERNAL_ERROR);
} }
} }

View File

@ -4,6 +4,8 @@
*/ */
package com.djrapitops.plan.api.exceptions.connection; package com.djrapitops.plan.api.exceptions.connection;
import com.djrapitops.plan.system.webserver.response.ResponseCode;
/** /**
* Thrown when Connection returns 404, when page is not found. * Thrown when Connection returns 404, when page is not found.
* *
@ -11,6 +13,6 @@ package com.djrapitops.plan.api.exceptions.connection;
*/ */
public class NotFoundException extends WebFailException { public class NotFoundException extends WebFailException {
public NotFoundException(String message) { public NotFoundException(String message) {
super(message); super(message, ResponseCode.NOT_FOUND);
} }
} }

View File

@ -4,6 +4,8 @@
*/ */
package com.djrapitops.plan.api.exceptions.connection; package com.djrapitops.plan.api.exceptions.connection;
import com.djrapitops.plan.system.webserver.response.ResponseCode;
/** /**
* Thrown when Connection gets a 412 response due to ServerUUID not being in the database. * Thrown when Connection gets a 412 response due to ServerUUID not being in the database.
* *
@ -12,14 +14,14 @@ package com.djrapitops.plan.api.exceptions.connection;
public class UnauthorizedServerException extends WebFailException { public class UnauthorizedServerException extends WebFailException {
public UnauthorizedServerException(String message) { public UnauthorizedServerException(String message) {
super(message); super(message, ResponseCode.PRECONDITION_FAILED);
} }
public UnauthorizedServerException(String message, Throwable cause) { public UnauthorizedServerException(String message, Throwable cause) {
super(message, cause); super(message, cause, ResponseCode.PRECONDITION_FAILED);
} }
public UnauthorizedServerException(Throwable cause) { public UnauthorizedServerException(Throwable cause) {
super(cause); super(cause, ResponseCode.PRECONDITION_FAILED);
} }
} }

View File

@ -4,6 +4,8 @@
*/ */
package com.djrapitops.plan.api.exceptions.connection; package com.djrapitops.plan.api.exceptions.connection;
import com.djrapitops.plan.system.webserver.response.ResponseCode;
/** /**
* Thrown when Connection POST-request fails, general Exception. * Thrown when Connection POST-request fails, general Exception.
* *
@ -11,18 +13,47 @@ package com.djrapitops.plan.api.exceptions.connection;
*/ */
public class WebException extends Exception { public class WebException extends Exception {
private final ResponseCode responseCode;
public WebException() { public WebException() {
responseCode = ResponseCode.NONE;
} }
public WebException(String message) { public WebException(String message) {
super(message); super(message);
responseCode = ResponseCode.NONE;
} }
public WebException(String message, Throwable cause) { public WebException(String message, Throwable cause) {
super(message, cause); super(message, cause);
responseCode = ResponseCode.NONE;
} }
public WebException(Throwable cause) { public WebException(Throwable cause) {
super(cause); super(cause);
responseCode = ResponseCode.NONE;
}
public WebException(ResponseCode responseCode) {
this.responseCode = responseCode;
}
public WebException(String message, ResponseCode responseCode) {
super(message);
this.responseCode = responseCode;
}
public WebException(String message, Throwable cause, ResponseCode responseCode) {
super(message, cause);
this.responseCode = responseCode;
}
public WebException(Throwable cause, ResponseCode responseCode) {
super(cause);
this.responseCode = responseCode;
}
public ResponseCode getResponseCode() {
return responseCode;
} }
} }

View File

@ -4,6 +4,8 @@
*/ */
package com.djrapitops.plan.api.exceptions.connection; package com.djrapitops.plan.api.exceptions.connection;
import com.djrapitops.plan.system.webserver.response.ResponseCode;
/** /**
* Group of WebExceptions that can be considered a failed connection state on some occasions. * Group of WebExceptions that can be considered a failed connection state on some occasions.
* *
@ -11,18 +13,19 @@ package com.djrapitops.plan.api.exceptions.connection;
*/ */
public class WebFailException extends WebException { public class WebFailException extends WebException {
public WebFailException() {
}
public WebFailException(String message) {
super(message);
}
public WebFailException(String message, Throwable cause) { public WebFailException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }
public WebFailException(Throwable cause) { public WebFailException(String message, ResponseCode responseCode) {
super(cause); super(message, responseCode);
}
public WebFailException(String message, Throwable cause, ResponseCode responseCode) {
super(message, cause, responseCode);
}
public WebFailException(Throwable cause, ResponseCode responseCode) {
super(cause, responseCode);
} }
} }

View File

@ -4,7 +4,8 @@
*/ */
package com.djrapitops.plan.system.info.connection; package com.djrapitops.plan.system.info.connection;
import com.djrapitops.plan.api.exceptions.connection.*; import com.djrapitops.plan.api.exceptions.connection.NotFoundException;
import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.system.info.request.InfoRequest; import com.djrapitops.plan.system.info.request.InfoRequest;
import com.djrapitops.plan.system.webserver.Request; import com.djrapitops.plan.system.webserver.Request;
import com.djrapitops.plan.system.webserver.pages.PageHandler; import com.djrapitops.plan.system.webserver.pages.PageHandler;
@ -54,22 +55,6 @@ public class InfoRequestPageHandler extends PageHandler {
} }
private int getResponseCodeFor(WebException e) { private int getResponseCodeFor(WebException e) {
if (e instanceof BadRequestException) { return e.getResponseCode().getCode();
return 400;
} else if (e instanceof ForbiddenException) {
return 403;
} else if (e instanceof NotFoundException) {
return 404;
} else if (e instanceof UnauthorizedServerException) {
return 412;
} else if (e instanceof InternalErrorException) {
return 500;
} else if (e instanceof GatewayException) {
return 504;
} else if (e instanceof ConnectionFailException) {
return -1;
} else {
return 0;
}
} }
} }

View File

@ -0,0 +1,29 @@
package com.djrapitops.plan.system.webserver.response;
/**
* Enum for HTTP response codes.
*
* @author Rsl1122
*/
public enum ResponseCode {
NONE(0),
CONNECTION_REFUSED(-1),
SUCCESS(200),
BAD_REQUEST(400),
UNAUTHORIZED(401),
FORBIDDEN(403),
NOT_FOUND(404),
PRECONDITION_FAILED(412),
INTERNAL_ERROR(500),
GATEWAY_ERROR(504);
private final int code;
ResponseCode(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}