CustomErrorController.java

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
  /**
19
   * Handles error requests and returns a custom error page.
20
   *
21
   * @param request the HTTP request
22
   * @param model the model to pass attributes to the view
23
   * @return the name of the error view template
24
   */
25
  @RequestMapping("/error")
26
  public String handleError(HttpServletRequest request, Model model) {
27
    // Get error status
28
    Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
29
    int statusCode =
30 1 1. handleError : negated conditional → KILLED
        (status != null)
31
            ? Integer.parseInt(status.toString())
32
            : 500; // Default to internal server error
33
34
    // Get error message
35
    String message =
36
        switch (statusCode) {
37
          case 404 -> "The page you are looking for might have been removed or is temporarily unavailable";
38
          case 403 -> "You don't have permission to access this resource";
39
          case 500 -> "We're sorry, something went wrong on our end";
40
          default -> "An unexpected error occurred";
41
        };
42
43
    // Get exception details for debugging
44
    Throwable throwable = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
45
46
    // Add attributes to the model
47
    model.addAttribute("status", statusCode);
48
    model.addAttribute("error", HttpStatus.valueOf(statusCode).getReasonPhrase());
49
    model.addAttribute("message", message);
50
    model.addAttribute(
51
        "exceptionMessage",
52 1 1. handleError : negated conditional → KILLED
        throwable != null ? throwable.getMessage() : "No exception details available");
53 1 1. handleError : negated conditional → KILLED
    model.addAttribute("stackTrace", throwable != null ? stackToString(throwable) : "");
54
    model.addAttribute("timestamp", java.time.LocalDateTime.now());
55
    model.addAttribute("path", request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI));
56
57 1 1. handleError : replaced return value with "" for edu/ucsb/cs156/courses/controllers/CustomErrorController::handleError → KILLED
    return "error";
58
  }
59
60
  /**
61
   * Converts a Throwable's stack trace to a single string with line breaks.
62
   *
63
   * @param t the Throwable whose stack trace is to be converted
64
   * @return a String representation of the stack trace
65
   */
66
  private String stackToString(Throwable t) {
67
    StringBuilder sb = new StringBuilder();
68
    for (StackTraceElement e : t.getStackTrace()) {
69
      sb.append(e.toString()).append("\n");
70
    }
71 1 1. stackToString : replaced return value with "" for edu/ucsb/cs156/courses/controllers/CustomErrorController::stackToString → KILLED
    return sb.toString();
72
  }
73
}

Mutations

30

1.1
Location : handleError
Killed by : edu.ucsb.cs156.courses.controllers.CustomErrorControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.CustomErrorControllerTests]/[method:testHandleError_DefaultStatus()]
negated conditional → KILLED

52

1.1
Location : handleError
Killed by : edu.ucsb.cs156.courses.controllers.CustomErrorControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.CustomErrorControllerTests]/[method:testHandleError_DefaultStatus()]
negated conditional → KILLED

53

1.1
Location : handleError
Killed by : edu.ucsb.cs156.courses.controllers.CustomErrorControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.CustomErrorControllerTests]/[method:testHandleError_DefaultStatus()]
negated conditional → KILLED

57

1.1
Location : handleError
Killed by : edu.ucsb.cs156.courses.controllers.CustomErrorControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.CustomErrorControllerTests]/[method:testHandleError_DefaultStatus()]
replaced return value with "" for edu/ucsb/cs156/courses/controllers/CustomErrorController::handleError → KILLED

71

1.1
Location : stackToString
Killed by : edu.ucsb.cs156.courses.controllers.CustomErrorControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.CustomErrorControllerTests]/[method:testHandleError_500()]
replaced return value with "" for edu/ucsb/cs156/courses/controllers/CustomErrorController::stackToString → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0