-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
160 lines (144 loc) · 4.39 KB
/
Copy patherrors.go
File metadata and controls
160 lines (144 loc) · 4.39 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package gor
import (
"context"
"errors"
"github.com/suraciii/gor/cluster"
"github.com/suraciii/gor/internal/mail"
runtimepkg "github.com/suraciii/gor/internal/runtime"
"github.com/suraciii/gor/store"
)
// Code is the stable identity of an application or gor framework error.
// A Code is also an error and can be matched with errors.Is.
type Code string
// Error returns the code's diagnostic text.
func (c Code) Error() string {
return string(c)
}
// Code returns c.
func (c Code) Code() Code {
return c
}
// Is reports whether target is the same Code as c.
func (c Code) Is(target error) bool {
other, ok := target.(Code)
return ok && c == other
}
// Coded exposes the stable Code carried by an error.
type Coded interface {
Code() Code
}
// CodeOf reports the single Code reachable from err. It traverses err the way
// errors.Is does—the error itself, the single-value Unwrap chain, and every
// branch of a multi-value Unwrap—collecting every reachable Code. Exactly one
// reachable Code is the error's determined Code; none, or more than one, means
// the error has no determined Code and crosses the network as opaque text. As
// with errors.Is, a cyclic Unwrap is not handled.
func CodeOf(err error) (Code, bool) {
var (
codes map[Code]struct{}
walk func(error)
)
walk = func(e error) {
if e == nil {
return
}
if coded, ok := e.(Coded); ok {
if codes == nil {
codes = map[Code]struct{}{}
}
codes[coded.Code()] = struct{}{}
}
switch u := e.(type) {
case interface{ Unwrap() error }:
walk(u.Unwrap())
case interface{ Unwrap() []error }:
for _, sub := range u.Unwrap() {
walk(sub)
}
}
}
walk(err)
if len(codes) != 1 {
return "", false
}
var sole Code
for c := range codes {
sole = c
}
return sole, true
}
// The framework Code values are a closed set. Applications must declare codes
// under an owner other than gor.
const (
ErrNoOwner Code = "gor.no_owner"
ErrNodeDead Code = "gor.node_dead"
ErrRuntimeNotStarted Code = "gor.runtime_not_started"
ErrRuntimeClosed Code = "gor.runtime_closed"
ErrSetupFrozen Code = "gor.setup_frozen"
ErrInvalidSetup Code = "gor.invalid_setup"
ErrOverloaded Code = "gor.overloaded"
ErrTypeNotInstalled Code = "gor.type_not_installed"
ErrUnknownMethod Code = "gor.unknown_method"
ErrInvalidRequest Code = "gor.invalid_request"
ErrPersistenceConflict Code = "gor.persistence_conflict"
ErrPersistenceFailed Code = "gor.persistence_failed"
ErrPanic Code = "gor.panic"
ErrRequestEncodeFailed Code = "gor.request_encode_failed"
ErrReplyEncodeFailed Code = "gor.reply_encode_failed"
ErrTransportFailed Code = "gor.transport_failed"
// ErrCallCycle reports that a Call targeted a Grain that the same Call
// chain already occupies, so the Call could never start. The error text
// names the Grains in the cycle. The cycle is detected at delivery, not
// inferred from elapsed time: a slow call that is not a cycle still times
// out as a plain timeout.
ErrCallCycle Code = "gor.call_cycle"
)
type codedError struct {
code Code
err error
}
func (e codedError) Error() string {
if e.err == nil {
return e.code.Error()
}
return e.err.Error()
}
func (e codedError) Code() Code {
return e.code
}
func (e codedError) Is(target error) bool {
other, ok := target.(Code)
return ok && e.code == other
}
func (e codedError) Unwrap() error {
return e.err
}
func withCode(code Code, err error) error {
return codedError{code: code, err: err}
}
func publicError(err error) error {
if err == nil || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return err
}
if _, ok := CodeOf(err); ok {
return err
}
switch {
case errors.Is(err, cluster.ErrNodeDead):
return withCode(ErrNodeDead, err)
case errors.Is(err, runtimepkg.ErrRuntimeClosed), errors.Is(err, mail.ErrClosed):
return withCode(ErrRuntimeClosed, err)
case errors.Is(err, mail.ErrOverloaded), errors.Is(err, runtimepkg.ErrActivationLimit):
return withCode(ErrOverloaded, err)
case errors.Is(err, runtimepkg.ErrTypeNotRegistered):
return withCode(ErrTypeNotInstalled, err)
case errors.Is(err, store.ErrConflict):
return withCode(ErrPersistenceConflict, err)
case errors.Is(err, runtimepkg.ErrPanic):
return withCode(ErrPanic, err)
case errors.Is(err, runtimepkg.ErrCallCycle):
return withCode(ErrCallCycle, err)
default:
return err
}
}