| 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 | @Controller | |
| 12 | public class CustomErrorController implements ErrorController { | |
| 13 | ||
| 14 | @RequestMapping("/error") | |
| 15 | public String handleError(HttpServletRequest request, Model model) { | |
| 16 | Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); | |
| 17 |
1
1. handleError : negated conditional → KILLED |
int statusCode = (status != null) ? Integer.parseInt(status.toString()) : 500; |
| 18 | ||
| 19 | String message = | |
| 20 | switch (statusCode) { | |
| 21 | case 404 -> "The page you are looking for might have been removed or is temporarily unavailable"; | |
| 22 | case 403 -> "You don't have permission to access this resource"; | |
| 23 | case 500 -> "We're sorry, something went wrong on our end"; | |
| 24 | default -> "An unexpected error occurred"; | |
| 25 | }; | |
| 26 | ||
| 27 | Throwable throwable = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION); | |
| 28 | ||
| 29 | model.addAttribute("status", statusCode); | |
| 30 | model.addAttribute("error", HttpStatus.valueOf(statusCode).getReasonPhrase()); | |
| 31 | model.addAttribute("message", message); | |
| 32 | model.addAttribute( | |
| 33 | "exceptionMessage", | |
| 34 |
1
1. handleError : negated conditional → KILLED |
throwable != null ? throwable.getMessage() : "No exception details available"); |
| 35 |
1
1. handleError : negated conditional → KILLED |
model.addAttribute("stackTrace", throwable != null ? stackToString(throwable) : ""); |
| 36 | model.addAttribute("timestamp", java.time.LocalDateTime.now()); | |
| 37 | model.addAttribute("path", request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)); | |
| 38 | ||
| 39 |
1
1. handleError : replaced return value with "" for edu/ucsb/cs156/courses/controllers/CustomErrorController::handleError → KILLED |
return "error"; |
| 40 | } | |
| 41 | ||
| 42 | private String stackToString(Throwable t) { | |
| 43 | StringBuilder sb = new StringBuilder(); | |
| 44 | for (StackTraceElement e : t.getStackTrace()) { | |
| 45 | sb.append(e.toString()).append("\n"); | |
| 46 | } | |
| 47 |
1
1. stackToString : replaced return value with "" for edu/ucsb/cs156/courses/controllers/CustomErrorController::stackToString → KILLED |
return sb.toString(); |
| 48 | } | |
| 49 | } | |
Mutations | ||
| 17 |
1.1 |
|
| 34 |
1.1 |
|
| 35 |
1.1 |
|
| 39 |
1.1 |
|
| 47 |
1.1 |