-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
67 lines (55 loc) · 2.32 KB
/
errors.go
File metadata and controls
67 lines (55 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package relay
import (
"errors"
"fmt"
)
// Sentinel errors returned by Execute, ExecuteStream, and related methods.
var (
// ErrCircuitOpen is returned when the circuit breaker is in the Open state
// and the request is rejected without being sent.
ErrCircuitOpen = errors.New("circuit breaker is open")
// ErrMaxRetriesReached is returned when all retry attempts have been
// exhausted and the last attempt ended with a retryable error.
ErrMaxRetriesReached = errors.New("max retries reached")
// ErrRateLimitExceeded is returned when the rate limiter cannot grant a
// token before the request context expires.
ErrRateLimitExceeded = errors.New("rate limit exceeded")
// ErrNilRequest is returned when a nil *Request is passed to Execute.
ErrNilRequest = errors.New("request cannot be nil")
// ErrTimeout wraps context.DeadlineExceeded when the per-request timeout
// set via WithTimeout fires.
ErrTimeout = errors.New("request timed out")
// ErrBodyTruncated is a sentinel that callers may check against when they
// need to detect truncation programmatically; the actual truncation is
// signalled by Response.IsTruncated().
ErrBodyTruncated = errors.New("response body exceeded size limit and was truncated")
// ErrClientClosed is returned when Execute is called after Shutdown.
ErrClientClosed = errors.New("client is closed")
// ErrBulkheadFull is returned when the bulkhead limit is reached and the
// context is cancelled before a slot becomes available.
ErrBulkheadFull = errors.New("relay: bulkhead full")
)
// HTTPError represents an HTTP response whose status code indicates a client
// or server error (4xx or 5xx). It is returned by Response.AsHTTPError and
// can be used with errors.As.
type HTTPError struct {
StatusCode int
Status string
Body []byte
}
func (e *HTTPError) Error() string {
body := e.Body
const maxBodyInMsg = 512
if len(body) > maxBodyInMsg {
body = body[:maxBodyInMsg]
}
return fmt.Sprintf("http error: status=%d body=%s", e.StatusCode, string(body))
}
// IsHTTPError reports whether err (or any error in its chain) is an *HTTPError
// and returns it if so. Use this instead of errors.As when you want both the
// bool and the typed value in one call.
func IsHTTPError(err error) (*HTTPError, bool) {
var httpErr *HTTPError
ok := errors.As(err, &httpErr)
return httpErr, ok
}