Skip to content

Error Types ​

HTTPC uses a two-layer error model: the lower layer is the standard error interface, and the upper layer is the classified ClientError struct. Network-layer request failures are mapped by classifyError to a context-carrying ClientError, which provides an error type, a retryability check, and structured fields.

HTTP-layer errors (4xx/5xx) are not returned as errors; check them via Result.StatusCode().

ClientError ​

go
type ClientError = engine.ClientError

Classified HTTP client error, extracted via errors.As. It is a type alias of the internal engine.ClientError.

Struct Fields ​

go
type ClientError struct {
    Type       ErrorType  // Error classification
    Message    string     // Error description
    Cause      error      // Underlying error
    URL        string     // Request URL (sanitized)
    Method     string     // HTTP method
    Attempts   int        // Number of attempts made
    StatusCode int        // HTTP status code (if applicable)
    Host       string     // Hostname (for circuit breakers)
    ProxyURL   string     // Redacted URL of the proxy used for the final attempt (empty for direct connections, pre-dispatch errors, and system proxy)
}
FieldTypeDescriptionTypical value
ClientError.TypeErrorTypeError classification, for switch branchingErrorTypeNetwork, ErrorTypeTimeout
ClientError.MessagestringError description"network operation failed"
ClientError.CauseerrorUnderlying error, obtainable via Unwrap()*net.OpError, *net.DNSError
ClientError.URLstringRequest URL (credentials sanitized)"https://example.com/path"
ClientError.MethodstringHTTP method"GET", "POST"
ClientError.AttemptsintNumber of attempts (including the first request)1 (first failure), 4 (after 3 retries)
ClientError.StatusCodeintHTTP status code (0 for non-HTTP errors)0 (network error), 503 (server error)
ClientError.HoststringRequest hostname (for circuit breakers)"example.com"
ClientError.ProxyURLstringRedacted URL of the proxy used for the final attempt (password masked, safe to log); empty for direct connections, for errors raised before dispatch (validation or callback failures), and under EnableSystemProxy; with proxy rotation it reports the last attempt's proxy"http://proxy:8080"

Host lets circuit breakers track failures per hostname, Message can be logged directly, and Cause preserves the underlying error for errors.As inspection. ProxyURL is the value the transport records when it selects a proxy, the same source that populates Result.Meta.ProxyURL on success, so a dial failure still reports which proxy was about to be used.

Methods ​

MethodSignatureDescription
Errorfunc (e *ClientError) Error() stringFormatted as "METHOD url: message: cause (attempt N)"
Codefunc (e *ClientError) Code() stringReturns a short error code, e.g. "NETWORK_ERROR", "TIMEOUT"
IsRetryablefunc (e *ClientError) IsRetryable() boolReturns whether this error is retryable
Unwrapfunc (e *ClientError) Unwrap() errorReturns ClientError.Cause, supporting errors.Is/errors.As traversal of the error chain

All four methods are defined on the *ClientError pointer receiver. ClientError.Error implements the error interface; ClientError.Code and ClientError.IsRetryable serve log classification and retry decisions; ClientError.Unwrap exposes the underlying cause.

Error() Formatting ​

Error() formats the error into a readable string:

  • Both URL and Method present: "GET https://example.com: network operation failed: dial tcp ... (attempt 1)"
  • Message only: outputs Message directly
  • Has Cause: appends ": " + Cause.Error()
  • Has Attempts (>0): appends " (attempt N)"

The URL is sanitized via SanitizeURL (credentials removed) before output. Errors produced by the engine classification path are pre-sanitized (urlSanitized=true), so the redundant url.Parse call is skipped and no allocation occurs per log output.

Code() Error Codes ​

Code() returns a short string identifying the error type, used for log classification and monitoring alerts:

ErrorTypeCode() return value
ErrorTypeNetwork"NETWORK_ERROR"
ErrorTypeTimeout"TIMEOUT"
ErrorTypeContextCanceled"CONTEXT_CANCELED"
ErrorTypeResponseRead"RESPONSE_READ_ERROR"
ErrorTypeTransport"TRANSPORT_ERROR"
ErrorTypeRetryExhausted"RETRY_EXHAUSTED"
ErrorTypeTLS"TLS_ERROR"
ErrorTypeCertificate"CERTIFICATE_ERROR"
ErrorTypeDNS"DNS_ERROR"
ErrorTypeValidation"VALIDATION_ERROR"
ErrorTypeHTTP"HTTP_ERROR"
ErrorTypeUnknown (and others)"UNKNOWN_ERROR"
go
var clientErr *httpc.ClientError
if errors.As(err, &clientErr) {
    log.Printf("error code: %s, URL: %s, attempts: %d, retryable: %v",
        clientErr.Code(), clientErr.URL, clientErr.Attempts, clientErr.IsRetryable())
}

