Documentation
¶
Overview ¶
Package errors provides error types and handling for the DAST crawler.
Index ¶
- func BackoffDuration(attempt int, initial, max time.Duration, multiplier float64) time.Duration
- func ExponentialBackoff(maxRetries int, initial, max time.Duration, multiplier float64) []time.Duration
- func GetStatusCode(err error) int
- func IsAuthError(err error) bool
- func IsRateLimitError(err error) bool
- func IsRetryable(err error) bool
- type CircuitBreaker
- func (cb *CircuitBreaker) Allow() bool
- func (cb *CircuitBreaker) Execute(fn func() error) error
- func (cb *CircuitBreaker) OnStateChange(fn func(from, to CircuitState))
- func (cb *CircuitBreaker) RecordFailure()
- func (cb *CircuitBreaker) RecordSuccess()
- func (cb *CircuitBreaker) Reset()
- func (cb *CircuitBreaker) State() CircuitState
- func (cb *CircuitBreaker) Stats() CircuitBreakerStats
- type CircuitBreakerConfig
- type CircuitBreakerStats
- type CircuitOpenError
- type CircuitState
- type CrawlError
- func Categorize(err error, url string) *CrawlError
- func CategorizeHTTPStatus(statusCode int, url string) *CrawlError
- func NewAuthError(url string, statusCode int, message string) *CrawlError
- func NewBrowserError(url, operation string, cause error) *CrawlError
- func NewCancelledError(url, operation string) *CrawlError
- func NewClientError(url string, statusCode int, message string) *CrawlError
- func NewCrawlError(errType ErrorType, url, operation, message string, cause error) *CrawlError
- func NewNetworkError(url, operation string, cause error) *CrawlError
- func NewNotFoundError(url string) *CrawlError
- func NewParseError(url, operation string, cause error) *CrawlError
- func NewRateLimitError(url string, retryAfter int) *CrawlError
- func NewScopeError(url, reason string) *CrawlError
- func NewServerError(url string, statusCode int, message string) *CrawlError
- func NewTimeoutError(url, operation string, cause error) *CrawlError
- type ErrorType
- type HostCircuitBreakers
- type Retrier
- type RetryConfig
- type RetryFunc
- type RetryResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BackoffDuration ¶
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 ¶
GetStatusCode extracts the status code from an error.
func IsAuthError ¶
IsAuthError checks if an error is authentication-related.
func IsRateLimitError ¶
IsRateLimitError checks if an error is rate limiting.
func IsRetryable ¶
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 ¶
GetErrorType extracts the error type from an error.
func (ErrorType) IsRetryable ¶
IsRetryable returns whether errors of this type should be retried.
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.
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.