Untitled
user_1100245
plain_text
2 years ago
1.7 kB
8
Indexable
package com.fa.controller;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServletRequest;
@Controller
public class CustomErrorController implements ErrorController {
@RequestMapping("/error")
String error(HttpServletRequest request, Model model) {
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
if (status != null) {
int statusCode = Integer.parseInt(status.toString());
if (statusCode == HttpStatus.BAD_REQUEST.value()) {
String message = "Bad Request! Please try again!";
model.addAttribute("message", message);
return "400";
}
if (statusCode == HttpStatus.FORBIDDEN.value()) {
return "/accessDenied";
}
if (statusCode == HttpStatus.NOT_FOUND.value()) {
String message = "Link not found!";
model.addAttribute("message", message);
return "404";
}
if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
String message = "Some internal server error happend! Please try again!";
model.addAttribute("message", message);
return "500";
}
}
return "500";
}
public String getErrorPath(){
return "/error";
}
}Editor is loading...
Leave a Comment