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.
This commit is contained in:
Francesco 2023-10-09 17:28:55 +02:00
parent 0b1b394144
commit 9bd896e5c7
5 changed files with 34 additions and 5 deletions

View File

@ -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.");
});
}

View File

@ -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"
);
});

View File

@ -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.

View File

@ -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);
}
}