{"record":{"id":"af5c9c0a7e8a4abd","repo":"yudaocode/SpringBoot-Labs","slug":"1001002001-af5c9c","errorCode":"1001002001","errorMessage":"用户已存在","messagePattern":"用户已存在","errorType":"error_code","errorClass":"ServiceException","httpStatus":null,"severity":"warning","filePath":"labx-07-spring-cloud-alibaba-dubbo/labx-07-sca-dubbo-demo04-filter/labx-07-sca-dubbo-demo04-provider/src/main/java/cn/iocoder/springcloudalibaba/labx7/providerdemo/service/UserServiceImpl.java","lineNumber":23,"sourceCode":"import cn.iocoder.springcloudalibaba.labx7.core.ServiceExceptionEnum;\nimport cn.iocoder.springcloudalibaba.labx7.dto.UserAddDTO;\nimport cn.iocoder.springcloudalibaba.labx7.dto.UserDTO;\n\n@org.apache.dubbo.config.annotation.Service(protocol = \"dubbo\", version = \"1.0.0\", validation = \"true\", filter = \"-exception\")\npublic class UserServiceImpl implements UserService {\n\n    @Override\n    public UserDTO get(Integer id) {\n        return new UserDTO().setId(id)\n                .setName(\"没有昵称：\" + id)\n                .setGender(id % 2 + 1); // 1 - 男；2 - 女\n    }\n\n    @Override\n    public Integer add(UserAddDTO addDTO) {\n        // 【额外添加】这里，模拟用户已经存在的情况\n        if (\"yudaoyuanma\".equals(addDTO.getName())) {\n            throw new ServiceException(ServiceExceptionEnum.USER_EXISTS);\n        }\n        return (int) (System.currentTimeMillis() / 1000); // 嘿嘿，随便返回一个 id\n    }\n\n}\n","sourceCodeStart":5,"sourceCodeEnd":29,"githubUrl":"https://github.com/yudaocode/SpringBoot-Labs/blob/6c12efaed06d12907a0f40dd2ad1f7020aec8798/labx-07-spring-cloud-alibaba-dubbo/labx-07-sca-dubbo-demo04-filter/labx-07-sca-dubbo-demo04-provider/src/main/java/cn/iocoder/springcloudalibaba/labx7/providerdemo/service/UserServiceImpl.java#L5-L29","documentation":"Deliberate ServiceException (code 1001002001, USER_EXISTS) thrown by the Dubbo provider in labx-07 demo04, which demonstrates Dubbo Filters. When UserService.add is called with the name 'yudaoyuanma', the provider throws ServiceException constructed from ServiceExceptionEnum.USER_EXISTS; the accompanying AccessContextFilter / GlobalExceptionHandler machinery in the demo shows how a custom Dubbo filter can unwrap ServiceException on the consumer side and rethrow it with the original code+message.","triggerScenarios":"RPC call UserService.add(new UserAddDTO().setName(\"yudaoyuanma\")) — any other name returns a fake id; the special name is hard-coded to simulate a duplicate-user conflict.","commonSituations":"Running the demo04 integration test or tutorial curl that uses the reserved name; forgetting this sentinel value exists and reusing it as real test data; consumers not registering the demo's filter so the ServiceException arrives wrapped in RuntimeException and loses its code.","solutions":["Call add with any name other than 'yudaoyuanma' to get the success path.","On the consumer, ensure the demo's Dubbo filter is loaded (scan/activate it) so ServiceException is deserialized with code 1001002001 intact.","If you extended this into real code, replace the hard-coded check with a real uniqueness check and return the same ServiceException shape."],"exampleFix":"// before\nif (\"yudaoyuanma\".equals(addDTO.getName())) {\n    throw new ServiceException(ServiceExceptionEnum.USER_EXISTS);\n}\n\n// after — real duplicate check keeping the same error contract\nif (userMapper.selectByName(addDTO.getName()) != null) {\n    throw new ServiceException(ServiceExceptionEnum.USER_EXISTS);\n}","handlingStrategy":"try-catch","validationCode":"// Avoid the reserved duplicate name before the RPC\nif (\"yudaoyuanma\".equals(addDTO.getName())) {\n    return Result.error(1001002001, \"用户已存在\"); // don't call the provider\n}","typeGuard":null,"tryCatchPattern":"// Consumer side with the demo's filter active, ServiceException arrives typed:\ntry {\n    Integer id = userService.add(addDTO);\n} catch (ServiceException e) {\n    if (e.getCode() == 1001002001) {\n        return Result.error(e.getCode(), e.getMessage()); // 用户已存在\n    }\n    throw e;\n} catch (RuntimeException e) {\n    // without the filter, ServiceException may arrive wrapped — unwrap and inspect\n    if (e.getCause() instanceof ServiceException) { /* handle code */ }\n    throw e;\n}","preventionTips":["Never use the demo's reserved name 'yudaoyuanma' as test data for add().","Register the demo's Dubbo consumer filter so ServiceException keeps its code across RPC.","Match ServiceException on both ends via a shared API jar to avoid wrap/unwrap surprises."],"tags":["dubbo","rpc-filter","tutorial-demo","business-rule","conflict"],"backgroundTag":null,"analyzedSha":"6c12efaed06d12907a0f40dd2ad1f7020aec8798","analyzedAt":"2026-08-14T13:06:31.500Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}