-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
118 lines (105 loc) · 2.81 KB
/
Copy patherror.go
File metadata and controls
118 lines (105 loc) · 2.81 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
package strut
import (
"errors"
"fmt"
"time"
"github.com/disgoorg/disgo/discord"
)
// ErrorKind classifies why an invocation failed. Sentinel values are
// comparable with errors.Is against an *Error.
type ErrorKind int
const (
ErrArgumentParse ErrorKind = iota
ErrValidation
ErrCheckFailed
ErrCooldown
ErrMissingUserPerms
ErrMissingBotPerms
ErrNotOwner
ErrGuildOnly
ErrDMOnly
ErrNSFWOnly
ErrCommandPanic
ErrCommandFailed
ErrSetup
ErrEventHandler
ErrModalTimeout
ErrNoInteraction
ErrShuttingDown
ErrSubcommandRequired
)
func (k ErrorKind) Error() string { return k.String() }
func (k ErrorKind) String() string {
switch k {
case ErrArgumentParse:
return "argument parse failed"
case ErrValidation:
return "input failed validation"
case ErrCheckFailed:
return "check failed"
case ErrCooldown:
return "on cooldown"
case ErrMissingUserPerms:
return "user is missing permissions"
case ErrMissingBotPerms:
return "bot is missing permissions"
case ErrNotOwner:
return "not a bot owner"
case ErrGuildOnly:
return "command is guild only"
case ErrDMOnly:
return "command is DM only"
case ErrNSFWOnly:
return "command is NSFW only"
case ErrCommandPanic:
return "command panicked"
case ErrCommandFailed:
return "command returned an error"
case ErrSetup:
return "setup failed"
case ErrEventHandler:
return "event handler failed"
case ErrModalTimeout:
return "modal was not submitted in time"
case ErrNoInteraction:
return "no interaction to respond to"
case ErrShuttingDown:
return "the bot is shutting down"
case ErrSubcommandRequired:
return "a subcommand is required"
}
return "unknown error"
}
// ErrNotYours is a convenience check failure for components clicked by
// someone other than the user they were created for.
var ErrNotYours = errors.New("strut: this component is not yours")
// Error is every failure strut reports, from argument parsing through to a
// command returning an error.
type Error[D any] struct {
Kind ErrorKind
Event *Event[D]
Err error
Input string // ErrArgumentParse: the text that failed to parse
Field string // ErrArgumentParse: the option it belongs to
Remaining time.Duration // ErrCooldown
Missing discord.Permissions // ErrMissingUserPerms, ErrMissingBotPerms
Stack []byte // ErrCommandPanic
}
func (e *Error[D]) Error() string {
name := "?"
if e.Event != nil {
name = e.Event.meta.Name
}
if e.Err != nil {
return fmt.Sprintf("strut: %s: %s: %v", name, e.Kind, e.Err)
}
return fmt.Sprintf("strut: %s: %s", name, e.Kind)
}
// Unwrap exposes the underlying cause, if any.
func (e *Error[D]) Unwrap() error { return e.Err }
// Is matches against an ErrorKind, so callers can write
// errors.Is(err, strut.ErrCooldown).
func (e *Error[D]) Is(target error) bool {
k, ok := target.(ErrorKind)
return ok && k == e.Kind
}