This repository has been archived by the owner on Nov 23, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
99 lines (79 loc) · 1.88 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
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
// Copyright © 2019 Developer Network, LLC
//
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.
package engine
import (
"encoding/gob"
"fmt"
"strings"
)
func init() {
gob.Register(&Error{})
}
type wrappedErr interface {
Unwrap() error
}
func simple(msg string, internal error) error {
return &Error{
Event: &Event{
Message: msg,
},
Internal: internal,
}
}
// ptoe takes a result of a recover and coverts it
// to a string
func ptoe(r interface{}) error {
return &Error{
Event: makeEvent(ptos(r)),
}
}
// ptos takes a result of a recover and coverts it
// to a string
func ptos(r interface{}) string {
return fmt.Sprintf("%v", r)
}
// Error is an error type which provides specific
// atomizer information as part of an error
type Error struct {
// Event is the event that took place to create
// the error and contains metadata relevant to the error
Event *Event `json:"event"`
// Internal is the internal error
Internal error `json:"internal"`
}
func (e *Error) Error() string {
return e.String()
}
func (e *Error) String() string {
var fields []string
fields = append(fields, "atomizer error")
msg := e.Event.Event()
if msg != "" {
fields = append(fields, msg)
}
if e.Internal != nil {
fields = append(
fields,
"| internal: ("+e.Internal.Error()+")",
)
}
return strings.Join(fields, " ")
}
// Unwrap unwraps the error to the deepest error and returns that one
func (e *Error) Unwrap() (err error) {
err = e.Internal
// Determine if the internal error implements
// the wrappedErr interface then continue unwrapping
// if it does
if internal, ok := err.(wrappedErr); ok {
// Recursive unwrap to get the lowest error
err = internal.Unwrap()
}
return err
}
// Validate determines if this is a properly built error
func (e *Error) Validate() bool {
return e.Event.Validate()
}