From 68be3117b1d1ce2030e1e4fa6bbe7bcce06074e0 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Tue, 10 Mar 2020 10:41:49 +0200 Subject: [PATCH] Moved BadRequestException to API Changed Bad Request response to JSON: { "status": 400, "error": "message", "target": "/requested/target" } --- .../web/resolver/exception}/BadRequestException.java | 9 ++++++--- .../plan/delivery/webserver/ResponseFactory.java | 9 +++++++-- .../plan/delivery/webserver/ResponseResolver.java | 2 +- .../webserver/resolver/json/GraphsJSONResolver.java | 2 +- .../com/djrapitops/plan/identification/Identifiers.java | 2 +- .../plan/delivery/webserver/HttpsServerTest.java | 2 +- 6 files changed, 17 insertions(+), 9 deletions(-) rename Plan/{common/src/main/java/com/djrapitops/plan/exceptions/connection => api/src/main/java/com/djrapitops/plan/delivery/web/resolver/exception}/BadRequestException.java (68%) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/exceptions/connection/BadRequestException.java b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/exception/BadRequestException.java similarity index 68% rename from Plan/common/src/main/java/com/djrapitops/plan/exceptions/connection/BadRequestException.java rename to Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/exception/BadRequestException.java index eb054378c..0657d9c06 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/exceptions/connection/BadRequestException.java +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resolver/exception/BadRequestException.java @@ -14,14 +14,17 @@ * You should have received a copy of the GNU Lesser General Public License * along with Plan. If not, see . */ -package com.djrapitops.plan.exceptions.connection; +package com.djrapitops.plan.delivery.web.resolver.exception; /** - * Thrown when connection is returned 401 Bad Request. + * Throw this exception when a Resolver gets invalid query for JSON. + *

+ * Plan will construct bad request error json automatically. + * Note that you might need to handle the error page, which is json: {@code {"status": 400, "error": "message"}} * * @author Rsl1122 */ -public class BadRequestException extends WebException { +public class BadRequestException extends IllegalArgumentException { public BadRequestException(String message) { super(message); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseFactory.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseFactory.java index 0bf6ea134..537a6c8c3 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseFactory.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseFactory.java @@ -33,6 +33,7 @@ import com.djrapitops.plan.storage.database.DBSystem; import com.djrapitops.plan.storage.database.Database; import com.djrapitops.plan.storage.database.queries.containers.ContainerFetchQueries; import com.djrapitops.plan.storage.file.PlanFiles; +import com.djrapitops.plan.utilities.java.Maps; import javax.inject.Inject; import javax.inject.Singleton; @@ -340,8 +341,12 @@ public class ResponseFactory { public Response badRequest(String errorMessage, String target) { return Response.builder() - .setMimeType(MimeType.HTML) - .setContent("400 Bad Request: " + errorMessage + " (when requesting '" + target + "')") + .setMimeType(MimeType.JSON) + .setJSONContent(Maps.builder(String.class, Object.class) + .put("status", 400) + .put("error", errorMessage) + .put("requestedTarget", target) + .build()) .setStatus(400) .build(); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseResolver.java index 052c21141..bbd9ac9bb 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseResolver.java @@ -21,12 +21,12 @@ import com.djrapitops.plan.delivery.web.ResolverSvc; import com.djrapitops.plan.delivery.web.resolver.NoAuthResolver; import com.djrapitops.plan.delivery.web.resolver.Resolver; import com.djrapitops.plan.delivery.web.resolver.Response; +import com.djrapitops.plan.delivery.web.resolver.exception.BadRequestException; import com.djrapitops.plan.delivery.web.resolver.request.Request; import com.djrapitops.plan.delivery.webserver.auth.Authentication; import com.djrapitops.plan.delivery.webserver.resolver.*; import com.djrapitops.plan.delivery.webserver.resolver.json.RootJSONResolver; import com.djrapitops.plan.exceptions.WebUserAuthException; -import com.djrapitops.plan.exceptions.connection.BadRequestException; import com.djrapitops.plan.exceptions.connection.ForbiddenException; import com.djrapitops.plan.exceptions.connection.NotFoundException; import com.djrapitops.plugin.logging.L; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/resolver/json/GraphsJSONResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/resolver/json/GraphsJSONResolver.java index 7ff19e463..8a2d6f386 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/resolver/json/GraphsJSONResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/resolver/json/GraphsJSONResolver.java @@ -19,11 +19,11 @@ package com.djrapitops.plan.delivery.webserver.resolver.json; import com.djrapitops.plan.delivery.rendering.json.graphs.GraphJSONCreator; import com.djrapitops.plan.delivery.web.resolver.Resolver; import com.djrapitops.plan.delivery.web.resolver.Response; +import com.djrapitops.plan.delivery.web.resolver.exception.BadRequestException; import com.djrapitops.plan.delivery.web.resolver.request.Request; import com.djrapitops.plan.delivery.web.resolver.request.WebUser; import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.JSONCache; -import com.djrapitops.plan.exceptions.connection.BadRequestException; import com.djrapitops.plan.identification.Identifiers; import javax.inject.Inject; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/identification/Identifiers.java b/Plan/common/src/main/java/com/djrapitops/plan/identification/Identifiers.java index d48b3e4d7..52e224ec1 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/identification/Identifiers.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/identification/Identifiers.java @@ -16,8 +16,8 @@ */ package com.djrapitops.plan.identification; +import com.djrapitops.plan.delivery.web.resolver.exception.BadRequestException; import com.djrapitops.plan.delivery.web.resolver.request.Request; -import com.djrapitops.plan.exceptions.connection.BadRequestException; import com.djrapitops.plan.storage.database.DBSystem; import com.djrapitops.plan.storage.database.queries.objects.ServerQueries; import com.djrapitops.plan.storage.database.queries.objects.UserIdentifierQueries; diff --git a/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/HttpsServerTest.java b/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/HttpsServerTest.java index dbbda46b6..1b0967fee 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/HttpsServerTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/HttpsServerTest.java @@ -16,7 +16,7 @@ */ package com.djrapitops.plan.delivery.webserver; -import com.djrapitops.plan.exceptions.connection.BadRequestException; +import com.djrapitops.plan.delivery.web.resolver.exception.BadRequestException; import com.djrapitops.plan.exceptions.connection.ForbiddenException; import com.djrapitops.plan.exceptions.connection.NotFoundException; import com.djrapitops.plan.exceptions.connection.WebException;