errors

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package errors provides error types and handling for the DAST crawler.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BackoffDuration

func BackoffDuration(attempt int, initial, max time.Duration, multiplier float64) time.Duration

BackoffDuration calculates the backoff duration for a given attempt.

func ExponentialBackoff

func ExponentialBackoff(maxRetries int, initial, max time.Duration, multiplier float64) []time.Duration

ExponentialBackoff returns a sequence of backoff durations.

func GetStatusCode

func GetStatusCode(err error) int

GetStatusCode extracts the status code from an error.

func IsAuthError

func IsAuthError(err error) bool

IsAuthError checks if an error is authentication-related.

func IsRateLimitError

func IsRateLimitError(err error) bool

IsRateLimitError checks if an error is rate limiting.

func IsRetryable

func IsRetryable(err error) bool

IsRetryable checks if an error should be retried.

Types

type CircuitBreaker

type CircuitBreaker struct {
	// contains filtered or unexported fields
}

CircuitBreaker implements the circuit breaker pattern.

func NewCircuitBreaker

func NewCircuitBreaker(config CircuitBreakerConfig) *CircuitBreaker

NewCircuitBreaker creates a new circuit breaker.

func NewDefaultCircuitBreaker

func NewDefaultCircuitBreaker() *CircuitBreaker

NewDefaultCircuitBreaker creates a circuit breaker with default configuration.

func (*CircuitBreaker) Allow

func (cb *CircuitBreaker) Allow() bool

Allow checks if a request should be allowed.

func (*CircuitBreaker) Execute

func (cb *CircuitBreaker) Execute(fn func() error) error

Execute executes a function through the circuit breaker.

func (*CircuitBreaker) OnStateChange

func (cb *CircuitBreaker) OnStateChange(fn func(from, to CircuitState))

OnStateChange sets a callback for state changes.

func (*CircuitBreaker) RecordFailure

func (cb *CircuitBreaker) RecordFailure()

RecordFailure records a failed request.

func (*CircuitBreaker) RecordSuccess

func (cb *CircuitBreaker) RecordSuccess()

RecordSuccess records a successful request.

func (*CircuitBreaker) Reset

func (cb *CircuitBreaker) Reset()

Reset resets the circuit breaker to closed state.

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() CircuitState

State returns the current state.

func (*CircuitBreaker) Stats

func (cb *CircuitBreaker) Stats() CircuitBreakerStats

Stats returns current statistics.

type CircuitBreakerConfig

type CircuitBreakerConfig struct {
	FailureThreshold int           // Number of failures before opening
	SuccessThreshold int           // Number of successes in half-open before closing
	Timeout          time.Duration // Time to wait before trying half-open
	MaxConcurrent    int           // Max concurrent requests in half-open (0 = 1)
}

CircuitBreakerConfig configures a circuit breaker.

func DefaultCircuitBreakerConfig

func DefaultCircuitBreakerConfig() CircuitBreakerConfig

DefaultCircuitBreakerConfig returns sensible defaults.

type CircuitBreakerStats

type CircuitBreakerStats struct {
	State            CircuitState
	Failures         int
	Successes        int
	LastFailureTime  time.Time
	HalfOpenRequests int
}

CircuitBreakerStats holds circuit breaker statistics.

type CircuitOpenError

type CircuitOpenError struct {
	State CircuitState
}

CircuitOpenError is returned when the circuit is open.

func (*CircuitOpenError) Error

func (e *CircuitOpenError) Error() string

Error implements the error interface.

type CircuitState

type CircuitState int

CircuitState represents the state of a circuit breaker.

const (
	// Closed means the circuit is operating normally.
	Closed CircuitState = iota
	// Open means the circuit has tripped and requests are blocked.
	Open
	// HalfOpen means the circuit is testing if it can close again.
	HalfOpen
)

func (CircuitState) String

func (s CircuitState) String() string

String returns the string representation of CircuitState.

type CrawlError

type CrawlError struct {
	Type       ErrorType
	URL        string
	Operation  string
	Message    string
	Cause      error
	StatusCode int
	Retryable  bool
}

CrawlError represents a categorized crawl error.

func Categorize

func Categorize(err error, url string) *CrawlError

Categorize determines the error type from a generic error.

func CategorizeHTTPStatus

func CategorizeHTTPStatus(statusCode int, url string) *CrawlError

CategorizeHTTPStatus creates an error from HTTP status code.

func NewAuthError

func NewAuthError(url string, statusCode int, message string) *CrawlError

NewAuthError creates an authentication error.

func NewBrowserError

func NewBrowserError(url, operation string, cause error) *CrawlError

NewBrowserError creates a browser error.

func NewCancelledError

func NewCancelledError(url, operation string) *CrawlError

NewCancelledError creates a cancelled error.

func NewClientError

func NewClientError(url string, statusCode int, message string) *CrawlError

NewClientError creates a client error.

func NewCrawlError

func NewCrawlError(errType ErrorType, url, operation, message string, cause error) *CrawlError

