Documentation
¶
Overview ¶
Package errors provides domain-specific error types for Deputy.
This package enables robust error handling using errors.Is and errors.As, allowing callers to distinguish between different failure modes and handle them appropriately.
Sentinel Errors ¶
Common failure modes are represented as sentinel errors:
if errors.Is(err, deperrors.ErrNotFound) {
// Handle missing resource
}
if errors.Is(err, deperrors.ErrNetwork) {
// Handle network failure
}
Typed Errors ¶
Domain-specific errors carry additional context:
var policyErr *PolicyError
if errors.As(err, &policyErr) {
fmt.Printf("Policy %s failed at line %d\n", policyErr.PolicyName, policyErr.Line)
}
Error Suggestions ¶
Errors can carry remediation suggestions for display to users:
err := deperrors.Suggest(
errors.New("ANTHROPIC_API_KEY is not set"),
"Set the ANTHROPIC_API_KEY environment variable",
)
// Later, when displaying the error:
if suggestion := deperrors.GetSuggestion(err); suggestion != "" {
fmt.Printf("Suggestion: %s\n", suggestion)
}
Common suggestions are available via CommonSuggestions:
deperrors.SuggestFor("network") // Returns network troubleshooting hint
Silent Errors ¶
For errors that should exit non-zero but not print (because the command already explained the issue):
return deperrors.Silent(err) // CLI framework won't print this
Error Types ¶
Available error types:
- PolicyError - Policy evaluation or compilation failures
- ValidationError - Input validation failures
- NetworkError - Network operation failures with retry context
- ConfigError - Configuration-related failures
- ScanError - Dependency scanning failures
- TargetError - Target resolution failures (repos, images)
- PluginError - Plugin execution failures
Package errors provides domain-specific error types for Deputy. These errors enable robust error handling using errors.Is and errors.As, allowing callers to distinguish between different failure modes and handle them appropriately.
Index ¶
- Variables
- func ExitCode(err error) int
- func GetSuggestion(err error) string
- func Silent(err error) error
- func Suggest(err error, suggestion string) error
- func SuggestFor(category string) string
- func WithExitCode(err error, code int) error
- type ConfigError
- type ExitError
- type NetworkError
- type PluginError
- type PluginFailure
- type PolicyError
- type ScanError
- type SilentError
- type Suggestible
- type TargetError
- type ValidationError
- type WithSuggestion
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound indicates a requested resource does not exist. ErrNotFound = errors.New("not found") // ErrInvalidInput indicates user input failed validation. ErrInvalidInput = errors.New("invalid input") // ErrNetwork indicates a network operation failed. ErrNetwork = errors.New("network error") // ErrConfiguration indicates invalid or missing configuration. ErrConfiguration = errors.New("configuration error") // ErrPermission indicates insufficient permissions. ErrPermission = errors.New("permission denied") )
Sentinel errors for common failure modes
var CommonSuggestions = map[string]string{
"no go.mod": "Run 'go mod init' to initialize a Go module in this directory",
"no package.json": "Run 'npm init' to create a package.json file",
"network": "Check your internet connection and try again. If behind a proxy, set HTTP_PROXY/HTTPS_PROXY",
"auth": "Check your credentials. For GitHub, ensure GITHUB_TOKEN is set correctly",
"rate limit": "Wait a few minutes or authenticate to increase rate limits",
"not found": "Verify the path or URL is correct and the resource exists",
"permission denied": "Check file permissions or run with appropriate privileges",
"invalid format": "Check the input format matches expected format (JSON, YAML, etc.)",
"policy syntax": "Run 'deputy policy lint' to validate your policy files",
"no vulnerabilities": "No action needed - your dependencies appear secure",
}
CommonSuggestions provides standard remediation suggestions for common errors.
Functions ¶
func ExitCode ¶
ExitCode returns the exit code from an error chain if present. Returns 1 if error is non-nil but has no ExitError, or 0 if error is nil.
func GetSuggestion ¶
GetSuggestion extracts a suggestion from an error chain. Returns empty string if no suggestion is found.
func Silent ¶
Silent wraps err so the CLI framework can suppress printing while still returning a non-zero exit status.
func Suggest ¶
Suggest wraps an error with a suggestion for how to fix it. Returns nil if err is nil.
func SuggestFor ¶
SuggestFor returns a standard suggestion for a given error category. Falls back to empty string if no match is found.
func WithExitCode ¶
WithExitCode wraps an error with a specific exit code. Returns nil if both err is nil and code is 0.
Types ¶
type ConfigError ¶
ConfigError represents configuration-related failures.
func (*ConfigError) Error ¶
func (e *ConfigError) Error() string
func (*ConfigError) Is ¶
func (e *ConfigError) Is(target error) bool
func (*ConfigError) Unwrap ¶
func (e *ConfigError) Unwrap() error
type ExitError ¶
type ExitError struct {
Code int // Exit code to use (0 = success, 1 = error, 130 = interrupted, etc.)
Cause error // Underlying error (may be nil for non-error exit codes)
}
ExitError carries a specific exit code for the CLI to use. This allows commands to signal different exit codes (e.g., 130 for SIGINT, or custom codes for partial success/failure scenarios).
type NetworkError ¶
NetworkError represents network operation failures with retry context.
func (*NetworkError) Error ¶
func (e *NetworkError) Error() string
func (*NetworkError) Is ¶
func (e *NetworkError) Is(target error) bool
func (*NetworkError) Temporary ¶
func (e *NetworkError) Temporary() bool
func (*NetworkError) Unwrap ¶
func (e *NetworkError) Unwrap() error
type PluginError ¶
type PluginError struct {
Failures []PluginFailure
}
PluginError represents one or more plugin failures during scanning.
func (*PluginError) Error ¶
func (e *PluginError) Error() string
func (*PluginError) Is ¶
func (e *PluginError) Is(target error) bool
func (*PluginError) Unwrap ¶
func (e *PluginError) Unwrap() error
Unwrap returns the first plugin error for compatibility with errors.Unwrap.
type PluginFailure ¶
PluginFailure represents a single plugin failure with context.
type PolicyError ¶
PolicyError represents a policy evaluation or compilation failure.
func (*PolicyError) Error ¶
func (e *PolicyError) Error() string
func (*PolicyError) Is ¶
func (e *PolicyError) Is(target error) bool
func (*PolicyError) Unwrap ¶
func (e *PolicyError) Unwrap() error
type ScanError ¶
type ScanError struct {
Target string
Phase string // "inventory", "query", "analysis"
Message string
Cause error
}
ScanError represents failures during dependency scanning.
type SilentError ¶
type SilentError struct {
Cause error
}
SilentError indicates an error that should cause a non-zero exit but should not be printed by the CLI framework (because the command already explained it).
func (*SilentError) Error ¶
func (e *SilentError) Error() string
func (*SilentError) Unwrap ¶
func (e *SilentError) Unwrap() error
type Suggestible ¶
Suggestible is an interface for errors that can provide remediation suggestions.
type TargetError ¶
type TargetError struct {
Target string
Message string
Cause error
// contains filtered or unexported fields
}
TargetError represents failures related to target resolution (repos, images, etc.).
func NewTargetError ¶
func NewTargetError(target, message string, cause error, suggestion string) *TargetError
NewTargetError creates a TargetError with an optional suggestion.
func (*TargetError) Error ¶
func (e *TargetError) Error() string
func (*TargetError) Is ¶
func (e *TargetError) Is(target error) bool
func (*TargetError) Suggestion ¶
func (e *TargetError) Suggestion() string
func (*TargetError) Unwrap ¶
func (e *TargetError) Unwrap() error
type ValidationError ¶
ValidationError represents input validation failures with detailed field information.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
func (*ValidationError) Is ¶
func (e *ValidationError) Is(target error) bool
func (*ValidationError) Unwrap ¶
func (e *ValidationError) Unwrap() error
type WithSuggestion ¶
type WithSuggestion struct {
Err error
// contains filtered or unexported fields
}
WithSuggestion wraps an error with a remediation suggestion. The suggestion appears in CLI output to help users resolve the issue.
func (*WithSuggestion) Error ¶
func (e *WithSuggestion) Error() string
func (*WithSuggestion) Suggestion ¶
func (e *WithSuggestion) Suggestion() string
func (*WithSuggestion) Unwrap ¶
func (e *WithSuggestion) Unwrap() error