From 9bd896e5c7ab2fd001c592c887f3593030246b71 Mon Sep 17 00:00:00 2001 From: Francesco Date: Mon, 9 Oct 2023 17:28:55 +0200 Subject: [PATCH] Improved handling of 404 errors Instead of shipping an `error/404.html` template, added a `@ControllerAdvice` that handles `DbAdminNotFoundException`. This way, we only show our error page for exceptions that are thrown inside our controller's methods, and don't interfere with the 404 settings for the other routes defined by the user. --- .../java/tech/ailef/dbadmin/external/DbAdmin.java | 3 ++- .../controller/DefaultDbAdminController.java | 10 ++++++---- .../external/controller/GlobalController.java | 13 +++++++++++++ .../exceptions/DbAdminNotFoundException.java | 13 +++++++++++++ .../templates/{error/404.html => other/error.html} | 0 5 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 src/main/java/tech/ailef/dbadmin/external/exceptions/DbAdminNotFoundException.java rename src/main/resources/templates/{error/404.html => other/error.html} (100%) diff --git a/src/main/java/tech/ailef/dbadmin/external/DbAdmin.java b/src/main/java/tech/ailef/dbadmin/external/DbAdmin.java index 780bdb0..1dc99f6 100644 --- a/src/main/java/tech/ailef/dbadmin/external/DbAdmin.java +++ b/src/main/java/tech/ailef/dbadmin/external/DbAdmin.java @@ -54,6 +54,7 @@ import tech.ailef.dbadmin.external.dbmapping.DbFieldType; import tech.ailef.dbadmin.external.dbmapping.DbObjectSchema; import tech.ailef.dbadmin.external.dto.MappingError; import tech.ailef.dbadmin.external.exceptions.DbAdminException; +import tech.ailef.dbadmin.external.exceptions.DbAdminNotFoundException; import tech.ailef.dbadmin.external.exceptions.UnsupportedFieldTypeException; import tech.ailef.dbadmin.external.misc.Utils; @@ -132,7 +133,7 @@ public class DbAdmin { */ public DbObjectSchema findSchemaByClassName(String className) { return schemas.stream().filter(s -> s.getClassName().equals(className)).findFirst().orElseThrow(() -> { - return new DbAdminException("Schema " + className + " not found."); + return new DbAdminNotFoundException("Schema " + className + " not found."); }); } diff --git a/src/main/java/tech/ailef/dbadmin/external/controller/DefaultDbAdminController.java b/src/main/java/tech/ailef/dbadmin/external/controller/DefaultDbAdminController.java index 46f591f..06dfdec 100644 --- a/src/main/java/tech/ailef/dbadmin/external/controller/DefaultDbAdminController.java +++ b/src/main/java/tech/ailef/dbadmin/external/controller/DefaultDbAdminController.java @@ -57,6 +57,7 @@ import tech.ailef.dbadmin.external.dto.LogsSearchRequest; import tech.ailef.dbadmin.external.dto.PaginatedResult; import tech.ailef.dbadmin.external.dto.QueryFilter; import tech.ailef.dbadmin.external.exceptions.DbAdminException; +import tech.ailef.dbadmin.external.exceptions.DbAdminNotFoundException; import tech.ailef.dbadmin.external.exceptions.InvalidPageException; import tech.ailef.dbadmin.external.misc.Utils; import tech.ailef.dbadmin.internal.model.UserAction; @@ -248,11 +249,12 @@ public class DefaultDbAdminController { DbObjectSchema schema = dbAdmin.findSchemaByClassName(className); DbObject object = repository.findById(schema, id).orElseThrow(() -> { - return new ResponseStatusException( - HttpStatus.NOT_FOUND, "Object " + className + " with id " + id + " not found" + return new DbAdminNotFoundException( + "Object " + className + " with id " + id + " not found" ); }); + model.addAttribute("title", "Entities | " + schema.getJavaClass().getSimpleName() + " | " + object.getDisplayName()); model.addAttribute("object", object); model.addAttribute("activePage", "entities"); @@ -292,8 +294,8 @@ public class DefaultDbAdminController { } DbObject object = repository.findById(schema, id).orElseThrow(() -> { - return new ResponseStatusException( - HttpStatus.NOT_FOUND, "Object " + className + " with id " + id + " not found" + return new DbAdminNotFoundException( + "Object " + className + " with id " + id + " not found" ); }); diff --git a/src/main/java/tech/ailef/dbadmin/external/controller/GlobalController.java b/src/main/java/tech/ailef/dbadmin/external/controller/GlobalController.java index 8b53576..2718174 100644 --- a/src/main/java/tech/ailef/dbadmin/external/controller/GlobalController.java +++ b/src/main/java/tech/ailef/dbadmin/external/controller/GlobalController.java @@ -22,11 +22,14 @@ package tech.ailef.dbadmin.external.controller; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ModelAttribute; import jakarta.servlet.http.HttpServletRequest; import tech.ailef.dbadmin.external.DbAdminProperties; +import tech.ailef.dbadmin.external.exceptions.DbAdminNotFoundException; import tech.ailef.dbadmin.internal.UserConfiguration; /** @@ -42,6 +45,16 @@ public class GlobalController { @Autowired private UserConfiguration userConf; + @ExceptionHandler(DbAdminNotFoundException.class) + public String handleNotFound(Exception e, Model model) { + model.addAttribute("status", "404"); + model.addAttribute("error", "Error"); + model.addAttribute("message", e.getMessage()); + model.addAttribute("userConf", userConf); + model.addAttribute("baseUrl", getBaseUrl()); + return "other/error"; + } + /** * A multi valued map containing the query parameters. It is used primarily * in building complex URL when performing faceted search with multiple filters. diff --git a/src/main/java/tech/ailef/dbadmin/external/exceptions/DbAdminNotFoundException.java b/src/main/java/tech/ailef/dbadmin/external/exceptions/DbAdminNotFoundException.java new file mode 100644 index 0000000..ed0ca7d --- /dev/null +++ b/src/main/java/tech/ailef/dbadmin/external/exceptions/DbAdminNotFoundException.java @@ -0,0 +1,13 @@ +package tech.ailef.dbadmin.external.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.server.ResponseStatusException; + +public class DbAdminNotFoundException extends ResponseStatusException { + private static final long serialVersionUID = 4090093290330473479L; + + public DbAdminNotFoundException(String message) { + super(HttpStatus.NOT_FOUND, message); + } + +} diff --git a/src/main/resources/templates/error/404.html b/src/main/resources/templates/other/error.html similarity index 100% rename from src/main/resources/templates/error/404.html rename to src/main/resources/templates/other/error.html