-
Notifications
You must be signed in to change notification settings - Fork 10
/
errors.go
64 lines (55 loc) · 2.17 KB
/
errors.go
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
package pcap
import (
"errors"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
errTestEnded = fmt.Errorf("test ended")
errContextCancelled = fmt.Errorf("context error")
errDiscardedMsg = fmt.Errorf("discarding packets")
ErrValidationFailed = fmt.Errorf("validation failed")
errNilField = fmt.Errorf("field is nil: %w", ErrValidationFailed)
errEmptyField = fmt.Errorf("field is empty: %w", ErrValidationFailed)
errInvalidPayload = fmt.Errorf("invalid payload: %w", ErrValidationFailed)
errIllegalCharacter = fmt.Errorf("illegal character: %w", ErrValidationFailed)
errNoVcapID = fmt.Errorf("no vcap-id")
errTooManyCaptures = fmt.Errorf("too many concurrent captures")
errDraining = fmt.Errorf("draining")
errUnexpectedMessage = fmt.Errorf("unexpected message")
ErrNoEndpoints = fmt.Errorf("no matching endpoints found")
ErrNotConnected = fmt.Errorf("client not connected to api")
ErrResolverUnhealthy = fmt.Errorf("resolver unhealthy")
ErrNotAuthorized = fmt.Errorf("not authorized")
ErrTokenUnsupported = fmt.Errorf("token unsupported: %w", ErrNotAuthorized)
ErrBoshNotConnected = fmt.Errorf("not connected to bosh director")
)
// pcapError is an attempt to work around the shortcomings of error handling in the gRPC
// library. It's gRPC status compatible and supports unwrapping (if an error has been
// wrapped).
// Use errorf to construct a new error.
type pcapError struct {
status *status.Status
inner error
}
func (e pcapError) GRPCStatus() *status.Status {
return e.status
}
func (e pcapError) Error() string {
return e.status.Message()
}
func (e pcapError) Unwrap() error {
return e.inner
}
// errorf creates a new error that is compatible with gRPC status and has support for
// wrapping errors. It works like fmt.Errorf (because that's what's used under the hood).
func errorf(c codes.Code, format string, a ...interface{}) error {
// we simply utilize fmt.Errorf instead of struggling with a custom implementation
e := fmt.Errorf(format, a...)
return pcapError{
status: status.New(c, e.Error()),
// if something has been wrapped, get it
inner: errors.Unwrap(e),
}
}