-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassthrough.go
More file actions
54 lines (45 loc) · 1.41 KB
/
passthrough.go
File metadata and controls
54 lines (45 loc) · 1.41 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
package errors
import (
"context"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// NewPassthroughError handles an error from an external dependency. If the error is a
// timeout, canceled, unavailable, unknown, or temporary error, it is passed through as
// the appropriate correlating type from this package. Otherwise, an internal error
// with the provided message is returned.
func NewPassthroughError(msg string, err error) error {
// test err against target interfaces
sErr, sOk := err.(interface{ GRPCStatus() *(status.Status) })
tiErr, tiOk := err.(interface{ Timeout() bool })
teErr, teOk := err.(interface{ Temporary() bool })
// CanceledError
switch {
case err == context.Canceled:
fallthrough
case sOk && codes.Canceled == sErr.GRPCStatus().Code():
return NewCanceledError(msg, err)
}
// DeadlineExceededError
switch {
case err == context.DeadlineExceeded:
fallthrough
case sOk && codes.DeadlineExceeded == sErr.GRPCStatus().Code():
fallthrough
case tiOk && tiErr.Timeout():
return NewDeadlineExceededError(msg, err)
}
// UnavailableError
if sOk && codes.Unavailable == sErr.GRPCStatus().Code() {
return NewUnavailableError(msg, err)
}
// UnknownError
switch {
case sOk && codes.Unknown == sErr.GRPCStatus().Code():
fallthrough
case teOk && teErr.Temporary():
return NewUnknownError(msg, err)
}
// InternalError
return NewInternalError(msg, err)
}