This directory contains tools for managing error codes in the CLI application.
The generate_error_codes.go script generates Go code for error types based on definitions in the error_codes.yaml file at the root of the project.
-
Error codes are defined in
error_codes.yamlin the following format:errors: - code: CLI-0001 message: Failed to delete agents - code: CLI-0002 message: Failed to create project
-
The code can be generated in two ways:
- Using
go generate ./...ormake go-generate(recommended) - Using the legacy method:
go run tools/generate_error_codes.goormake generate
- Using
Both methods will:
- Read the YAML file
- Generate appropriate variable names based on the error messages
- Create the
internal/errsystem/errorcodes.gofile with the defined error types
To add a new error code:
- Edit
error_codes.yamland add a new entry with a unique code and descriptive message - Run
go generate ./...ormake go-generateto update the Go code - Use the generated error type in your code
The generator creates variable names by:
- Removing common words like "failed", "unable", "to", etc.
- Converting the remaining words to CamelCase
- Prefixing with "Err"
For example:
- "Failed to delete agents" becomes
ErrDeleteAgents - "Unable to authenticate user" becomes
ErrAuthenticateUser
Use the generated error types with the errsystem.New function:
import "your-project/internal/errsystem"
func someFunction() error {
// ...
if err != nil {
return errsystem.New(errsystem.ErrDeleteAgents, err)
}
// ...
}