Repository navigation
Expand file tree
/
Copy pathinspect.go
More file actions
59 lines (53 loc) · 1.51 KB
/
Copy pathinspect.go
File metadata and controls
59 lines (53 loc) · 1.51 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
package errors
import (
"context"
"maps"
)
// inspect selects one frame, stopping before independent aggregate branches.
func inspect(err error) (*Error, Kind, bool) {
for err != nil {
switch e := err.(type) {
case *Error:
if e == nil {
return nil, Internal, false
}
return e, e.kind, e.kind.valid()
case interface{ Unwrap() []error }:
return nil, Internal, false
}
switch err {
case context.Canceled:
return nil, Canceled, true
case context.DeadlineExceeded:
return nil, Timeout, true
}
wrapped, ok := err.(interface{ Unwrap() error })
if !ok {
break
}
err = wrapped.Unwrap()
}
return nil, Internal, false
}
// KindOf separates recognized classification from presentation fallback.
func KindOf(err error) (Kind, bool) { _, kind, ok := inspect(err); return kind, ok }
// DiagnosticTypeOf reads the selected frame's private type, including Internal.
// Use TypeOf/Public at a response boundary; this accessor does not redact it.
func DiagnosticTypeOf(err error) string {
e, _, _ := inspect(err)
if e == nil {
return ""
}
return e.typ
}
// IsKind is false for success and unclassified failures, including for Internal.
func IsKind(err error, kind Kind) bool { actual, ok := KindOf(err); return ok && actual == kind }
// DetailsOf copies only the selected private frame, including for Internal.
// It does not flatten inner causes or aggregate branches.
func DetailsOf(err error) map[string]string {
e, _, _ := inspect(err)
if e == nil {
return nil
}
return maps.Clone(e.details)
}