IsRetryable Decision Logic ​

IsRetryable() decides whether a single error is retryable: it checks context errors first, then dispatches by ErrorType. The table below is the complete rule set; for the retry strategy as a whole, see Retry and Fault Tolerance.

  1. Context errors first: if Cause is context.Canceled or context.DeadlineExceeded, return false immediately and never retry.
  2. Dispatch by ErrorType: see the table.
ErrorTypeRetryableDecision logic
ErrorTypeNetworkConditionalInspect Cause: wrapped ClientError -> recursive check; *net.OpError -> timeout or retryable syscall (ECONNREFUSED/ECONNRESET/EPIPE/ETIMEDOUT/ENETUNREACH/EHOSTUNREACH); net.Error -> retryable by default; message match ("connection reset"/"eof"/"broken pipe", etc.)
ErrorTypeTimeoutYesAll transport-layer timeouts are retryable
ErrorTypeTransportYesHTTP transport-layer errors
ErrorTypeResponseReadConditionalOnly read operations (Op == "read" or "readfrom") are retryable; write operations are not
ErrorTypeDNSConditionalWhen Cause is *net.DNSError, retry only if IsTemporary or IsTimeout is true
ErrorTypeHTTPConditionalRetryable when StatusCode hits retryableStatusCodes (408/429/500/502/503/504)
ErrorTypeContextCanceledNoCanceled by the caller
ErrorTypeValidationNoThe request itself is invalid; retrying cannot help
ErrorTypeTLSNoTLS protocol error; retrying cannot help
ErrorTypeCertificateNoCertificate verification failed; retrying cannot help
ErrorTypeRetryExhaustedNoRetries already exhausted
ErrorTypeUnknownNoUnknown error; not retried by default

retryableStatusCodes ​

go
var retryableStatusCodes = map[int]bool{
    408: true, // Request Timeout
    429: true, // Too Many Requests
    500: true, // Internal Server Error
    502: true, // Bad Gateway
    503: true, // Service Unavailable
    504: true, // Gateway Timeout
}

This is the source of truth for HTTP-status-code-triggered retries under the built-in policy; the retry engine and IsRetryable() share this table. When Connection.ProxyRotateOnStatus is configured, the engine additionally treats those status codes as retryable beyond this table — such a request may be retried even though its ClientError.IsRetryable() returns false.

Subtle distinction among timeout types

ErrorTypeTimeout is retryable, but timeouts triggered by a context deadline are not: context.DeadlineExceeded is intercepted at step 1 and returns false. Only transport-layer timeouts (e.g. net.OpError.Timeout()) reach step 2 and count as retryable, so a WithTimeout deadline is never exceeded by retries.

ErrorType ​

go
type ErrorType = engine.ErrorType

Error classification enum (type int).

ConstantValueMeaningTypical trigger scenarioRetryable
ErrorTypeUnknown0Unknown/unclassifiedMatches no known patternNo
ErrorTypeNetwork1Network-layer errorConnection refused, connection reset, network unreachableConditional
ErrorTypeTimeout2Timeoutnet.OpError timeout, context deadlineConditional
ErrorTypeContextCanceled3Context canceledTriggered by context.CancelNo
ErrorTypeResponseRead4Response-body read errorEOF/connection drop while reading the bodyConditional
ErrorTypeTransport5Transport-layer errorHTTP protocol error, transport failureYes
ErrorTypeRetryExhausted6Retries exhaustedStill failing after reaching MaxRetriesNo
ErrorTypeTLS7TLS errorTLS handshake failure, protocol mismatchNo
ErrorTypeCertificate8Certificate verification errorx509 certificate expired/untrustedNo
ErrorTypeDNS9DNS resolution errorDomain does not exist, DNS timeoutConditional
ErrorTypeValidation10Request validation errorMalformed URL, redirect limit exceeded, CRLF injectionNo
ErrorTypeHTTP11HTTP-layer errorAssigned only by the classifier's error-message pattern match (message contains HTTP 4xx/HTTP 5xx). A normal 4xx/5xx response is not an error; an error returned by custom middleware never reaches the classifier, so it cannot carry this typeConditional

¹ Timeouts triggered by a context deadline (WithTimeout, TimeoutConfig.Request) are not retried; only transport-level timeouts (e.g. net.OpError timeout) are retried. ² See IsRetryable decision logic above.

