-
Notifications
You must be signed in to change notification settings - Fork 20
/
error.go
67 lines (50 loc) · 1.66 KB
/
error.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
package types
import (
"encoding/json"
"fmt"
"github.com/nyaruka/goflow/envs"
)
// XError is an error
type XError interface {
error
XValue
Equals(XError) bool
}
type xerror struct {
native error
}
// NewXError creates a new XError
func NewXError(err error) XError {
return xerror{native: err}
}
// NewXErrorf creates a new XError
func NewXErrorf(format string, a ...interface{}) XError {
return NewXError(fmt.Errorf(format, a...))
}
// Describe returns a representation of this type for error messages
func (x xerror) Describe() string { return "error" }
// Truthy determines truthiness for this type
func (x xerror) Truthy() bool { return false }
// Render returns the canonical text representation
func (x xerror) Render() string { return x.Native().Error() }
// Format returns the pretty text representation
func (x xerror) Format(env envs.Environment) string { return "" }
// MarshalJSON converts this type to JSON
func (x xerror) MarshalJSON() ([]byte, error) { return json.Marshal(nil) }
// String returns the native string representation of this type for debugging
func (x xerror) String() string { return `XError("` + x.Native().Error() + `")` }
// Native returns the native value of this type
func (x xerror) Native() error { return x.native }
func (x xerror) Error() string { return x.Native().Error() }
// Equals determines equality for this type
func (x xerror) Equals(other XError) bool {
return x.String() == other.String()
}
// NilXError is the nil error value
var NilXError = NewXError(nil)
var _ XError = NilXError
// IsXError returns whether the given value is an error value
func IsXError(x XValue) bool {
_, isError := x.(XError)
return isError
}