| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import jakarta.servlet.RequestDispatcher; | |
| 4 | import jakarta.servlet.http.HttpServletRequest; | |
| 5 | import jakarta.servlet.http.HttpServletResponse; | |
| 6 | import lombok.extern.slf4j.Slf4j; | |
| 7 | import org.springframework.boot.autoconfigure.web.ServerProperties; | |
| 8 | import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; | |
| 9 | import org.springframework.boot.web.servlet.error.ErrorAttributes; | |
| 10 | import org.springframework.http.HttpStatus; | |
| 11 | import org.springframework.stereotype.Controller; | |
| 12 | import org.springframework.ui.ModelMap; | |
| 13 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 14 | import org.springframework.web.servlet.ModelAndView; | |
| 15 | ||
| 16 | /** | |
| 17 | * Custom error controller that replaces the default white label error page with a more | |
| 18 | * user-friendly error page. | |
| 19 | */ | |
| 20 | @Controller | |
| 21 | @Slf4j | |
| 22 | public class CustomErrorController extends BasicErrorController { | |
| 23 | ||
| 24 | public CustomErrorController(ErrorAttributes errorAttributes, ServerProperties properties) { | |
| 25 | super(errorAttributes, properties.getError()); | |
| 26 | } | |
| 27 | ||
| 28 | /** | |
| 29 | * Handles error requests and returns a custom error page. | |
| 30 | * | |
| 31 | * @param request the HTTP request | |
| 32 | * @param response the HTTP response | |
| 33 | * @return the name of the error view template | |
| 34 | */ | |
| 35 | @RequestMapping(produces = "text/html") | |
| 36 | @Override | |
| 37 | public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { | |
| 38 | // Get error status | |
| 39 | Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); | |
| 40 | int statusCode = 500; // Default to internal server error | |
| 41 | ||
| 42 |
1
1. errorHtml : negated conditional → KILLED |
if (status != null) { |
| 43 | statusCode = Integer.parseInt(status.toString()); | |
| 44 | } | |
| 45 | ||
| 46 | // Get error message | |
| 47 | String errorMessage = "An unexpected error occurred"; | |
| 48 |
1
1. errorHtml : negated conditional → KILLED |
if (statusCode == 404) { |
| 49 | errorMessage = | |
| 50 | "The page you are looking for might have been removed or is temporarily unavailable"; | |
| 51 |
1
1. errorHtml : negated conditional → KILLED |
} else if (statusCode == 403) { |
| 52 | errorMessage = "You don't have permission to access this resource"; | |
| 53 |
1
1. errorHtml : negated conditional → KILLED |
} else if (statusCode == 500) { |
| 54 | errorMessage = "We're sorry, something went wrong on our end"; | |
| 55 | } | |
| 56 | ||
| 57 | // Get exception details for debugging | |
| 58 | Throwable throwable = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION); | |
| 59 | String exceptionMessage = | |
| 60 |
1
1. errorHtml : negated conditional → KILLED |
throwable != null ? throwable.getMessage() : "No exception details available"; |
| 61 | ||
| 62 | // Get stack trace | |
| 63 | String stackTrace = ""; | |
| 64 |
1
1. errorHtml : negated conditional → KILLED |
if (throwable != null) { |
| 65 | for (StackTraceElement element : throwable.getStackTrace()) { | |
| 66 | stackTrace += element.toString() + "\n"; | |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | ModelMap model = new ModelMap(); | |
| 71 | ||
| 72 | // Add attributes to the model | |
| 73 | model.addAttribute("status", statusCode); | |
| 74 | model.addAttribute("error", HttpStatus.valueOf(statusCode).getReasonPhrase()); | |
| 75 | model.addAttribute("message", errorMessage); | |
| 76 | model.addAttribute("exceptionMessage", exceptionMessage); | |
| 77 | model.addAttribute("stackTrace", stackTrace); | |
| 78 | model.addAttribute("timestamp", java.time.LocalDateTime.now()); | |
| 79 | model.addAttribute("path", request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)); | |
| 80 | ||
| 81 |
1
1. errorHtml : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CustomErrorController::errorHtml → KILLED |
return new ModelAndView("error", model); |
| 82 | } | |
| 83 | } | |
Mutations | ||
| 42 |
1.1 |
|
| 48 |
1.1 |
|
| 51 |
1.1 |
|
| 53 |
1.1 |
|
| 60 |
1.1 |
|
| 64 |
1.1 |
|
| 81 |
1.1 |