-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherrors.go
63 lines (53 loc) · 1.62 KB
/
errors.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
57
58
59
60
61
62
63
package fwew_lib
import (
"fmt"
"strings"
)
// Errors raised by package x.
const (
// cache
DictionaryNotFound = constError("no dictionary found")
// numbers
NegativeNumber = constError("negative numbers not allowed")
NumberTooBig = constError("number too big")
NoTranslationFound = constError("no translation found")
// list
InvalidNumber = constError("invalidNumericError")
NoResults = constError("noResultsError")
)
// errors are basically strings, that implement the error interface
type constError string
// Implement error interface, so this is a valid error.
func (err constError) Error() string {
return string(err)
}
// Implement newer Is method to check if wrapped error is the desired error.
func (err constError) Is(target error) bool {
targetError := target.Error()
errorString := string(err)
return targetError == errorString || strings.HasPrefix(targetError, errorString+": ")
}
// wrap suberror with this error. `Is` can be checked if wrapped errors is of type
func (err constError) wrap(inner error) error {
return wrapError{msg: string(err), err: inner}
}
// If an error is wrapped, we change the type to this
type wrapError struct {
err error
msg string
}
// Also implement Error interface, to use wrapErrors as error
func (err wrapError) Error() string {
if err.err != nil {
return fmt.Sprintf("%s: %v", err.msg, err.err)
}
return err.msg
}
// Make it possible to Unwrap a wrapped error again.
func (err wrapError) Unwrap() error {
return err.err
}
// Implement newer Is method to check unwrapped error
func (err wrapError) Is(target error) bool {
return constError(err.msg).Is(target)
}