{"record":{"id":"3710e93f518ca9d3","repo":"yudaocode/SpringBoot-Labs","slug":"1001002000","errorCode":"1001002000","errorMessage":"用户不存在","messagePattern":"用户不存在","errorType":"http","errorClass":"ServiceException","httpStatus":500,"severity":"error","filePath":"lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab23/springmvc/controller/UserController.java","lineNumber":81,"sourceCode":"    public UserVO get3(@RequestParam(\"id\") Integer id) {\n        // 查询并返回用户\n        return new UserVO().setId(id).setUsername(UUID.randomUUID().toString());\n    }\n\n    /**\n     * 测试抛出 NullPointerException 异常\n     */\n    @GetMapping(\"/exception-01\")\n    public UserVO exception01() {\n        throw new NullPointerException(\"没有粗面鱼丸\");\n    }\n\n    /**\n     * 测试抛出 ServiceException 异常\n     */\n    @GetMapping(\"/exception-02\")\n    public UserVO exception02() {\n        throw new ServiceException(ServiceExceptionEnum.USER_NOT_FOUND);\n    }\n\n    @GetMapping(\"/do_something\")\n    public void doSomething() {\n        logger.info(\"[doSomething]\");\n    }\n\n    @GetMapping(\"/current_user\")\n    public UserVO currentUser() {\n        logger.info(\"[currentUser]\");\n        return new UserVO().setId(10).setUsername(UUID.randomUUID().toString());\n    }\n\n    @GetMapping(\"/exception-03\")\n    public void exception03() {\n        logger.info(\"[exception03]\");\n        throw new ServiceException(ServiceExceptionEnum.USER_NOT_FOUND);\n    }","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/yudaocode/SpringBoot-Labs/blob/6c12efaed06d12907a0f40dd2ad1f7020aec8798/lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab23/springmvc/controller/UserController.java#L63-L99","documentation":"A ServiceException carrying ServiceExceptionEnum.USER_NOT_FOUND (message '用户不存在', code 1001002000) thrown from GET /user/exception-02. ServiceException is the lab's custom business exception, designed to be intercepted by the GlobalExceptionHandler (@ControllerAdvice) and serialized as a CommonResult with the numeric code, so HTTP status stays 200 while the body reports the business failure. It demonstrates separating business errors from system errors in Spring MVC.","triggerScenarios":"Calling GET /user/exception-02. The method body unconditionally throws new ServiceException(ServiceExceptionEnum.USER_NOT_FOUND).","commonSituations":"Triggered on purpose to verify the global-exception-handler JSON shape ({code:1001002000, message:'用户不存在', data:null}). In production-style code the same exception is thrown by service layers when a lookup by id returns null; hitting it usually means the requested user id genuinely does not exist or was deleted.","solutions":["Treat this as the expected error contract: check CommonResult.code == 1001002000 in the caller instead of expecting an exception on the wire.","Verify the GlobalExceptionHandler bean is registered (same component-scan package) so ServiceException maps to a CommonResult body rather than a 500.","If testing with a real user lookup, use an id that exists, or first check existence via the repository before throwing.","Keep ServiceExceptionEnum codes documented so clients can switch on codes like 1001002000."],"exampleFix":"// before: caller lets business error propagate raw\nUserVO user = userService.get(id); // may throw ServiceException(USER_NOT_FOUND)\n\n// after: caller handles the unified result\ntry {\n    UserVO user = userService.get(id);\n} catch (ServiceException e) {\n    if (ServiceExceptionEnum.USER_NOT_FOUND.getCode().equals(e.getCode())) {\n        return ResponseEntity.notFound().build();\n    }\n    throw e;\n}","handlingStrategy":"try-catch","validationCode":"// Before calling a real lookup, verify existence:\nif (userRepository.findById(id).isEmpty()) {\n    return ResponseEntity.notFound().build();\n}","typeGuard":null,"tryCatchPattern":"try {\n    UserVO user = userService.get(id);\n} catch (ServiceException e) {\n    if (ServiceExceptionEnum.USER_NOT_FOUND.getCode().equals(e.getCode())) {\n        // 404-style handling\n    } else { throw e; }\n}","preventionTips":["Check the returned CommonResult.code instead of relying on exceptions crossing the wire.","Document ServiceExceptionEnum codes in the API contract shared with clients.","Validate ids (non-null, plausible range) before invoking the service."],"tags":["spring-mvc","business-exception","service-exception","tutorial","error-code"],"backgroundTag":null,"analyzedSha":"6c12efaed06d12907a0f40dd2ad1f7020aec8798","analyzedAt":"2026-08-14T13:06:31.500Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}