-
Notifications
You must be signed in to change notification settings - Fork 4
/
errors.go
67 lines (56 loc) · 1.68 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
64
65
66
67
package cloudlog
import (
"errors"
"fmt"
"reflect"
)
var (
// ErrIndexNotDefined indicates that the target index has not been defined
ErrIndexNotDefined = errors.New("Target index is not defined")
)
// EventEncodingError indicates that an event could not be encoded
type EventEncodingError struct {
Message string
Event interface{}
}
func (e *EventEncodingError) Error() string {
return e.Message
}
// IsEventEncodingError checks if a supplied error is an EventEncodingError
// and returns a boolean flag and the event that caused the error
func IsEventEncodingError(err error) (ok bool, event interface{}) {
if e, ok := err.(*EventEncodingError); ok {
return true, e.Event
}
return false, nil
}
// NewUnsupportedEventType constructs a new EventEncodingError that indicates that the
// supplied event type is unsupported
func NewUnsupportedEventType(event interface{}) *EventEncodingError {
return &EventEncodingError{
Message: fmt.Sprintf("Cannot encode event, type %s is unsupported",
reflect.ValueOf(event).Type().String()),
Event: event,
}
}
// MarshalError represents a marshalling error
type MarshalError struct {
// EventMap contains the events data map
EventMap map[string]interface{}
// Parent contains the parent error
Parent error
}
func (e *MarshalError) Error() string {
return fmt.Sprintf("Marshal of event failed: %s", e.Parent.Error())
}
// WrappedErrors returns the wrapped parent error
func (e *MarshalError) WrappedErrors() []error {
return []error{e.Parent}
}
// NewMarshalError returns a new MarshalError
func NewMarshalError(eventMap map[string]interface{}, parent error) *MarshalError {
return &MarshalError{
EventMap: eventMap,
Parent: parent,
}
}