Type Checking ​

go
result, err := client.Get(url)
if err != nil {
    var clientErr *httpc.ClientError
    if errors.As(err, &clientErr) {
        switch clientErr.Type {
        case httpc.ErrorTypeTimeout:
            log.Println("request timed out")
        case httpc.ErrorTypeNetwork:
            log.Println("network error:", clientErr.Message)
        case httpc.ErrorTypeTLS:
            log.Println("TLS error")
        case httpc.ErrorTypeCertificate:
            log.Println("certificate verification failed")
        case httpc.ErrorTypeDNS:
            log.Println("DNS resolution failed")
        case httpc.ErrorTypeRetryExhausted:
            log.Println("retries exhausted, attempted", clientErr.Attempts, "times")
        case httpc.ErrorTypeContextCanceled:
            log.Println("request canceled")
        case httpc.ErrorTypeValidation:
            log.Println("request validation failed")
        }
    }
}

URL Sanitization Mechanism ​

ClientError.Error() calls validation.SanitizeURL when formatting to remove credentials from the URL (user:pass@host -> ***:***@host), keeping sensitive data out of logs and error messages.

go
// Original URL: https://admin:[email protected]/data
// Error() output: GET https://***:***@api.example.com/data: ...

The engine classification path (classifyErrorWithSanitizedURL) sanitizes at first classification and sets urlSanitized=true, so later Error() calls skip the redundant url.Parse and allocate nothing per log output.

Error sanitization in callbacks

In callbacks of middleware such as MetricsMiddleware and LoggingMiddleware, HTTPC additionally inspects the messages of non-ClientError errors and replaces any original URL with a sanitized version, so callbacks do not leak credentials.

Error Classification Flow ​

classifyError maps an underlying error to a *ClientError, checking layer by layer in this order:

  1. Context errors: context.Canceled -> ErrorTypeContextCanceled; context.DeadlineExceeded -> ErrorTypeTimeout
  2. Connection-pool exhaustion: connection.ErrPoolExhausted -> ErrorTypeNetwork
  3. *url.Error unwrap: invalid HTTP/2 header, URL parse failure -> ErrorTypeValidation; otherwise unwrap the inner error and continue
  4. *net.DNSError: ErrorTypeDNS, distinguishing timeout from failure
  5. *net.OpError: ErrorTypeNetwork, distinguishing timeout from operation failure
  6. net.Error: timeout -> ErrorTypeTimeout; other -> ErrorTypeNetwork
  7. Message-pattern matching (fallback): matches 20+ patterns by keywords in the error message (TLS/certificate/timeout/connection refused, etc.)
  8. Fallback: when the inner of a url.Error matches no pattern -> ErrorTypeNetwork; otherwise -> ErrorTypeUnknown

Error Variables ​

Configuration Errors ​

VariableError messageTrigger condition
ErrNilConfig"config cannot be nil"nil Config passed to New/ValidateConfig
ErrInvalidTimeout"invalid timeout"Timeout value is 0, negative, or above the 30-minute cap (WithTimeout); for no timeout use WithNoTimeout
ErrInvalidRetry"invalid retry configuration"MaxRetries outside 0-10, BackoffFactor outside 1.0-10.0
ErrInvalidConnection"invalid connection configuration"MaxIdleConns/MaxConnsPerHost out of range, malformed ProxyURL
ErrInvalidSecurity"invalid security configuration"A body-size cap is out of range, a TLS version is invalid or min exceeds max, SSRFExemptCIDRs holds an invalid CIDR, a SensitiveHeaders key is invalid, or TLSFingerprint is unknown or combined with TLSConfig
ErrInvalidMiddleware"invalid middleware configuration"Defaults.UserAgent exceeds 512 characters or contains control characters
ErrInvalidHeader"invalid header"Header key-value contains control characters or exceeds the size limit

Request and Response Errors ​

VariableError messageTrigger condition
ErrEmptyFilePath"file path cannot be empty"DownloadConfig.FilePath is empty
ErrFileExists"file already exists"File exists and Overwrite=false and ResumeDownload=false
ErrResponseBodyEmpty"response body is empty"Calling Unmarshal or similar parsing methods on an empty body
ErrResponseBodyTooLarge"response body too large"The body parsed by Result.Unmarshal exceeds the 50 MB JSON cap. The engine-side size cap (Security.MaxResponseBodySize) reports response body exceeds limit of N bytes instead, which is not this sentinel
ErrInvalidMaxRedirects"invalid max redirects"WithMaxRedirects passed a negative value or one exceeding 50, or Security.MaxRedirects outside 0-50
ErrInvalidCredentials"invalid credentials"WithBasicAuth/WithBearerToken credentials empty, oversized, or containing invalid characters
ErrStreamBodyRequiresDownload"WithStreamBody requires Download; standard request methods buffer the response body"WithStreamBody(true) combined with the standard request methods (Get/Post/.../Request); streaming works only on the Download path

