-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrs.go
56 lines (46 loc) · 1.41 KB
/
errs.go
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
package gostr
import "fmt"
// AlphabetLookupError are errors that occur if you look up a character
// that is not in the alphabet.
type AlphabetLookupError struct {
char byte
}
// Error implements the interface for errors.
func (err *AlphabetLookupError) Error() string {
return fmt.Sprintf("byte %c is not in alphabet", err.char)
}
// InvalidCigar are errors when you use a cigar that isn't in the right format
type InvalidCigar struct {
x string
}
// NewInvalidCigar creates an InvalidCigar error
func NewInvalidCigar(x string) *InvalidCigar {
return &InvalidCigar{x: x}
}
// Error implements the interface for errors.
func (err *InvalidCigar) Error() string {
return fmt.Sprintf("invalid cigar: %s", err.x)
}
// Is implements the Is interface for errors.
func (err *InvalidCigar) Is(other error) bool {
if ic, ok := other.(*InvalidCigar); ok {
return ic.x == err.x
}
return false
}
// wrap around calls that can cause an error, to turn the
// error into a panic that you can capture with catchError.
func checkError(err error) {
if err != nil {
panic(err)
}
}
// This recovers from an error panic and returns the error.
// That way, you can set the error in a defer function and
// you don't have to check errors everywhere in a sequence
// of statements.
func catchError(err *error) { //nolint:gocritic // no, err should definitely be a reference here
if received, ok := recover().(error); ok {
*err = received
}
}