chore(errors): create deprecation alias pointing to pkg/log
Makes pkg/errors a thin compatibility layer that re-exports from pkg/log. All error handling functions now have canonical implementations in pkg/log. Migration guide in package documentation: - errors.Error -> log.Err - errors.E -> log.E - errors.Code -> log.NewCode - errors.New -> log.NewError Fixes behavior consistency: - E(op, msg, nil) now creates an error (for errors without cause) - Wrap(nil, op, msg) returns nil (for conditional wrapping) - WrapCode returns nil only when both err is nil AND code is empty Closes #128 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
a15c7e6441
commit
73b8873aae
3 changed files with 76 additions and 90 deletions
|
|
@ -1,151 +1,128 @@
|
|||
// Package errors provides structured error handling for Core applications.
|
||||
//
|
||||
// Errors include operational context (what was being done) and support
|
||||
// error wrapping for debugging while keeping user-facing messages clean:
|
||||
// Deprecated: Use pkg/log instead. This package is maintained for backward
|
||||
// compatibility and will be removed in a future version. All error handling
|
||||
// functions are now available in pkg/log:
|
||||
//
|
||||
// err := errors.E("user.Create", "email already exists", nil)
|
||||
// err := errors.Wrap(dbErr, "user.Create", "failed to save user")
|
||||
// // Instead of:
|
||||
// import "github.com/host-uk/core/pkg/errors"
|
||||
// err := errors.E("op", "msg", cause)
|
||||
//
|
||||
// // Check error types
|
||||
// if errors.Is(err, sql.ErrNoRows) { ... }
|
||||
// // Use:
|
||||
// import "github.com/host-uk/core/pkg/log"
|
||||
// err := log.E("op", "msg", cause)
|
||||
//
|
||||
// // Extract operation
|
||||
// var e *errors.Error
|
||||
// if errors.As(err, &e) {
|
||||
// fmt.Println("Operation:", e.Op)
|
||||
// }
|
||||
// Migration guide:
|
||||
// - errors.Error -> log.Err
|
||||
// - errors.E -> log.E
|
||||
// - errors.Wrap -> log.Wrap
|
||||
// - errors.WrapCode -> log.WrapCode
|
||||
// - errors.Code -> log.NewCode
|
||||
// - errors.New -> log.NewError
|
||||
// - errors.Is -> log.Is
|
||||
// - errors.As -> log.As
|
||||
// - errors.Join -> log.Join
|
||||
// - errors.Op -> log.Op
|
||||
// - errors.ErrCode -> log.ErrCode
|
||||
// - errors.Message -> log.Message
|
||||
// - errors.Root -> log.Root
|
||||
package errors
|
||||
|
||||
import (
|
||||
stderrors "errors"
|
||||
"fmt"
|
||||
"github.com/host-uk/core/pkg/log"
|
||||
)
|
||||
|
||||
// Error represents a structured error with operational context.
|
||||
type Error struct {
|
||||
Op string // Operation being performed (e.g., "user.Create")
|
||||
Msg string // Human-readable message
|
||||
Err error // Underlying error (optional)
|
||||
Code string // Error code for i18n/categorisation (optional)
|
||||
}
|
||||
//
|
||||
// Deprecated: Use log.Err instead.
|
||||
type Error = log.Err
|
||||
|
||||
// E creates a new Error with operation context.
|
||||
//
|
||||
// err := errors.E("config.Load", "file not found", os.ErrNotExist)
|
||||
// err := errors.E("api.Call", "rate limited", nil)
|
||||
// Deprecated: Use log.E instead.
|
||||
func E(op, msg string, err error) error {
|
||||
return &Error{Op: op, Msg: msg, Err: err}
|
||||
return log.E(op, msg, err)
|
||||
}
|
||||
|
||||
// Wrap wraps an error with operation context.
|
||||
// Returns nil if err is nil.
|
||||
//
|
||||
// return errors.Wrap(err, "db.Query", "failed to fetch user")
|
||||
// Deprecated: Use log.Wrap instead.
|
||||
func Wrap(err error, op, msg string) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return &Error{Op: op, Msg: msg, Err: err}
|
||||
return log.Wrap(err, op, msg)
|
||||
}
|
||||
|
||||
// WrapCode wraps an error with operation context and an error code.
|
||||
//
|
||||
// return errors.WrapCode(err, "ERR_NOT_FOUND", "user.Get", "user not found")
|
||||
// Deprecated: Use log.WrapCode instead.
|
||||
func WrapCode(err error, code, op, msg string) error {
|
||||
if err == nil && code == "" {
|
||||
return nil
|
||||
}
|
||||
return &Error{Op: op, Msg: msg, Err: err, Code: code}
|
||||
return log.WrapCode(err, code, op, msg)
|
||||
}
|
||||
|
||||
// Code creates an error with just a code and message.
|
||||
//
|
||||
// return errors.Code("ERR_VALIDATION", "invalid email format")
|
||||
// Deprecated: Use log.NewCode instead.
|
||||
func Code(code, msg string) error {
|
||||
return &Error{Code: code, Msg: msg}
|
||||
}
|
||||
|
||||
// Error returns the error message.
|
||||
func (e *Error) Error() string {
|
||||
if e.Op != "" && e.Err != nil {
|
||||
return fmt.Sprintf("%s: %s: %v", e.Op, e.Msg, e.Err)
|
||||
}
|
||||
if e.Op != "" {
|
||||
return fmt.Sprintf("%s: %s", e.Op, e.Msg)
|
||||
}
|
||||
if e.Err != nil {
|
||||
return fmt.Sprintf("%s: %v", e.Msg, e.Err)
|
||||
}
|
||||
return e.Msg
|
||||
}
|
||||
|
||||
// Unwrap returns the underlying error.
|
||||
func (e *Error) Unwrap() error {
|
||||
return e.Err
|
||||
return log.NewCode(code, msg)
|
||||
}
|
||||
|
||||
// --- Standard library wrappers ---
|
||||
|
||||
// Is reports whether any error in err's tree matches target.
|
||||
//
|
||||
// Deprecated: Use log.Is instead.
|
||||
func Is(err, target error) bool {
|
||||
return stderrors.Is(err, target)
|
||||
return log.Is(err, target)
|
||||
}
|
||||
|
||||
// As finds the first error in err's tree that matches target.
|
||||
//
|
||||
// Deprecated: Use log.As instead.
|
||||
func As(err error, target any) bool {
|
||||
return stderrors.As(err, target)
|
||||
return log.As(err, target)
|
||||
}
|
||||
|
||||
// New returns an error with the given text.
|
||||
//
|
||||
// Deprecated: Use log.NewError instead.
|
||||
func New(text string) error {
|
||||
return stderrors.New(text)
|
||||
return log.NewError(text)
|
||||
}
|
||||
|
||||
// Join returns an error that wraps the given errors.
|
||||
//
|
||||
// Deprecated: Use log.Join instead.
|
||||
func Join(errs ...error) error {
|
||||
return stderrors.Join(errs...)
|
||||
return log.Join(errs...)
|
||||
}
|
||||
|
||||
// --- Helper functions ---
|
||||
|
||||
// Op extracts the operation from an error, or empty string if not an Error.
|
||||
//
|
||||
// Deprecated: Use log.Op instead.
|
||||
func Op(err error) string {
|
||||
var e *Error
|
||||
if As(err, &e) {
|
||||
return e.Op
|
||||
}
|
||||
return ""
|
||||
return log.Op(err)
|
||||
}
|
||||
|
||||
// ErrCode extracts the error code, or empty string if not set.
|
||||
//
|
||||
// Deprecated: Use log.ErrCode instead.
|
||||
func ErrCode(err error) string {
|
||||
var e *Error
|
||||
if As(err, &e) {
|
||||
return e.Code
|
||||
}
|
||||
return ""
|
||||
return log.ErrCode(err)
|
||||
}
|
||||
|
||||
// Message extracts the message from an error.
|
||||
// For Error types, returns Msg; otherwise returns err.Error().
|
||||
//
|
||||
// Deprecated: Use log.Message instead.
|
||||
func Message(err error) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
var e *Error
|
||||
if As(err, &e) {
|
||||
return e.Msg
|
||||
}
|
||||
return err.Error()
|
||||
return log.Message(err)
|
||||
}
|
||||
|
||||
// Root returns the deepest error in the chain.
|
||||
//
|
||||
// Deprecated: Use log.Root instead.
|
||||
func Root(err error) error {
|
||||
for {
|
||||
unwrapped := stderrors.Unwrap(err)
|
||||
if unwrapped == nil {
|
||||
return err
|
||||
}
|
||||
err = unwrapped
|
||||
}
|
||||
return log.Root(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,36 +41,38 @@ func (e *Err) Unwrap() error {
|
|||
// --- Error Creation Functions ---
|
||||
|
||||
// E creates a new Err with operation context.
|
||||
// If err is nil, returns nil to support conditional wrapping.
|
||||
// The underlying error can be nil for creating errors without a cause.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// return log.E("user.Save", "failed to save user", err)
|
||||
// return log.E("api.Call", "rate limited", nil) // No underlying cause
|
||||
func E(op, msg string, err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return &Err{Op: op, Msg: msg, Err: err}
|
||||
}
|
||||
|
||||
// Wrap wraps an error with operation context.
|
||||
// Alias for E() for semantic clarity when wrapping existing errors.
|
||||
// Returns nil if err is nil, to support conditional wrapping.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// return log.Wrap(err, "db.Query", "database query failed")
|
||||
func Wrap(err error, op, msg string) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return E(op, msg, err)
|
||||
}
|
||||
|
||||
// WrapCode wraps an error with operation context and error code.
|
||||
// Returns nil only if both err is nil AND code is empty.
|
||||
// Useful for API errors that need machine-readable codes.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// return log.WrapCode(err, "VALIDATION_ERROR", "user.Validate", "invalid email")
|
||||
func WrapCode(err error, code, op, msg string) error {
|
||||
if err == nil {
|
||||
if err == nil && code == "" {
|
||||
return nil
|
||||
}
|
||||
return &Err{Op: op, Msg: msg, Err: err, Code: code}
|
||||
|
|
|
|||
|
|
@ -52,9 +52,10 @@ func TestE_Good(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestE_Good_NilError(t *testing.T) {
|
||||
// Should return nil when wrapping nil
|
||||
// E creates an error even with nil underlying - useful for errors without causes
|
||||
err := E("op.Name", "message", nil)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, "op.Name: message", err.Error())
|
||||
}
|
||||
|
||||
func TestWrap_Good(t *testing.T) {
|
||||
|
|
@ -80,7 +81,13 @@ func TestWrapCode_Good(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestWrapCode_Good_NilError(t *testing.T) {
|
||||
// WrapCode with nil error but with code still creates an error
|
||||
err := WrapCode(nil, "CODE", "op", "msg")
|
||||
assert.NotNil(t, err)
|
||||
assert.Contains(t, err.Error(), "[CODE]")
|
||||
|
||||
// Only returns nil when both error and code are empty
|
||||
err = WrapCode(nil, "", "op", "msg")
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue