Documentation
¶
Overview ¶
Copyright (c) 2025 Guilherme Silva Sousa Licensed under the MIT License See LICENSE file in the project root for full license information.
Copyright (c) 2025 Guilherme Silva Sousa Licensed under the MIT License See LICENSE file in the project root for full license information. Package errors provides comprehensive error handling for CCMD.
Copyright (c) 2025 Guilherme Silva Sousa Licensed under the MIT License See LICENSE file in the project root for full license information.
Index ¶
- Variables
- func As(err error, target interface{}) bool
- func Handle(err error) bool
- func HandleFatal(err error)
- func Is(err, target error) bool
- func IsAlreadyExists(err error) bool
- func IsCode(err error, code ErrorCode) bool
- func IsGitError(err error) bool
- func IsNetworkError(err error) bool
- func IsNotFound(err error) bool
- func IsPermissionDenied(err error) bool
- func IsValidationError(err error) bool
- func NewMulti(errs ...error) error
- func RecoverPanic(name string)
- type CommandRunner
- type Error
- type ErrorCode
- type Handler
- type MultiError
- type OutputFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultHandler = NewHandler(nil)
DefaultHandler is the default error handler instance
Functions ¶
func HandleFatal ¶
func HandleFatal(err error)
HandleFatal is a convenience function using the default handler
func IsAlreadyExists ¶
IsAlreadyExists checks if an error is an already exists error
func IsNetworkError ¶
IsNetworkError checks if an error is network related
func IsNotFound ¶
IsNotFound checks if an error is a not found error
Example ¶
package main
import (
"fmt"
"github.com/gifflet/ccmd/pkg/errors"
)
func main() {
err1 := errors.New(errors.CodeNotFound, "not found")
err2 := errors.New(errors.CodeCommandNotFound, "command not found")
err3 := errors.New(errors.CodeInternal, "internal error")
fmt.Println(errors.IsNotFound(err1)) // true
fmt.Println(errors.IsNotFound(err2)) // true
fmt.Println(errors.IsNotFound(err3)) // false
}
Output: true true false
func IsPermissionDenied ¶
IsPermissionDenied checks if an error is a permission denied error
func IsValidationError ¶
IsValidationError checks if an error is a validation error
func RecoverPanic ¶
func RecoverPanic(name string)
RecoverPanic recovers from panics and converts them to errors
Types ¶
type CommandRunner ¶
CommandRunner wraps a cobra command with error handling
func WrapCommand ¶
func WrapCommand(name string, runner CommandRunner) CommandRunner
WrapCommand wraps a command runner with error handling and logging
type Error ¶
Error represents a structured error with code, message and context
Example (Basic) ¶
package main
import (
"fmt"
"github.com/gifflet/ccmd/pkg/errors"
)
func main() {
// Create a simple error
err := errors.New(errors.CodeNotFound, "command not found")
fmt.Println(err)
}
Output: [NOT_FOUND] command not found
Example (WithDetails) ¶
package main
import (
"fmt"
"github.com/gifflet/ccmd/pkg/errors"
)
func main() {
// Create an error with details
err := errors.New(errors.CodeConfigInvalid, "invalid configuration").
WithDetail("file", "ccmd.yaml").
WithDetail("line", 42)
// The error includes the code and message
fmt.Println(err)
}
Output: [CONFIG_INVALID] invalid configuration
func Wrap ¶
Wrap wraps an existing error with additional context
Example ¶
package main
import (
"fmt"
"github.com/gifflet/ccmd/pkg/errors"
)
func main() {
// Simulate an underlying error
originalErr := fmt.Errorf("connection timeout")
// Wrap it with context
err := errors.Wrap(originalErr, errors.CodeNetworkTimeout, "failed to fetch repository")
fmt.Println(err)
}
Output: [NETWORK_TIMEOUT] failed to fetch repository: connection timeout
func (*Error) WithDetail ¶
WithDetail adds a detail to the error
func (*Error) WithDetails ¶
WithDetails adds multiple details to the error
type ErrorCode ¶
type ErrorCode string
ErrorCode represents a specific error type
const ( // CodeUnknown represents an unknown error CodeUnknown ErrorCode = "UNKNOWN" // CodeInternal represents an internal error CodeInternal ErrorCode = "INTERNAL" // CodeInvalidArgument represents an invalid argument error CodeInvalidArgument ErrorCode = "INVALID_ARGUMENT" // CodeNotFound represents a not found error CodeNotFound ErrorCode = "NOT_FOUND" // CodeAlreadyExists represents an already exists error CodeAlreadyExists ErrorCode = "ALREADY_EXISTS" // CodePermissionDenied represents a permission denied error CodePermissionDenied ErrorCode = "PERMISSION_DENIED" // CodeGitClone represents a git clone error CodeGitClone ErrorCode = "GIT_CLONE" // CodeGitInvalidRepo represents an invalid git repository error CodeGitInvalidRepo ErrorCode = "GIT_INVALID_REPO" // CodeGitAuth represents a git authentication error CodeGitAuth ErrorCode = "GIT_AUTH" // CodeGitNotFound represents a git not found error CodeGitNotFound ErrorCode = "GIT_NOT_FOUND" // CodeCommandNotFound represents a command not found error CodeCommandNotFound ErrorCode = "COMMAND_NOT_FOUND" // CodeCommandExists represents a command already exists error CodeCommandExists ErrorCode = "COMMAND_EXISTS" // CodeCommandInvalid represents an invalid command error CodeCommandInvalid ErrorCode = "COMMAND_INVALID" // CodeCommandExecute represents a command execution error CodeCommandExecute ErrorCode = "COMMAND_EXECUTE" // CodeConfigInvalid represents an invalid configuration error CodeConfigInvalid ErrorCode = "CONFIG_INVALID" // CodeConfigNotFound represents a configuration not found error CodeConfigNotFound ErrorCode = "CONFIG_NOT_FOUND" // CodeConfigParse represents a configuration parse error CodeConfigParse ErrorCode = "CONFIG_PARSE" // CodeFileNotFound represents a file not found error CodeFileNotFound ErrorCode = "FILE_NOT_FOUND" // CodeFileExists represents a file already exists error CodeFileExists ErrorCode = "FILE_EXISTS" // CodeFilePermission represents a file permission error CodeFilePermission ErrorCode = "FILE_PERMISSION" // CodeFileIO represents a file I/O error CodeFileIO ErrorCode = "FILE_IO" // CodeValidationFailed represents a validation failure CodeValidationFailed ErrorCode = "VALIDATION_FAILED" // CodeValidationSchema represents a schema validation error CodeValidationSchema ErrorCode = "VALIDATION_SCHEMA" // CodeNetworkTimeout represents a network timeout error CodeNetworkTimeout ErrorCode = "NETWORK_TIMEOUT" CodeNetworkUnavailable ErrorCode = "NETWORK_UNAVAILABLE" // CodeValidation represents a general validation error CodeValidation ErrorCode = "VALIDATION" // CodeLockConflict represents a lock conflict error CodeLockConflict ErrorCode = "LOCK_CONFLICT" // CodeTimeout represents a general timeout error CodeTimeout ErrorCode = "TIMEOUT" // CodePartialFailure represents a partial failure where some operations succeeded CodePartialFailure ErrorCode = "PARTIAL_FAILURE" // CodeNotImplemented represents a not implemented error CodeNotImplemented ErrorCode = "NOT_IMPLEMENTED" )
func GetCode ¶
GetCode extracts the error code from an error
Example ¶
package main
import (
"fmt"
"github.com/gifflet/ccmd/pkg/errors"
)
func main() {
// Create different types of errors
err1 := errors.New(errors.CodeNotFound, "not found")
err2 := fmt.Errorf("standard error")
// Get error codes
fmt.Println(errors.GetCode(err1))
fmt.Println(errors.GetCode(err2))
}
Output: NOT_FOUND UNKNOWN
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler provides centralized error handling with logging
Example ¶
package main
import (
"os"
"github.com/gifflet/ccmd/pkg/errors"
"github.com/gifflet/ccmd/pkg/logger"
)
func main() {
// Set up a test logger
log := logger.New(os.Stdout, logger.InfoLevel)
handler := errors.NewHandler(log)
// Create an error
err := errors.New(errors.CodeCommandNotFound, "command 'test' not found").
WithDetail("command", "test")
// Handle the error (this would normally print to stderr)
handler.Handle(err)
}
Output:
func NewHandler ¶
NewHandler creates a new error handler
func (*Handler) HandleFatal ¶
HandleFatal processes a fatal error and exits
func (*Handler) SetOutputFuncs ¶
func (h *Handler) SetOutputFuncs(printError, printWarning, printInfo OutputFunc)
SetOutputFuncs sets custom output functions
type OutputFunc ¶
type OutputFunc func(format string, args ...interface{})
OutputFunc is a function that outputs messages to the user