working out how to handle exception based errors in a 'nice' way

This commit is contained in:
Yvan 2025-01-30 12:45:49 +00:00
parent 8ccd5390fa
commit b218591ea7
3 changed files with 16 additions and 2 deletions

View file

@ -5,7 +5,11 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import java.util.List; import java.util.List;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -21,6 +25,17 @@ public class BotController {
@Autowired @Autowired
private BotService botServ; private BotService botServ;
/**
* Send a 404 page for bots if someone tries to access a bot that doesn't exist.
*/
@ExceptionHandler(value = BotNotFoundByUsernameException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ModelAndView handleBotNotFoundByUsernameException(BotNotFoundByUsernameException ex) {
ModelAndView mav = new ModelAndView("user_not_found");
mav.addObject("message", ex.getLocalizedMessage());
return mav;
}
@GetMapping("/viewbot") @GetMapping("/viewbot")
public String listAll(Model model) { public String listAll(Model model) {
log.info("BotController::viewbot"); log.info("BotController::viewbot");

View file

@ -31,7 +31,7 @@ public class BotService {
} }
public Bot getBotByUsername( String username ) { public Bot getBotByUsername( String username ) {
return repo.findByUsername( username ).get(); return repo.findByUsername( username ).orElseThrow(() -> new BotNotFoundByUsernameException(username));
} }

View file

@ -7,7 +7,6 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.thymeleaf.context.Context; import org.thymeleaf.context.Context;