-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.go
More file actions
89 lines (80 loc) · 2.17 KB
/
Copy patherrors.go
File metadata and controls
89 lines (80 loc) · 2.17 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
package flow
import (
"fmt"
"strings"
"time"
"github.com/goware/flow/internal/durable"
"github.com/goware/flow/internal/failure"
"github.com/goware/flow/internal/flowerr"
)
var (
ErrNotFound = flowerr.ErrNotFound
ErrConflict = flowerr.ErrConflict
ErrInvalid = flowerr.ErrInvalid
ErrInvalidState = flowerr.ErrInvalidState
ErrTerminal = flowerr.ErrTerminal
ErrLeaseLost = flowerr.ErrLeaseLost
ErrPayloadTooLarge = flowerr.ErrPayloadTooLarge
ErrClosed = flowerr.ErrClosed
ErrSchema = flowerr.ErrSchema
)
// Error adds safe structured context to a sentinel category. Its fields must
// contain identifiers and bounded reasons only, never payloads, SQL, secrets,
// or lease tokens.
type Error struct {
Category error
Op string
Resource string
ID string
Reason string
}
func (e *Error) Error() string {
if e == nil {
return "<nil>"
}
parts := make([]string, 0, 4)
if e.Op != "" {
parts = append(parts, e.Op)
}
if e.Resource != "" {
parts = append(parts, e.Resource)
}
if e.ID != "" {
parts = append(parts, e.ID)
}
if e.Reason != "" {
parts = append(parts, e.Reason)
}
message := strings.Join(parts, ": ")
if e.Category == nil {
return message
}
if message == "" {
return e.Category.Error()
}
return fmt.Sprintf("%s: %s", e.Category, message)
}
func (e *Error) Unwrap() error {
if e == nil {
return nil
}
return e.Category
}
func newError(category error, op, resource, id, reason string) error {
return &Error{Category: category, Op: op, Resource: resource, ID: id, Reason: reason}
}
// NoRetry classifies an application error as terminal for the current command
// delivery, preventing it from being retried.
func NoRetry(err error) error { return failure.NoRetry(err) }
// RetryAfter classifies an error as retryable after a requested delay. The
// command's immutable retry bounds still apply.
func RetryAfter(delay time.Duration, err error) error {
if delay > 0 {
normalized, _, normalizeErr := durable.CeilMilliseconds("retry-after delay", delay)
if normalizeErr != nil {
return failure.NoRetry(normalizeErr)
}
delay = normalized
}
return failure.RetryAfter(delay, err)
}