CustomErrorController.java

1
package edu.ucsb.cs156.courses.controllers;
2
3
import jakarta.servlet.RequestDispatcher;
4
import jakarta.servlet.http.HttpServletRequest;
5
import java.io.PrintWriter;
6
import java.io.StringWriter;
7
import java.time.LocalDateTime;
8
import org.springframework.boot.web.servlet.error.ErrorController;
9
import org.springframework.http.HttpStatus;
10
import org.springframework.stereotype.Controller;
11
import org.springframework.ui.Model;
12
import org.springframework.web.bind.annotation.RequestMapping;
13
14
@Controller
15
public class CustomErrorController implements ErrorController {
16
17
  @RequestMapping("/error")
18
  public String handleError(HttpServletRequest request, Model model) {
19
    Object statusObj = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
20
21
    int statusCode = 500;
22 1 1. handleError : negated conditional → KILLED
    if (statusObj != null) {
23
      statusCode = Integer.parseInt(statusObj.toString());
24
    }
25
26
    HttpStatus httpStatus = HttpStatus.resolve(statusCode);
27 1 1. handleError : negated conditional → KILLED
    String error = httpStatus == null ? "Unknown Error" : httpStatus.getReasonPhrase();
28
29
    Object messageObj = request.getAttribute(RequestDispatcher.ERROR_MESSAGE);
30 1 1. handleError : negated conditional → KILLED
    String message = messageObj == null ? "" : messageObj.toString();
31
32
    Object pathObj = request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI);
33 1 1. handleError : negated conditional → KILLED
    String path = pathObj == null ? "" : pathObj.toString();
34
35
    Throwable throwable = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
36
37
    String exceptionMessage = "";
38
    String stackTrace = "";
39
40 1 1. handleError : negated conditional → KILLED
    if (throwable != null) {
41
      exceptionMessage = throwable.getMessage();
42
43
      StringWriter sw = new StringWriter();
44
      PrintWriter pw = new PrintWriter(sw);
45 1 1. handleError : removed call to java/lang/Throwable::printStackTrace → KILLED
      throwable.printStackTrace(pw);
46
      stackTrace = sw.toString();
47
    }
48
49
    model.addAttribute("status", statusCode);
50
    model.addAttribute("error", error);
51
    model.addAttribute("message", message);
52
    model.addAttribute("exceptionMessage", exceptionMessage);
53
    model.addAttribute("stackTrace", stackTrace);
54
    model.addAttribute("timestamp", LocalDateTime.now());
55
    model.addAttribute("path", path);
56
57 1 1. handleError : replaced return value with "" for edu/ucsb/cs156/courses/controllers/CustomErrorController::handleError → KILLED
    return "custom-error";
58
  }
59
}

Mutations

22

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

27

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

30

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

33

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

40

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

45

1.1
Location : handleError
Killed by : edu.ucsb.cs156.courses.controllers.CustomErrorControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.CustomErrorControllerTests]/[method:handleError_setsModelAttributes_for500WithException()]
removed call to java/lang/Throwable::printStackTrace → 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:handleError_setsDefaults_whenNoAttributes()]
replaced return value with "" for edu/ucsb/cs156/courses/controllers/CustomErrorController::handleError → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0