Documentation
¶
Overview ¶
Example (ErrorHandlingPattern) ¶
Example_errorHandlingPattern demonstrates a complete error handling pattern.
package main
import (
"fmt"
"os"
"github.com/snowdreamtech/unirtm/internal/pkg/errors"
)
func main() {
// Simulate a function that might fail
findUser := func(id int) error {
// Simulate not found
return errors.ErrNotFound
}
// Call the function
err := findUser(123)
if err != nil {
// Wrap with context
err = errors.Wrap(err, "find user %d", 123)
// Categorize as user error
err = errors.NewUserError("user lookup failed", err)
// Check category and handle appropriately
if errors.IsUserError(err) {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
// In real code: os.Exit(errors.ExitCode(err))
}
}
}
Output:
Example (MultipleWrapping) ¶
Example_multipleWrapping demonstrates wrapping errors multiple times.
package main
import (
"fmt"
"github.com/snowdreamtech/unirtm/internal/pkg/errors"
)
func main() {
// Start with a base error
baseErr := errors.ErrNetworkFailure
// Wrap at different layers
layer1 := errors.Wrap(baseErr, "download artifact")
layer2 := errors.Wrap(layer1, "install tool node")
layer3 := errors.NewExternalError("installation failed", layer2)
fmt.Println(layer3)
}
Output: [external] installation failed: install tool node: download artifact: network failure
Index ¶
- Variables
- func ExitCode(err error) int
- func IsExternalError(err error) bool
- func IsSystemError(err error) bool
- func IsUserError(err error) bool
- func NewExternalError(message string, err error) error
- func NewSystemError(message string, err error) error
- func NewUserError(message string, err error) error
- func Wrap(err error, format string, args ...interface{}) error
- type CategorizedError
- type ErrorCategory
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound indicates a resource was not found. ErrNotFound = errors.New("not found") // ErrAlreadyExists indicates a resource already exists. ErrAlreadyExists = errors.New("already exists") // ErrInvalidConfig indicates invalid configuration. ErrInvalidConfig = errors.New("invalid configuration") // ErrNetworkFailure indicates a network operation failed. ErrNetworkFailure = errors.New("network failure") // ErrChecksumMismatch indicates checksum verification failed. ErrChecksumMismatch = errors.New("checksum mismatch") // ErrTransactionFailed indicates a transaction failed. ErrTransactionFailed = errors.New("transaction failed") )
Sentinel errors for common error conditions. These errors can be used with errors.Is() for error checking.
Functions ¶
func ExitCode ¶
ExitCode returns the appropriate exit code for an error based on its category. User errors: 1, System errors: 2, External errors: 3, Unknown: 1
Example ¶
ExampleExitCode demonstrates getting the exit code for an error.
package main
import (
"fmt"
"github.com/snowdreamtech/unirtm/internal/pkg/errors"
)
func main() {
userErr := errors.NewUserError("test", nil)
systemErr := errors.NewSystemError("test", nil)
externalErr := errors.NewExternalError("test", nil)
fmt.Println("User error exit code:", errors.ExitCode(userErr))
fmt.Println("System error exit code:", errors.ExitCode(systemErr))
fmt.Println("External error exit code:", errors.ExitCode(externalErr))
}
Output: User error exit code: 1 System error exit code: 2 External error exit code: 3
func IsExternalError ¶
IsExternalError checks if an error is an external error.
func IsSystemError ¶
IsSystemError checks if an error is a system error.
func IsUserError ¶
IsUserError checks if an error is a user error.
Example ¶
ExampleIsUserError demonstrates checking if an error is a user error.
package main
import (
"fmt"
"github.com/snowdreamtech/unirtm/internal/pkg/errors"
)
func main() {
err := errors.NewUserError("test", nil)
fmt.Println(errors.IsUserError(err))
}
Output: true
func NewExternalError ¶
NewExternalError creates a new external error with the given message and optional wrapped error. External errors indicate network failures, backend API errors, or download failures.
Example ¶
ExampleNewExternalError demonstrates creating an external error.
package main
import (
"fmt"
"github.com/snowdreamtech/unirtm/internal/pkg/errors"
)
func main() {
err := errors.NewExternalError("API request failed", errors.ErrNetworkFailure)
fmt.Println(err)
}
Output: [external] API request failed: network failure
func NewSystemError ¶
NewSystemError creates a new system error with the given message and optional wrapped error. System errors indicate disk full, permission denied, or database corruption.
Example ¶
ExampleNewSystemError demonstrates creating a system error.
package main
import (
"fmt"
"github.com/snowdreamtech/unirtm/internal/pkg/errors"
)
func main() {
err := errors.NewSystemError("database connection failed", errors.ErrTransactionFailed)
fmt.Println(err)
}
Output: [system] database connection failed: transaction failed
func NewUserError ¶
NewUserError creates a new user error with the given message and optional wrapped error. User errors indicate invalid input, configuration errors, or version not found.
Example ¶
ExampleNewUserError demonstrates creating a user error.
package main
import (
"fmt"
"github.com/snowdreamtech/unirtm/internal/pkg/errors"
)
func main() {
err := errors.NewUserError("invalid version format", errors.ErrInvalidConfig)
fmt.Println(err)
}
Output: [user] invalid version format: invalid configuration
func Wrap ¶
Wrap wraps an error with additional context using fmt.Errorf with %w. This preserves the error chain for errors.Is() and errors.As().
Example:
user, err := repo.FindByID(ctx, id)
if err != nil {
return nil, Wrap(err, "find user %d", id)
}
Example ¶
ExampleWrap demonstrates wrapping an error with context.
package main
import (
"fmt"
"github.com/snowdreamtech/unirtm/internal/pkg/errors"
)
func main() {
baseErr := errors.ErrNotFound
wrappedErr := errors.Wrap(baseErr, "find user %d", 123)
fmt.Println(wrappedErr)
}
Output: find user 123: not found
Types ¶
type CategorizedError ¶
type CategorizedError struct {
// contains filtered or unexported fields
}
CategorizedError wraps an error with a category classification.
func (*CategorizedError) Category ¶
func (e *CategorizedError) Category() ErrorCategory
Category returns the error category.
func (*CategorizedError) Context ¶
func (e *CategorizedError) Context() string
Context returns the error context.
func (*CategorizedError) Error ¶
func (e *CategorizedError) Error() string
Error implements the error interface.
func (*CategorizedError) Unwrap ¶
func (e *CategorizedError) Unwrap() error
Unwrap returns the wrapped error for errors.Is() and errors.As() support.
type ErrorCategory ¶
type ErrorCategory int
ErrorCategory represents the classification of an error.
const ( // CategoryUnknown represents an unclassified error. CategoryUnknown ErrorCategory = iota // CategoryUser represents user errors (invalid input, configuration errors, version not found). // These errors should return descriptive messages and suggest corrective actions. // Exit code: 1 CategoryUser // CategorySystem represents system errors (disk full, permission denied, database corruption). // These errors should log full context and return generic user-safe messages. // Exit code: 2 CategorySystem // CategoryExternal represents external errors (network failures, backend API errors, download failures). // These errors should implement retry logic and return wrapped errors with context. // Exit code: 3 CategoryExternal )
func GetCategory ¶
func GetCategory(err error) ErrorCategory
GetCategory returns the category of an error. If the error is not a CategorizedError, it returns CategoryUnknown.
Example ¶
ExampleGetCategory demonstrates getting the category of an error.
package main
import (
"fmt"
"github.com/snowdreamtech/unirtm/internal/pkg/errors"
)
func main() {
err := errors.NewUserError("test", nil)
category := errors.GetCategory(err)
fmt.Println(category)
}
Output: user
func (ErrorCategory) String ¶
func (c ErrorCategory) String() string
String returns the string representation of the error category.