errors

package
v0.0.0-...-80ccc60 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

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:

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

Constants

This section is empty.

Variables

View Source
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

View Source
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

func ExitCode(err error) int

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

func GetSuggestion(err error) string

GetSuggestion extracts a suggestion from an error chain. Returns empty string if no suggestion is found.

func Silent

func Silent(err error) error

Silent wraps err so the CLI framework can suppress printing while still returning a non-zero exit status.

func Suggest

func Suggest(err error, suggestion string) error

Suggest wraps an error with a suggestion for how to fix it. Returns nil if err is nil.

func SuggestFor

func SuggestFor(category string) string

SuggestFor returns a standard suggestion for a given error category. Falls back to empty string if no match is found.

func WithExitCode

func WithExitCode(err error, code int) error

WithExitCode wraps an error with a specific exit code. Returns nil if both err is nil and code is 0.

Types

type ConfigError

type ConfigError struct {
	Path    string
	Field   string
	Message string
	Cause   error
}

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).

func (*ExitError) Error

func (e *ExitError) Error() string

func (*ExitError) Unwrap

func (e *ExitError) Unwrap() error

type NetworkError

type NetworkError struct {
	Operation string
	URL       string
	Attempt   int
	Cause     error
}

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

type PluginFailure struct {
	Name   string
	Reason string
	Err    error
}

PluginFailure represents a single plugin failure with context.

type PolicyError

type PolicyError struct {
	PolicyName string
	Source     string
	Line       int
	Cause      error
}

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.

func (*ScanError) Error

func (e *ScanError) Error() string

func (*ScanError) Is

func (e *ScanError) Is(target error) bool

func (*ScanError) Unwrap

func (e *ScanError) Unwrap() error

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

type Suggestible interface {
	error
	Suggestion() string
}

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

type ValidationError struct {
	Field   string
	Value   any
	Message string
	Cause   error
}

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL