error-handling
Consistent error handling with named exceptions and global handler. Use when implementing any method that can fail with a business reason.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Consistent error handling with named exceptions and global handler. Use when implementing any method that can fail with a business reason.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Team quality gate as executable instruction. Use when reviewing completed work before merge, or when processing PR review comments received from other reviewers.
Generate well-formed user stories from technical context. Use when generating or improving story descriptions for an issue tracker.
Type-safe validated configuration properties from application.yml. Use when adding any externalisable value.
Consistent, safe log statements with correct levels and no PII. Use when adding or reviewing log statements.
Team standard for improving existing code without changing behaviour. Use when refactoring code.
Team threat model as executable instruction. Use when checking code for security issues before merge.
| name | error-handling |
| description | Consistent error handling with named exceptions and global handler. Use when implementing any method that can fail with a business reason. |
When to load it: When implementing any service method that can fail with a meaningful business reason, or when adding exception handling to an existing feature.
Produces consistent, named exception classes and a global handler that translates domain failures to HTTP responses. Keeps error logic out of service code and ensures every failure category has an explicit type.
GlobalExceptionHandler translate to HTTP. Never catch-and-translate inside
service code.| Category | When to throw |
|---|---|
| Not found | Resource does not exist |
| Validation failure | Input does not meet business rules |
| External service failure | Downstream unavailable or returned unexpected response |
| Configuration error | Required config missing or invalid at runtime |
Named exception:
public class [Resource]NotFoundException extends RuntimeException {
public [Resource]NotFoundException([IdType] id) {
super("[Resource] not found: " + id);
}
}
Global handler:
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler([Resource]NotFoundException.class)
public ResponseEntity<ErrorResponse> handle([Resource]NotFoundException ex) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(new ErrorResponse(ex.getMessage()));
}
}
RuntimeException directly — always a named exception class