| 1 | package edu.ucsb.cs156.courses.controllers; | |
| 2 | ||
| 3 | import jakarta.servlet.RequestDispatcher; | |
| 4 | import jakarta.servlet.http.HttpServletRequest; | |
| 5 | import org.springframework.boot.web.servlet.error.ErrorController; | |
| 6 | import org.springframework.http.HttpStatus; | |
| 7 | import org.springframework.stereotype.Controller; | |
| 8 | import org.springframework.ui.Model; | |
| 9 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 10 | ||
| 11 | /** | |
| 12 | * Custom error controller that replaces the default white label error page with a more | |
| 13 | * user-friendly error page. | |
| 14 | */ | |
| 15 | @Controller | |
| 16 | public class CustomErrorController implements ErrorController { | |
| 17 | /** | |
| 18 | * Handles error requests and returns a custom error page. | |
| 19 | * | |
| 20 | * @param request the HTTP request | |
| 21 | * @param model the model to pass attributes to the view | |
| 22 | * @return the name of the error view template | |
| 23 | */ | |
| 24 | @RequestMapping("/error") | |
| 25 | public String handleError(HttpServletRequest request, Model model) { | |
| 26 | // Get error status | |
| 27 | Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); | |
| 28 | int statusCode = 500; // Default to internal server error | |
| 29 | ||
| 30 |
1
1. handleError : negated conditional → KILLED |
if (status != null) { |
| 31 | statusCode = Integer.parseInt(status.toString()); | |
| 32 | } | |
| 33 | ||
| 34 | // Get error message | |
| 35 | String errorMessage = "An unexpected error occurred"; | |
| 36 |
1
1. handleError : negated conditional → KILLED |
if (statusCode == 404) { |
| 37 | errorMessage = | |
| 38 | "The page you are looking for is either have been removed or temporarily unavailable."; | |
| 39 |
1
1. handleError : negated conditional → KILLED |
} else if (statusCode == 403) { |
| 40 | errorMessage = "Access Denied"; | |
| 41 |
1
1. handleError : negated conditional → KILLED |
} else if (statusCode == 500) { |
| 42 | errorMessage = "Something went wrong on our end. Sorry about that"; | |
| 43 | } | |
| 44 | ||
| 45 | // Get exception details for debugging | |
| 46 | Throwable throwable = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION); | |
| 47 | String exceptionMessage = | |
| 48 |
1
1. handleError : negated conditional → KILLED |
throwable != null ? throwable.getMessage() : "No exception details available"; |
| 49 | ||
| 50 | // Get stack trace | |
| 51 | String stackTrace = ""; | |
| 52 |
1
1. handleError : negated conditional → KILLED |
if (throwable != null) { |
| 53 | for (StackTraceElement element : throwable.getStackTrace()) { | |
| 54 | stackTrace += element.toString() + "\n"; | |
| 55 | } | |
| 56 | } | |
| 57 | ||
| 58 | // Add attributes to the model | |
| 59 | model.addAttribute("status", statusCode); | |
| 60 | model.addAttribute("error", HttpStatus.valueOf(statusCode).getReasonPhrase()); | |
| 61 | model.addAttribute("message", errorMessage); | |
| 62 | model.addAttribute("exceptionMessage", exceptionMessage); | |
| 63 | model.addAttribute("stackTrace", stackTrace); | |
| 64 | model.addAttribute("timestamp", java.time.LocalDateTime.now()); | |
| 65 | model.addAttribute("path", request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)); | |
| 66 | ||
| 67 |
1
1. handleError : replaced return value with "" for edu/ucsb/cs156/courses/controllers/CustomErrorController::handleError → KILLED |
return "error"; |
| 68 | } | |
| 69 | } | |
Mutations | ||
| 30 |
1.1 |
|
| 36 |
1.1 |
|
| 39 |
1.1 |
|
| 41 |
1.1 |
|
| 48 |
1.1 |
|
| 52 |
1.1 |
|
| 67 |
1.1 |