mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
3f435c5e56
errutil.go: Change %v to %w in ClassifySendError and ClassifyNetError so callers can use errors.Is/errors.As on the underlying HTTP/network error. isolated_command_transport.go: Change %v to %w in Close() and Write() error paths for the same reason.
31 lines
776 B
Go
31 lines
776 B
Go
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:
|
|
return fmt.Errorf("%w: %w", ErrRateLimit, rawErr)
|
|
case statusCode >= 500:
|
|
return fmt.Errorf("%w: %w", ErrTemporary, rawErr)
|
|
case statusCode >= 400:
|
|
return fmt.Errorf("%w: %w", ErrSendFailed, rawErr)
|
|
default:
|
|
return rawErr
|
|
}
|
|
}
|
|
|
|
// ClassifyNetError wraps a network/timeout error as ErrTemporary.
|
|
func ClassifyNetError(err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("%w: %w", ErrTemporary, err)
|
|
}
|