2026-01-30 22:02:40 +00:00
|
|
|
// 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:
|
|
|
|
|
//
|
|
|
|
|
// err := errors.E("user.Create", "email already exists", nil)
|
|
|
|
|
// err := errors.Wrap(dbErr, "user.Create", "failed to save user")
|
|
|
|
|
//
|
|
|
|
|
// // Check error types
|
|
|
|
|
// if errors.Is(err, sql.ErrNoRows) { ... }
|
|
|
|
|
//
|
|
|
|
|
// // Extract operation
|
|
|
|
|
// var e *errors.Error
|
|
|
|
|
// if errors.As(err, &e) {
|
|
|
|
|
// fmt.Println("Operation:", e.Op)
|
|
|
|
|
// }
|
refactor: remove GUI packages for CGO-free CLI
Move all Wails-dependent packages to core-gui repo:
- pkg/core, pkg/display, pkg/docs, pkg/help, pkg/ide
- pkg/runtime, pkg/webview, pkg/workspace, pkg/ws
- pkg/plugin, pkg/config, pkg/i18n, pkg/module
- pkg/crypt, pkg/io, pkg/process
Add pkg/errors with simple E() helper for error wrapping.
Update go.work to only include CLI-relevant packages.
CLI now builds with CGO_ENABLED=0 - no linker warnings.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 12:15:01 +00:00
|
|
|
package errors
|
|
|
|
|
|
2026-01-30 22:02:40 +00:00
|
|
|
import (
|
|
|
|
|
stderrors "errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
refactor: remove GUI packages for CGO-free CLI
Move all Wails-dependent packages to core-gui repo:
- pkg/core, pkg/display, pkg/docs, pkg/help, pkg/ide
- pkg/runtime, pkg/webview, pkg/workspace, pkg/ws
- pkg/plugin, pkg/config, pkg/i18n, pkg/module
- pkg/crypt, pkg/io, pkg/process
Add pkg/errors with simple E() helper for error wrapping.
Update go.work to only include CLI-relevant packages.
CLI now builds with CGO_ENABLED=0 - no linker warnings.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 12:15:01 +00:00
|
|
|
|
2026-01-30 22:02:40 +00:00
|
|
|
// Error represents a structured error with operational context.
|
refactor: remove GUI packages for CGO-free CLI
Move all Wails-dependent packages to core-gui repo:
- pkg/core, pkg/display, pkg/docs, pkg/help, pkg/ide
- pkg/runtime, pkg/webview, pkg/workspace, pkg/ws
- pkg/plugin, pkg/config, pkg/i18n, pkg/module
- pkg/crypt, pkg/io, pkg/process
Add pkg/errors with simple E() helper for error wrapping.
Update go.work to only include CLI-relevant packages.
CLI now builds with CGO_ENABLED=0 - no linker warnings.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 12:15:01 +00:00
|
|
|
type Error struct {
|
2026-01-30 22:02:40 +00:00
|
|
|
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)
|
refactor: remove GUI packages for CGO-free CLI
Move all Wails-dependent packages to core-gui repo:
- pkg/core, pkg/display, pkg/docs, pkg/help, pkg/ide
- pkg/runtime, pkg/webview, pkg/workspace, pkg/ws
- pkg/plugin, pkg/config, pkg/i18n, pkg/module
- pkg/crypt, pkg/io, pkg/process
Add pkg/errors with simple E() helper for error wrapping.
Update go.work to only include CLI-relevant packages.
CLI now builds with CGO_ENABLED=0 - no linker warnings.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 12:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// E creates a new Error with operation context.
|
2026-01-30 22:02:40 +00:00
|
|
|
//
|
|
|
|
|
// err := errors.E("config.Load", "file not found", os.ErrNotExist)
|
|
|
|
|
// err := errors.E("api.Call", "rate limited", nil)
|
refactor: remove GUI packages for CGO-free CLI
Move all Wails-dependent packages to core-gui repo:
- pkg/core, pkg/display, pkg/docs, pkg/help, pkg/ide
- pkg/runtime, pkg/webview, pkg/workspace, pkg/ws
- pkg/plugin, pkg/config, pkg/i18n, pkg/module
- pkg/crypt, pkg/io, pkg/process
Add pkg/errors with simple E() helper for error wrapping.
Update go.work to only include CLI-relevant packages.
CLI now builds with CGO_ENABLED=0 - no linker warnings.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 12:15:01 +00:00
|
|
|
func E(op, msg string, err error) error {
|
2026-01-30 22:02:40 +00:00
|
|
|
return &Error{Op: op, Msg: msg, Err: err}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wrap wraps an error with operation context.
|
|
|
|
|
// Returns nil if err is nil.
|
|
|
|
|
//
|
|
|
|
|
// return errors.Wrap(err, "db.Query", "failed to fetch user")
|
|
|
|
|
func Wrap(err error, op, msg string) error {
|
refactor: remove GUI packages for CGO-free CLI
Move all Wails-dependent packages to core-gui repo:
- pkg/core, pkg/display, pkg/docs, pkg/help, pkg/ide
- pkg/runtime, pkg/webview, pkg/workspace, pkg/ws
- pkg/plugin, pkg/config, pkg/i18n, pkg/module
- pkg/crypt, pkg/io, pkg/process
Add pkg/errors with simple E() helper for error wrapping.
Update go.work to only include CLI-relevant packages.
CLI now builds with CGO_ENABLED=0 - no linker warnings.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 12:15:01 +00:00
|
|
|
if err == nil {
|
2026-01-30 22:02:40 +00:00
|
|
|
return nil
|
refactor: remove GUI packages for CGO-free CLI
Move all Wails-dependent packages to core-gui repo:
- pkg/core, pkg/display, pkg/docs, pkg/help, pkg/ide
- pkg/runtime, pkg/webview, pkg/workspace, pkg/ws
- pkg/plugin, pkg/config, pkg/i18n, pkg/module
- pkg/crypt, pkg/io, pkg/process
Add pkg/errors with simple E() helper for error wrapping.
Update go.work to only include CLI-relevant packages.
CLI now builds with CGO_ENABLED=0 - no linker warnings.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 12:15:01 +00:00
|
|
|
}
|
|
|
|
|
return &Error{Op: op, Msg: msg, Err: err}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 22:02:40 +00:00
|
|
|
// WrapCode wraps an error with operation context and an error code.
|
|
|
|
|
//
|
|
|
|
|
// return errors.WrapCode(err, "ERR_NOT_FOUND", "user.Get", "user not found")
|
|
|
|
|
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}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Code creates an error with just a code and message.
|
|
|
|
|
//
|
|
|
|
|
// return errors.Code("ERR_VALIDATION", "invalid email format")
|
|
|
|
|
func Code(code, msg string) error {
|
|
|
|
|
return &Error{Code: code, Msg: msg}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Error returns the error message.
|
refactor: remove GUI packages for CGO-free CLI
Move all Wails-dependent packages to core-gui repo:
- pkg/core, pkg/display, pkg/docs, pkg/help, pkg/ide
- pkg/runtime, pkg/webview, pkg/workspace, pkg/ws
- pkg/plugin, pkg/config, pkg/i18n, pkg/module
- pkg/crypt, pkg/io, pkg/process
Add pkg/errors with simple E() helper for error wrapping.
Update go.work to only include CLI-relevant packages.
CLI now builds with CGO_ENABLED=0 - no linker warnings.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 12:15:01 +00:00
|
|
|
func (e *Error) Error() string {
|
2026-01-30 22:02:40 +00:00
|
|
|
if e.Op != "" && e.Err != nil {
|
refactor: remove GUI packages for CGO-free CLI
Move all Wails-dependent packages to core-gui repo:
- pkg/core, pkg/display, pkg/docs, pkg/help, pkg/ide
- pkg/runtime, pkg/webview, pkg/workspace, pkg/ws
- pkg/plugin, pkg/config, pkg/i18n, pkg/module
- pkg/crypt, pkg/io, pkg/process
Add pkg/errors with simple E() helper for error wrapping.
Update go.work to only include CLI-relevant packages.
CLI now builds with CGO_ENABLED=0 - no linker warnings.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 12:15:01 +00:00
|
|
|
return fmt.Sprintf("%s: %s: %v", e.Op, e.Msg, e.Err)
|
|
|
|
|
}
|
2026-01-30 22:02:40 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Standard library wrappers ---
|
|
|
|
|
|
|
|
|
|
// Is reports whether any error in err's tree matches target.
|
|
|
|
|
func Is(err, target error) bool {
|
|
|
|
|
return stderrors.Is(err, target)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// As finds the first error in err's tree that matches target.
|
|
|
|
|
func As(err error, target any) bool {
|
|
|
|
|
return stderrors.As(err, target)
|
refactor: remove GUI packages for CGO-free CLI
Move all Wails-dependent packages to core-gui repo:
- pkg/core, pkg/display, pkg/docs, pkg/help, pkg/ide
- pkg/runtime, pkg/webview, pkg/workspace, pkg/ws
- pkg/plugin, pkg/config, pkg/i18n, pkg/module
- pkg/crypt, pkg/io, pkg/process
Add pkg/errors with simple E() helper for error wrapping.
Update go.work to only include CLI-relevant packages.
CLI now builds with CGO_ENABLED=0 - no linker warnings.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 12:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-30 22:02:40 +00:00
|
|
|
// New returns an error with the given text.
|
|
|
|
|
func New(text string) error {
|
|
|
|
|
return stderrors.New(text)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Join returns an error that wraps the given errors.
|
|
|
|
|
func Join(errs ...error) error {
|
|
|
|
|
return stderrors.Join(errs...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Helper functions ---
|
|
|
|
|
|
|
|
|
|
// Op extracts the operation from an error, or empty string if not an Error.
|
|
|
|
|
func Op(err error) string {
|
|
|
|
|
var e *Error
|
|
|
|
|
if As(err, &e) {
|
|
|
|
|
return e.Op
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ErrCode extracts the error code, or empty string if not set.
|
|
|
|
|
func ErrCode(err error) string {
|
|
|
|
|
var e *Error
|
|
|
|
|
if As(err, &e) {
|
|
|
|
|
return e.Code
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Message extracts the message from an error.
|
|
|
|
|
// For Error types, returns Msg; otherwise returns err.Error().
|
|
|
|
|
func Message(err error) string {
|
|
|
|
|
if err == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
var e *Error
|
|
|
|
|
if As(err, &e) {
|
|
|
|
|
return e.Msg
|
|
|
|
|
}
|
|
|
|
|
return err.Error()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Root returns the deepest error in the chain.
|
|
|
|
|
func Root(err error) error {
|
|
|
|
|
for {
|
|
|
|
|
unwrapped := stderrors.Unwrap(err)
|
|
|
|
|
if unwrapped == nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = unwrapped
|
|
|
|
|
}
|
|
|
|
|
}
|