{"record":{"id":"c610a6623bfc3a29","repo":"wuyouzhuguli/SpringAll","slug":"user-not-exist","errorCode":null,"errorMessage":"user not exist","messagePattern":"user not exist","errorType":"exception","errorClass":"UserNotExistException","httpStatus":500,"severity":"error","filePath":"25.Spring-Boot-Exception/src/main/java/cc/mrbird/controller/UserController.java","lineNumber":15,"sourceCode":"package cc.mrbird.controller;\n\nimport cc.mrbird.exception.UserNotExistException;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.PathVariable;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\n@RestController\n@RequestMapping(\"user\")\npublic class UserController {\n\n    @GetMapping(\"/{id:\\\\d+}\")\n    public void get(@PathVariable String id) {\n        throw new UserNotExistException(id);\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":18,"githubUrl":"https://github.com/wuyouzhuguli/SpringAll/blob/614d2578d9495acf53cc02f2dee9c6131cc5e51a/25.Spring-Boot-Exception/src/main/java/cc/mrbird/controller/UserController.java#L1-L18","documentation":"UserNotExistException is a project-defined RuntimeException (cc.mrbird.exception.UserNotExistException) whose super message is hardcoded to \"user not exist\" and which carries the offending id separately. UserController.get() throws it UNCONDITIONALLY for every GET /user/{id} where id matches \\\\d+. This is demo code for Spring MVC's @ControllerAdvice / @ExceptionHandler global exception handling, not real lookup logic.","triggerScenarios":"Any HTTP GET to /user/1, /user/42, etc. (path variable constrained to digits). The method always throws regardless of whether such a user exists.","commonSituations":"Developer left the demo throw in place expecting real service code; an @ExceptionHandler(UserNotExistException.class) is missing so Spring returns a default 500; the unconditional throw masks a missing findById call; tests hit the endpoint and see the literal message.","solutions":["Register a @ControllerAdvice with @ExceptionHandler(UserNotExistException.class) returning a structured error body and HTTP status (e.g., 400 or 404).","Replace the unconditional throw with a real lookup (userService.findById(id)) and throw only when the result is null.","If this is throwaway demo code, remove the endpoint before shipping.","Verify the exception's message/id fields are what your handler serializes to the client."],"exampleFix":"// before\n@GetMapping(\"/{id:\\\\d+}\")\npublic void get(@PathVariable String id) {\n    throw new UserNotExistException(id);\n}\n\n// after\n@GetMapping(\"/{id:\\\\d+}\")\npublic User get(@PathVariable String id) {\n    User user = userService.findById(id);\n    if (user == null) {\n        throw new UserNotExistException(id);\n    }\n    return user;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Register once via @ControllerAdvice\n@ControllerAdvice\npublic class GlobalExceptionHandler {\n    @ExceptionHandler(UserNotExistException.class)\n    public ResponseEntity<Map<String, Object>> handle(UserNotExistException e) {\n        Map<String, Object> body = new HashMap<>();\n        body.put(\"id\", e.getId());\n        body.put(\"message\", e.getMessage());\n        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(body);\n    }\n}","preventionTips":["Always pair a custom RuntimeException with a global @ExceptionHandler so it serializes to a clean response instead of a 500.","Do not throw unconditionally in a controller; gate the throw behind a real null check.","Strip demo endpoints before shipping to production."],"tags":["spring-mvc","exception-handling","custom-exception","demo"],"backgroundTag":null,"analyzedSha":"614d2578d9495acf53cc02f2dee9c6131cc5e51a","analyzedAt":"2026-08-14T04:40:03.488Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}