2026-02-22 17:45:48 +00:00
|
|
|
package channels
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ClassifySendError wraps a raw error with the appropriate sentinel based on
|
|
|
|
|
// an HTTP status code. Channels that perform HTTP API calls should use this
|
|
|
|
|
// in their Send path.
|
|
|
|
|
func ClassifySendError(statusCode int, rawErr error) error {
|
|
|
|
|
switch {
|
|
|
|
|
case statusCode == http.StatusTooManyRequests:
|
2026-06-08 01:10:14 +00:00
|
|
|
return fmt.Errorf("%w: %w", ErrRateLimit, rawErr)
|
2026-02-22 17:45:48 +00:00
|
|
|
case statusCode >= 500:
|
2026-06-08 01:10:14 +00:00
|
|
|
return fmt.Errorf("%w: %w", ErrTemporary, rawErr)
|
2026-02-22 17:45:48 +00:00
|
|
|
case statusCode >= 400:
|
2026-06-08 01:10:14 +00:00
|
|
|
return fmt.Errorf("%w: %w", ErrSendFailed, rawErr)
|
2026-02-22 17:45:48 +00:00
|
|
|
default:
|
|
|
|
|
return rawErr
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ClassifyNetError wraps a network/timeout error as ErrTemporary.
|
|
|
|
|
func ClassifyNetError(err error) error {
|
|
|
|
|
if err == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-06-08 01:10:14 +00:00
|
|
|
return fmt.Errorf("%w: %w", ErrTemporary, err)
|
2026-02-22 17:45:48 +00:00
|
|
|
}
|