yudaocode/SpringBoot-Labs · error · ServiceException

1001002000

1001002000

Error message

用户不存在

What it means

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.

Source

Thrown at lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab23/springmvc/controller/UserController.java:81

    public UserVO get3(@RequestParam("id") Integer id) {
        // 查询并返回用户
        return new UserVO().setId(id).setUsername(UUID.randomUUID().toString());
    }

    /**
     * 测试抛出 NullPointerException 异常
     */
    @GetMapping("/exception-01")
    public UserVO exception01() {
        throw new NullPointerException("没有粗面鱼丸");
    }

    /**
     * 测试抛出 ServiceException 异常
     */
    @GetMapping("/exception-02")
    public UserVO exception02() {
        throw new ServiceException(ServiceExceptionEnum.USER_NOT_FOUND);
    }

    @GetMapping("/do_something")
    public void doSomething() {
        logger.info("[doSomething]");
    }

    @GetMapping("/current_user")
    public UserVO currentUser() {
        logger.info("[currentUser]");
        return new UserVO().setId(10).setUsername(UUID.randomUUID().toString());
    }

    @GetMapping("/exception-03")
    public void exception03() {
        logger.info("[exception03]");
        throw new ServiceException(ServiceExceptionEnum.USER_NOT_FOUND);
    }

View on GitHub (pinned to 6c12efaed0)

Solutions

  1. Treat this as the expected error contract: check CommonResult.code == 1001002000 in the caller instead of expecting an exception on the wire.
  2. Verify the GlobalExceptionHandler bean is registered (same component-scan package) so ServiceException maps to a CommonResult body rather than a 500.
  3. If testing with a real user lookup, use an id that exists, or first check existence via the repository before throwing.
  4. Keep ServiceExceptionEnum codes documented so clients can switch on codes like 1001002000.

Example fix

// before: caller lets business error propagate raw
UserVO user = userService.get(id); // may throw ServiceException(USER_NOT_FOUND)

// after: caller handles the unified result
try {
    UserVO user = userService.get(id);
} catch (ServiceException e) {
    if (ServiceExceptionEnum.USER_NOT_FOUND.getCode().equals(e.getCode())) {
        return ResponseEntity.notFound().build();
    }
    throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Before calling a real lookup, verify existence:
if (userRepository.findById(id).isEmpty()) {
    return ResponseEntity.notFound().build();
}

Try / catch

try {
    UserVO user = userService.get(id);
} catch (ServiceException e) {
    if (ServiceExceptionEnum.USER_NOT_FOUND.getCode().equals(e.getCode())) {
        // 404-style handling
    } else { throw e; }
}

Prevention

When it happens

Trigger: Calling GET /user/exception-02. The method body unconditionally throws new ServiceException(ServiceExceptionEnum.USER_NOT_FOUND).

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

Related errors


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