Documentation
¶
Overview ¶
Package errors provides structured error types for the Stacktower application.
This package defines error codes and types that enable:
- Consistent error handling across CLI and API
- Machine-readable error codes for programmatic handling
- User-friendly error messages
- Error wrapping with context preservation
Error Codes ¶
Error codes follow a hierarchical naming convention:
- INVALID_*: Input validation failures
- NOT_FOUND_*: Resource not found
- NETWORK_*: Network-related errors
- INTERNAL_*: Unexpected internal errors
Usage ¶
err := errors.New(errors.ErrCodeInvalidInput, "invalid package name: %s", name)
if errors.Is(err, errors.ErrCodeInvalidInput) {
// Handle validation error
}
// Wrap existing errors
err := errors.Wrap(errors.ErrCodeNetwork, origErr, "failed to fetch %s", url)
Index ¶
- func Is(err error, code Code) bool
- func UserMessage(err error) string
- func ValidateCratesPackageName(name string) error
- func ValidateGoModulePath(path string) error
- func ValidateManifestFilename(filename string) error
- func ValidateNpmPackageName(name string) error
- func ValidatePackageName(name string) error
- func ValidatePath(path string) error
- func ValidatePythonPackageName(name string) error
- func ValidateURL(rawURL string) error
- type Code
- type Error
- type RateLimitedError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Is ¶
Is reports whether err has the given error code. It unwraps the error chain looking for an *Error with a matching code.
func UserMessage ¶
UserMessage returns a user-friendly message for the error. For *Error types, returns the message without the code prefix. For other errors, returns the error string as-is.
func ValidateCratesPackageName ¶
ValidateCratesPackageName validates a crates.io package name.
func ValidateGoModulePath ¶
ValidateGoModulePath validates a Go module path.
func ValidateManifestFilename ¶
ValidateManifestFilename validates a manifest filename for safety. It ensures the filename is a simple basename without path components.
func ValidateNpmPackageName ¶
ValidateNpmPackageName validates an npm package name.
func ValidatePackageName ¶
ValidatePackageName validates a package name for safety and correctness. It rejects names that could be used for path traversal or injection attacks.
The validation rules are intentionally conservative:
- No empty names
- No control characters
- No path traversal sequences (.., //, etc.)
- No null bytes
- Maximum length of 256 characters
Language-specific validation should be done separately by the language parsers.
func ValidatePath ¶
ValidatePath validates a file path within a repository for safety. It prevents path traversal attacks and ensures reasonable path length.
Validation rules:
- Path cannot be empty
- Maximum length of 500 characters
- No null bytes or control characters
- No absolute paths (must be relative)
- No path traversal sequences (..)
- No backslashes (Windows-style paths)
func ValidatePythonPackageName ¶
ValidatePythonPackageName validates a Python package name per PEP 508.
func ValidateURL ¶
ValidateURL validates a URL string for safety. It ensures the URL has a safe scheme (http or https).
Types ¶
type Code ¶
type Code string
Code represents a machine-readable error code.
const ( // Input validation errors ErrCodeInvalidInput Code = "INVALID_INPUT" ErrCodeInvalidLanguage Code = "INVALID_LANGUAGE" ErrCodeInvalidPackage Code = "INVALID_PACKAGE" ErrCodeInvalidFormat Code = "INVALID_FORMAT" ErrCodeInvalidStyle Code = "INVALID_STYLE" ErrCodeInvalidVizType Code = "INVALID_VIZ_TYPE" ErrCodeInvalidManifest Code = "INVALID_MANIFEST" ErrCodeInvalidPath Code = "INVALID_PATH" // Resource not found errors ErrCodeNotFound Code = "NOT_FOUND" ErrCodePackageNotFound Code = "PACKAGE_NOT_FOUND" ErrCodeFileNotFound Code = "FILE_NOT_FOUND" ErrCodeSessionNotFound Code = "SESSION_NOT_FOUND" // Network errors ErrCodeNetwork Code = "NETWORK_ERROR" ErrCodeTimeout Code = "TIMEOUT" ErrCodeRateLimited Code = "RATE_LIMITED" // Authentication errors ErrCodeForbidden Code = "FORBIDDEN" ErrCodeSessionExpired Code = "SESSION_EXPIRED" // Internal errors ErrCodeInternal Code = "INTERNAL_ERROR" ErrCodeUnsupported Code = "UNSUPPORTED" )
Error codes for different error categories.
type Error ¶
type Error struct {
Code Code // Machine-readable error code
Message string // Human-readable message
Cause error // Underlying error (optional)
}
Error is a structured error with a code and optional cause.
type RateLimitedError ¶
RateLimitedError provides additional information for rate-limited responses.
func (*RateLimitedError) Code ¶
func (e *RateLimitedError) Code() Code
Code returns the error code for this error type.
func (*RateLimitedError) Error ¶
func (e *RateLimitedError) Error() string
Error implements the error interface.