Repository navigation
Expand file tree
/
Copy pathinspect.go
More file actions
144 lines (134 loc) · 3.46 KB
/
Copy pathinspect.go
File metadata and controls
144 lines (134 loc) · 3.46 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package errx
import (
"context"
"errors"
"time"
)
// As is a generic, one-line errors.As: pull the first error of type T out
// of the chain without pre-declaring a variable.
//
// if pe, ok := errx.As[*fs.PathError](err); ok { _ = pe.Path }
func As[T error](err error) (T, bool) {
var t T
if errors.As(err, &t) {
return t, true
}
return t, false
}
// Get returns the first *Error in the chain, or nil.
func Get(err error) *Error {
var e *Error
if errors.As(err, &e) {
return e
}
return nil
}
// Code returns the code of the first error in the chain carrying one —
// either an *errx.Error or any error exposing a `Code() string` method
// (so errxgen-generated and foreign typed errors are first-class without
// being errx.Error). Behavioral detection, segmentio-style.
func Code(err error) string {
for err != nil {
if e, ok := err.(*Error); ok && e.code != "" {
return e.code
}
if c, ok := err.(interface{ Code() string }); ok {
if v := c.Code(); v != "" {
return v
}
}
switch x := err.(type) {
case interface{ Unwrap() error }:
err = x.Unwrap()
case interface{ Unwrap() []error }:
for _, c := range x.Unwrap() {
if code := Code(c); code != "" {
return code
}
}
return ""
default:
return ""
}
}
return ""
}
// HasCode reports whether any *Error in the chain has the given code.
func HasCode(err error, code string) bool {
for err != nil {
if e, ok := err.(*Error); ok && e.code == code {
return true
}
if c, ok := err.(interface{ Code() string }); ok && c.Code() == code {
return true
}
switch x := err.(type) {
case interface{ Unwrap() error }:
err = x.Unwrap()
case interface{ Unwrap() []error }:
for _, c := range x.Unwrap() {
if HasCode(c, code) {
return true
}
}
return false
default:
return false
}
}
return false
}
// FindByCode returns the first *Error in the chain with the given code.
func FindByCode(err error, code string) *Error {
for err != nil {
if e, ok := err.(*Error); ok && e.code == code {
return e
}
switch x := err.(type) {
case interface{ Unwrap() error }:
err = x.Unwrap()
case interface{ Unwrap() []error }:
for _, c := range x.Unwrap() {
if hit := FindByCode(c, code); hit != nil {
return hit
}
}
return nil
default:
return nil
}
}
return nil
}
// ClassOf returns the class of the nearest *Error in the chain, or
// ClassCancelled if the chain contains context cancellation/deadline,
// else ClassExpected.
func ClassOf(err error) Class {
if e := Get(err); e != nil {
return e.class
}
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return ClassCancelled
}
return ClassExpected
}
// IsExpected / IsDefect / IsCancelled are convenience predicates over ClassOf.
func IsExpected(err error) bool { return ClassOf(err) == ClassExpected }
func IsDefect(err error) bool { return ClassOf(err) == ClassDefect }
func IsCancelled(err error) bool { return ClassOf(err) == ClassCancelled }
// IsRetryable reports whether the nearest *Error is marked retryable. It
// also treats context.DeadlineExceeded as non-retryable by default (the
// caller's deadline is gone), mirroring the AIP-194 stance.
func IsRetryable(err error) bool {
if e := Get(err); e != nil {
return e.retryable
}
return false
}
// RetryAfter returns the suggested retry delay from the nearest *Error.
func RetryAfter(err error) time.Duration {
if e := Get(err); e != nil {
return e.retryAfter
}
return 0
}