NewCrawlError creates a new CrawlError.

func NewNetworkError

func NewNetworkError(url, operation string, cause error) *CrawlError

NewNetworkError creates a network error.

func NewNotFoundError

func NewNotFoundError(url string) *CrawlError

NewNotFoundError creates a not found error.

func NewParseError

func NewParseError(url, operation string, cause error) *CrawlError

NewParseError creates a parse error.

func NewRateLimitError

func NewRateLimitError(url string, retryAfter int) *CrawlError

NewRateLimitError creates a rate limit error.

func NewScopeError

func NewScopeError(url, reason string) *CrawlError

NewScopeError creates a scope error.

func NewServerError

func NewServerError(url string, statusCode int, message string) *CrawlError

NewServerError creates a server error.

func NewTimeoutError

func NewTimeoutError(url, operation string, cause error) *CrawlError

NewTimeoutError creates a timeout error.

func (*CrawlError) Error

func (e *CrawlError) Error() string

Error implements the error interface.

func (*CrawlError) Is

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

Is checks if the error matches a target.

func (*CrawlError) Unwrap

func (e *CrawlError) Unwrap() error

Unwrap returns the underlying error.

type ErrorType

type ErrorType int

ErrorType categorizes errors for handling decisions.

const (
	// Unknown is an uncategorized error.
	Unknown ErrorType = iota
	// Network represents network-related errors (DNS, connection).
	Network
	// Timeout represents timeout errors.
	Timeout
	// RateLimit represents rate limiting (429) errors.
	RateLimit
	// Auth represents authentication/authorization errors (401, 403).
	Auth
	// NotFound represents 404 errors.
	NotFound
	// ServerError represents 5xx errors.
	ServerError
	// ClientError represents 4xx errors (except 401, 403, 404, 429).
	ClientError
	// Parse represents parsing errors (HTML, JSON, etc.).
	Parse
	// Browser represents browser/CDP errors.
	Browser
	// Scope represents scope violation errors.
	Scope
	// Cancelled represents context cancellation.
	Cancelled
)

func GetErrorType

func GetErrorType(err error) ErrorType

GetErrorType extracts the error type from an error.

func (ErrorType) IsRetryable

func (t ErrorType) IsRetryable() bool

IsRetryable returns whether errors of this type should be retried.

func (ErrorType) String

func (t ErrorType) String() string

String returns the string representation of ErrorType.

type HostCircuitBreakers

type HostCircuitBreakers struct {
	// contains filtered or unexported fields
}

HostCircuitBreakers manages circuit breakers per host.

func NewHostCircuitBreakers

func NewHostCircuitBreakers(config CircuitBreakerConfig) *HostCircuitBreakers

NewHostCircuitBreakers creates a new host circuit breaker manager.

func (*HostCircuitBreakers) AllStats

func (hcb *HostCircuitBreakers) AllStats() map[string]CircuitBreakerStats

AllStats returns statistics for all hosts.

func (*HostCircuitBreakers) Get

func (hcb *HostCircuitBreakers) Get(host string) *CircuitBreaker

Get returns the circuit breaker for a host, creating one if needed.

func (*HostCircuitBreakers) Reset

func (hcb *HostCircuitBreakers) Reset()

Reset resets all circuit breakers.

type Retrier

type Retrier struct {
	// contains filtered or unexported fields
}

Retrier implements retry logic with exponential backoff.

func NewDefaultRetrier

func NewDefaultRetrier() *Retrier

NewDefaultRetrier creates a retrier with default configuration.

func NewRetrier

func NewRetrier(config RetryConfig) *Retrier

NewRetrier creates a new retrier.

func (*Retrier) Do

func (r *Retrier) Do(ctx context.Context, operation string, url string, fn RetryFunc) *RetryResult

Do executes the function with retries.

type RetryConfig

type RetryConfig struct {
	MaxRetries     int           // Maximum number of retries (0 = no retries)
	InitialDelay   time.Duration // Initial delay before first retry
	MaxDelay       time.Duration // Maximum delay between retries
	Multiplier     float64       // Delay multiplier for exponential backoff
	Jitter         float64       // Random jitter factor (0-1)
	RetryableTypes []ErrorType   // Error types that should be retried
}

RetryConfig configures retry behavior.

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns sensible defaults.

type RetryFunc

type RetryFunc func(ctx context.Context) error

RetryFunc is a function that can be retried.

type RetryResult

type RetryResult struct {
	Attempts  int           // Number of attempts made
	LastError error         // The last error encountered
	Duration  time.Duration // Total time spent retrying
	Success   bool          // Whether the operation succeeded
}

RetryResult holds the result of a retry operation.

func DoWithResult

func DoWithResult[T any](ctx context.Context, r *Retrier, operation, url string, fn func(ctx context.Context) (T, error)) (T, *RetryResult)

DoWithResult executes a function that returns a value and error.

Jump to

Keyboard shortcuts

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