-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
80 lines (64 loc) · 1.47 KB
/
string.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package errs
import (
"errors"
"fmt"
)
// StringErrorは指定された文字列をエラーメッセージとして扱います.
type StringError string
// Error returns error message
func (s StringError) Error() string {
return string(s)
}
// New creates *errs.Error that wraps StringError.
func (s StringError) New() *Error {
err := newError("", false)
err.cause = s
return err
}
// Wrap creates *errs.Error that wraps StringError and cause.
func (s StringError) Wrap(cause error) *Error {
if cause == nil {
panic("nil error")
}
wrap := &stringError{
strErr: s,
cause: cause,
}
err := newError("", haveStackTrace(cause))
err.cause = wrap
return err
}
type stringError struct {
strErr StringError
cause error
}
// Error returns error message
func (s stringError) Error() string {
return fmt.Sprintf("%s: %v", s.strErr, s.cause.Error())
}
// Unwrap returns errors that cause
func (s stringError) Unwrap() error {
return s.cause
}
// Is follows the specification of errors.Is.
func (s stringError) Is(target error) bool {
switch x := target.(type) { // nolint:errorlint
case StringError:
if string(s.strErr) == string(x) {
return true
}
case *StringError:
if x != nil && string(s.strErr) == string(*x) {
return true
}
}
return errors.Is(s.cause, target)
}
// As follows the specification of errors.As.
func (s stringError) As(target interface{}) bool {
if x, ok := target.(*StringError); ok {
*x = s.strErr
return true
}
return false
}