Download and Proxy Errors ​

VariableError messageTrigger condition
ErrDownloadInProgress"download already in progress for this file path"A download to the same file path is already running in this process; the second caller fails immediately instead of blocking, and can retry once the first download finishes. The mutex is in-process only; cross-process coordination is the caller's responsibility
ErrProxyConnectionFailed"proxy connection failed"A configured proxy could not be dialed (dial failure, invalid address, refused connection, etc.); wrapped with the underlying cause in the error chain and detectable via errors.Is. The retry engine uses it to move to the next proxy

Client Errors ​

VariableError messageTrigger condition
ErrClientClosed"client is closed"Using the client after Close()

Security Warning API ​

SetSecurityWarnOutput ​

go
func SetSecurityWarnOutput(w io.Writer)

Sets the output target of security warnings (default os.Stderr); defined in warnings.go. Passing io.Discard suppresses all warnings. A test environment is detected from the executable name (.test suffix) and the GO_TEST/GOTEST environment variables.

Three configurations trigger warnings in non-test environments, each printed at most once per process (sync.Once):

Trigger conditionWarning summary
TestingConfig() is called outside a test environmentPoints out that TLS certificate verification, SSRF protection, and URL/Header validation are all disabled, and recommends SecureConfig() or DefaultConfig() instead (the warning fires inside the preset function, regardless of whether a client is created)
Security.InsecureSkipVerify=true in a non-test environmentPoints out that TLS certificate verification is disabled, that it should only be used in tests, and that production should use SecureConfig()
A hand-built Config hits the all-off signature (ValidateURL/ValidateHeaders/StrictContentLength/FollowRedirects/EnableHTTP2 all false and Security.MaxResponseBodySize == 0)Prints [CONFIG WARNING]: those Default: true fields are true only in DefaultConfig()/presets, and a struct literal zeroes them; start from DefaultConfig() instead
go
// Suppress security warnings (only use after confirming the configuration is safe)
httpc.SetSecurityWarnOutput(io.Discard)

// Change the output to a custom log
httpc.SetSecurityWarnOutput(log.Writer())

Don't overuse suppression

SetSecurityWarnOutput(io.Discard) silently swallows security warnings. Use it only after fully auditing the configuration (e.g. confirming that TestingConfig is used only in test binaries); do not use it to mask warnings in a production deployment. For the complete list of security practices, see the Security Overview.

Practical Matching Patterns ​

errors.As to Extract ClientError ​

go
result, err := client.Get(url)
if err != nil {
    var clientErr *httpc.ClientError
    if errors.As(err, &clientErr) {
        // Access structured fields
        fmt.Printf("error code: %s\n", clientErr.Code())
        fmt.Printf("error type: %d\n", clientErr.Type)
        fmt.Printf("request: %s %s\n", clientErr.Method, clientErr.URL)
        fmt.Printf("attempts: %d\n", clientErr.Attempts)
        if clientErr.StatusCode != 0 {
            fmt.Printf("status code: %d\n", clientErr.StatusCode)
        }
    }
}

errors.Is to Match Sentinel Errors ​

go
if errors.Is(err, httpc.ErrClientClosed) {
    // Client is closed; recreate it
}
if errors.Is(err, httpc.ErrResponseBodyEmpty) {
    // Response body is empty; skip parsing
}
if errors.Is(err, httpc.ErrFileExists) {
    // File already exists; prompt the user or set Overwrite=true
}

errors.Unwrap to Traverse the Error Chain ​

go
var clientErr *httpc.ClientError
if errors.As(err, &clientErr) {
    // Cause is the underlying error (e.g. *net.OpError)
    cause := clientErr.Unwrap()
    if cause != nil {
        var opErr *net.OpError
        if errors.As(cause, &opErr) {
            fmt.Println("operation:", opErr.Op)
            fmt.Println("network:", opErr.Net)
            fmt.Println("address:", opErr.Addr)
        }
    }
}

Choosing among the three matching styles

  • errors.As: to access structured ClientError fields (Type/Code/URL/Attempts, etc.).
  • errors.Is: to match sentinel errors (config and file errors such as ErrClientClosed).
  • errors.Unwrap: to reach the bottom-level net/error for system-level diagnostics.

See Also ​