-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprotocol_error.go
More file actions
102 lines (89 loc) · 2.4 KB
/
Copy pathprotocol_error.go
File metadata and controls
102 lines (89 loc) · 2.4 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
package main
import (
"errors"
"fmt"
)
type protocolErrorKind string
const (
protocolInvalidInput protocolErrorKind = "invalid-input"
protocolAuthUnavailable protocolErrorKind = "auth-unavailable"
protocolBackendIncompatible protocolErrorKind = "backend-incompatible"
protocolEntropyFailure protocolErrorKind = "entropy-failure"
protocolBackendFailure protocolErrorKind = "backend-failure"
protocolContextClosed protocolErrorKind = "context-closed"
)
type protocolError struct {
kind protocolErrorKind
public string
internal error
}
func (e *protocolError) Error() string {
if e == nil {
return "Qoder protocol operation failed"
}
return e.public
}
func (e *protocolError) Unwrap() error {
if e == nil {
return nil
}
return e.internal
}
func newProtocolError(kind protocolErrorKind, public string, internal error) error {
return &protocolError{kind: kind, public: public, internal: internal}
}
func protocolErrorKindOf(err error) protocolErrorKind {
if target := protocolErrorFrom(err); target != nil {
return target.kind
}
return protocolBackendFailure
}
func protocolInternalError(err error) error {
if target := protocolErrorFrom(err); target != nil {
return target.internal
}
return err
}
func protocolDiagnosticError(err error) error {
if err == nil {
return nil
}
internal := protocolInternalError(err)
if internal == nil || internal == err {
return err
}
return fmt.Errorf("%s: %w", err.Error(), internal)
}
func protocolErrorFrom(err error) *protocolError {
var target *protocolError
if errors.As(err, &target) && target != nil {
return target
}
return nil
}
func normalizeHostDependencyError(err error, fallbackKind protocolErrorKind, fallbackPublic, action string) error {
if protocolErrorFrom(err) != nil {
return err
}
return newProtocolError(fallbackKind, fallbackPublic, fmt.Errorf("%s: %w", action, err))
}
func mergeProtocolErrors(primary, secondary error) error {
if primary == nil {
return secondary
}
if secondary == nil {
return primary
}
if primaryProtocol := protocolErrorFrom(primary); primaryProtocol != nil {
return newProtocolError(
primaryProtocol.kind,
primaryProtocol.public,
errors.Join(protocolInternalError(primary), protocolInternalError(secondary)),
)
}
return newProtocolError(
protocolBackendFailure,
"Qoder protocol operation failed",
errors.Join(primary, protocolInternalError(secondary)),
)
}