yudaocode/SpringBoot-Labs · warning · ServiceException

1001002001

1001002001

Error message

用户已存在

What it means

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.

Source

Thrown at 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:23

import cn.iocoder.springcloudalibaba.labx7.core.ServiceExceptionEnum;
import cn.iocoder.springcloudalibaba.labx7.dto.UserAddDTO;
import cn.iocoder.springcloudalibaba.labx7.dto.UserDTO;

@org.apache.dubbo.config.annotation.Service(protocol = "dubbo", version = "1.0.0", validation = "true", filter = "-exception")
public class UserServiceImpl implements UserService {

    @Override
    public UserDTO get(Integer id) {
        return new UserDTO().setId(id)
                .setName("没有昵称:" + id)
                .setGender(id % 2 + 1); // 1 - 男;2 - 女
    }

    @Override
    public Integer add(UserAddDTO addDTO) {
        // 【额外添加】这里,模拟用户已经存在的情况
        if ("yudaoyuanma".equals(addDTO.getName())) {
            throw new ServiceException(ServiceExceptionEnum.USER_EXISTS);
        }
        return (int) (System.currentTimeMillis() / 1000); // 嘿嘿,随便返回一个 id
    }

}

View on GitHub (pinned to 6c12efaed0)

Solutions

  1. Call add with any name other than 'yudaoyuanma' to get the success path.
  2. On the consumer, ensure the demo's Dubbo filter is loaded (scan/activate it) so ServiceException is deserialized with code 1001002001 intact.
  3. If you extended this into real code, replace the hard-coded check with a real uniqueness check and return the same ServiceException shape.

Example fix

// before
if ("yudaoyuanma".equals(addDTO.getName())) {
    throw new ServiceException(ServiceExceptionEnum.USER_EXISTS);
}

// after — real duplicate check keeping the same error contract
if (userMapper.selectByName(addDTO.getName()) != null) {
    throw new ServiceException(ServiceExceptionEnum.USER_EXISTS);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Avoid the reserved duplicate name before the RPC
if ("yudaoyuanma".equals(addDTO.getName())) {
    return Result.error(1001002001, "用户已存在"); // don't call the provider
}

Try / catch

// Consumer side with the demo's filter active, ServiceException arrives typed:
try {
    Integer id = userService.add(addDTO);
} catch (ServiceException e) {
    if (e.getCode() == 1001002001) {
        return Result.error(e.getCode(), e.getMessage()); // 用户已存在
    }
    throw e;
} catch (RuntimeException e) {
    // without the filter, ServiceException may arrive wrapped — unwrap and inspect
    if (e.getCause() instanceof ServiceException) { /* handle code */ }
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of yudaocode/SpringBoot-Labs@6c12efaed0 (2026-08-14). Data as JSON: /api/errors/af5c9c0a7e8a4abd. Report an issue: GitHub.