| name | go-error-handling |
| description | Manejo idiomático de errores en Go. Usar al diseñar errores para APIs, propagar errores entre capas, o crear tipos de error personalizados. |
| allowed-tools | Read, Grep, Glob |
Skill: go-error-handling
Manejo idiomático de errores en Go.
Cuándo usar este skill
- Al diseñar errores para una API
- Al propagar errores entre capas
- Al crear tipos de error personalizados
- Cuando se necesita contexto en errores
Patrones de Errores
Error Wrapping
if err != nil {
return fmt.Errorf("loading profile %s: %w", url, err)
}
if err != nil {
return err
}
Tipos de Error Personalizados
type EvalError struct {
Type ErrorType
Message string
Position Position
Underlying error
}
func (e *EvalError) Error() string {
return fmt.Sprintf("%s: %s", e.Type, e.Message)
}
func (e *EvalError) Unwrap() error {
return e.Underlying
}
Error Sentinels
var (
ErrNotFound = errors.New("not found")
ErrInvalidInput = errors.New("invalid input")
)
if errors.Is(err, ErrNotFound) {
}
Usar errors.Is y errors.As
if errors.Is(err, ErrNotFound) { }
var evalErr *EvalError
if errors.As(err, &evalErr) {
log.Printf("Error at line %d", evalErr.Position.Line)
}
Errores en el Validator FHIR
type Issue struct {
Severity Severity
Code string
Diagnostics string
Location []string
}
type ValidationResult struct {
Valid bool
Issues []Issue
}
Checklist
- [ ] ¿Los errores tienen contexto suficiente?
- [ ] ¿Se usa %w para wrapping?
- [ ] ¿Se implementa Unwrap() para chains?
- [ ] ¿Se usa errors.Is/As en lugar de comparación?