mirror of
https://github.com/dalbodeule/snap-admin.git
synced 2025-06-09 05:48:20 +00:00
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:
parent
0b1b394144
commit
9bd896e5c7
@ -54,6 +54,7 @@ import tech.ailef.dbadmin.external.dbmapping.DbFieldType;
|
|||||||
import tech.ailef.dbadmin.external.dbmapping.DbObjectSchema;
|
import tech.ailef.dbadmin.external.dbmapping.DbObjectSchema;
|
||||||
import tech.ailef.dbadmin.external.dto.MappingError;
|
import tech.ailef.dbadmin.external.dto.MappingError;
|
||||||
import tech.ailef.dbadmin.external.exceptions.DbAdminException;
|
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.exceptions.UnsupportedFieldTypeException;
|
||||||
import tech.ailef.dbadmin.external.misc.Utils;
|
import tech.ailef.dbadmin.external.misc.Utils;
|
||||||
|
|
||||||
@ -132,7 +133,7 @@ public class DbAdmin {
|
|||||||
*/
|
*/
|
||||||
public DbObjectSchema findSchemaByClassName(String className) {
|
public DbObjectSchema findSchemaByClassName(String className) {
|
||||||
return schemas.stream().filter(s -> s.getClassName().equals(className)).findFirst().orElseThrow(() -> {
|
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.");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,6 +57,7 @@ import tech.ailef.dbadmin.external.dto.LogsSearchRequest;
|
|||||||
import tech.ailef.dbadmin.external.dto.PaginatedResult;
|
import tech.ailef.dbadmin.external.dto.PaginatedResult;
|
||||||
import tech.ailef.dbadmin.external.dto.QueryFilter;
|
import tech.ailef.dbadmin.external.dto.QueryFilter;
|
||||||
import tech.ailef.dbadmin.external.exceptions.DbAdminException;
|
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.exceptions.InvalidPageException;
|
||||||
import tech.ailef.dbadmin.external.misc.Utils;
|
import tech.ailef.dbadmin.external.misc.Utils;
|
||||||
import tech.ailef.dbadmin.internal.model.UserAction;
|
import tech.ailef.dbadmin.internal.model.UserAction;
|
||||||
@ -248,11 +249,12 @@ public class DefaultDbAdminController {
|
|||||||
DbObjectSchema schema = dbAdmin.findSchemaByClassName(className);
|
DbObjectSchema schema = dbAdmin.findSchemaByClassName(className);
|
||||||
|
|
||||||
DbObject object = repository.findById(schema, id).orElseThrow(() -> {
|
DbObject object = repository.findById(schema, id).orElseThrow(() -> {
|
||||||
return new ResponseStatusException(
|
return new DbAdminNotFoundException(
|
||||||
HttpStatus.NOT_FOUND, "Object " + className + " with id " + id + " not found"
|
"Object " + className + " with id " + id + " not found"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
model.addAttribute("title", "Entities | " + schema.getJavaClass().getSimpleName() + " | " + object.getDisplayName());
|
model.addAttribute("title", "Entities | " + schema.getJavaClass().getSimpleName() + " | " + object.getDisplayName());
|
||||||
model.addAttribute("object", object);
|
model.addAttribute("object", object);
|
||||||
model.addAttribute("activePage", "entities");
|
model.addAttribute("activePage", "entities");
|
||||||
@ -292,8 +294,8 @@ public class DefaultDbAdminController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DbObject object = repository.findById(schema, id).orElseThrow(() -> {
|
DbObject object = repository.findById(schema, id).orElseThrow(() -> {
|
||||||
return new ResponseStatusException(
|
return new DbAdminNotFoundException(
|
||||||
HttpStatus.NOT_FOUND, "Object " + className + " with id " + id + " not found"
|
"Object " + className + " with id " + id + " not found"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -22,11 +22,14 @@ package tech.ailef.dbadmin.external.controller;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import tech.ailef.dbadmin.external.DbAdminProperties;
|
import tech.ailef.dbadmin.external.DbAdminProperties;
|
||||||
|
import tech.ailef.dbadmin.external.exceptions.DbAdminNotFoundException;
|
||||||
import tech.ailef.dbadmin.internal.UserConfiguration;
|
import tech.ailef.dbadmin.internal.UserConfiguration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,6 +45,16 @@ public class GlobalController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserConfiguration userConf;
|
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
|
* A multi valued map containing the query parameters. It is used primarily
|
||||||
* in building complex URL when performing faceted search with multiple filters.
|
* in building complex URL when performing faceted search with multiple filters.
|
||||||
|
13
src/main/java/tech/ailef/dbadmin/external/exceptions/DbAdminNotFoundException.java
vendored
Normal file
13
src/main/java/tech/ailef/dbadmin/external/exceptions/DbAdminNotFoundException.java
vendored
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user