go-create-error
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGo Error Generator
Go错误生成器
Generate typed custom errors for module-level packages.
errs为模块级包生成带类型的自定义错误。
errsPattern
存放规则
Place errors in:
internal/modules/<module>/errs/errs.goEach module error file follows:
package errs- Imports:
net/httpgithub.com/cristiano-pacheco/bricks/pkg/errs
- block with exported error variables
var (...) - Error creation via:
errs.New("<MODULE>_<NN>", "<Message>", http.<Status>, nil)
错误文件存放路径:
internal/modules/<module>/errs/errs.go每个模块的错误文件需遵循以下规则:
- 声明包名为
errs - 导入依赖:
net/httpgithub.com/cristiano-pacheco/bricks/pkg/errs
- 使用块定义导出的错误变量
var (...) - 通过以下方式创建错误:
errs.New("<MODULE>_<NN>", "<Message>", http.<Status>, nil)
Example Structure
示例结构
go
package errs
import (
"net/http"
"github.com/cristiano-pacheco/bricks/pkg/errs"
)
var (
// ErrInvalidContactType is returned when contact type is invalid.
ErrInvalidContactType = errs.New("MONITOR_01", "Invalid contact type", http.StatusBadRequest, nil)
// ErrContactNameAlreadyInUse is returned when contact name already exists.
ErrContactNameAlreadyInUse = errs.New("MONITOR_02", "Contact name already in use", http.StatusConflict, nil)
)go
package errs
import (
"net/http"
"github.com/cristiano-pacheco/bricks/pkg/errs"
)
var (
// ErrInvalidContactType 当联系类型无效时返回。
ErrInvalidContactType = errs.New("MONITOR_01", "Invalid contact type", http.StatusBadRequest, nil)
// ErrContactNameAlreadyInUse 当联系名称已被使用时返回。
ErrContactNameAlreadyInUse = errs.New("MONITOR_02", "Contact name already in use", http.StatusConflict, nil)
)Generation Steps
生成步骤
-
Identify error details:
- Target module (,
identity, ...)monitor - Error variable name ()
ErrInvalidContactType - Human message ()
"Invalid contact type" - HTTP status ()
http.StatusBadRequest
- Target module (
-
Find the next code:
- Open
internal/modules/<module>/errs/errs.go - Extract existing codes for that module prefix
- Allocate the next available code while preserving format:
- ,
IDENTITY_01, ...IDENTITY_02 - ,
MONITOR_01, ...MONITOR_02
- Keep code uniqueness inside the module
- Open
-
Add the new error:
- Insert into the module block
var (...) - Keep alphabetical or domain-group ordering used by the file
- Prefer a short doc comment for consistency where comments are already used
- Insert into the module
-
Validate usage path:
- Ensure new/updated usecases, validators, handlers, or enum constructors return the new typed error
- Do not return raw from business flows when a typed module error exists
errors.New(...)
-
确定错误详情:
- 目标模块(、
identity等)monitor - 错误变量名(如)
ErrInvalidContactType - 用户友好提示信息(如)
"无效的联系类型" - HTTP状态码(如)
http.StatusBadRequest
- 目标模块(
-
确定下一个错误码:
- 打开文件
internal/modules/<module>/errs/errs.go - 提取该模块前缀下已有的错误码
- 按照现有格式分配下一个可用的错误码:
- ,
IDENTITY_01, ...IDENTITY_02 - ,
MONITOR_01, ...MONITOR_02
- 确保同一模块内的错误码唯一
- 打开
-
添加新错误:
- 将错误变量插入到模块的块中
var (...) - 遵循文件已有的按字母顺序或领域分组的排序规则
- 如果文件中已有注释,建议添加简短的文档注释以保持一致性
- 将错误变量插入到模块的
-
验证使用路径:
- 确保新增或更新的用例、验证器、处理器或枚举构造函数返回新的带类型错误
- 当存在对应的模块带类型错误时,业务流程中不要返回原生的错误
errors.New(...)
Naming Conventions
命名规范
- Variable: + clear domain phrase in PascalCase
Err- Example: ,
ErrInvalidUserStatusErrOAuthStateNotFound
- Example:
- Code:
<MODULE>_<NN>- ,
IDENTITY_54MONITOR_05
- Message:
- Short, user-safe, sentence case
- Start with uppercase letter
- Avoid punctuation unless needed
- HTTP status:
- Validation errors:
http.StatusBadRequest - Auth failures: /
http.StatusUnauthorizedhttp.StatusForbidden - Missing resources:
http.StatusNotFound - Conflicts:
http.StatusConflict - Rate limit:
http.StatusTooManyRequests - Infra/internal failures: /
http.StatusInternalServerErrorhttp.StatusServiceUnavailable
- Validation errors:
- 变量命名:开头,后跟清晰的领域相关短语,采用大驼峰命名法
Err- 示例:,
ErrInvalidUserStatusErrOAuthStateNotFound
- 示例:
- 错误码:格式为
<MODULE>_<NN>- 示例:,
IDENTITY_54MONITOR_05
- 示例:
- 错误信息:
- 简洁、对用户友好,首字母大写的句子格式
- 首字母大写
- 除非必要,否则不要使用标点符号
- HTTP状态码:
- 验证错误:
http.StatusBadRequest - 认证失败:/
http.StatusUnauthorizedhttp.StatusForbidden - 资源不存在:
http.StatusNotFound - 资源冲突:
http.StatusConflict - 请求限流:
http.StatusTooManyRequests - 基础设施/内部错误:/
http.StatusInternalServerErrorhttp.StatusServiceUnavailable
- 验证错误:
Implementation Checklist
实施检查清单
- Open target
internal/modules/<module>/errs/errs.go - Compute next unique module code
- Add exported variable with
Err...errs.New(...) - Match existing ordering/grouping style
- Ensure message and status align with domain behavior
- Replace raw error returns in calling code with typed where applicable
errs.Err...
- 打开目标文件
internal/modules/<module>/errs/errs.go - 计算下一个唯一的模块错误码
- 使用添加导出的
errs.New(...)变量Err... - 与现有排序/分组风格保持一致
- 确保错误信息和状态码与领域行为匹配
- 在适用的场景下,将调用代码中返回的原生错误替换为带类型的
errs.Err...
Usage Pattern
使用示例
go
if input.RedirectURI == "" {
return errs.ErrInvalidRedirectURI
}
if user == nil {
return errs.ErrUserNotFound
}go
if input.RedirectURI == "" {
return errs.ErrInvalidRedirectURI
}
if user == nil {
return errs.ErrUserNotFound
}Critical Rules
核心规则
- Do not create a new error package; use module-local
internal/modules/<module>/errs - Do not duplicate codes within the same module
- Do not return persistence or infrastructure-specific raw errors to transport when a typed domain error exists
- Keep error messages stable once exposed, unless migration/compatibility impact is accepted
- 不要创建新的错误包,使用模块本地的包
internal/modules/<module>/errs - 同一模块内不要重复使用错误码
- 当存在对应的领域带类型错误时,不要将持久层或基础设施相关的原生错误返回给传输层
- 错误信息一旦对外暴露,需保持稳定,除非已接受迁移/兼容性影响