forked from AppsFlyer/go-sundheit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
67 lines (55 loc) · 1.66 KB
/
types.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 gosundheit
import (
"fmt"
"time"
"github.com/pkg/errors"
)
const (
maxExpectedChecks = 16
initialResultMsg = "didn't run yet"
// ValAllChecks is the value used for the check tags when tagging all tests
ValAllChecks = "all_checks"
)
// Result represents the output of a health check execution.
type Result struct {
// the details of task Result - may be nil
Details interface{} `json:"message,omitempty"`
// the error returned from a failed health check - nil when successful
Error error `json:"error,omitempty"`
// the time of the last health check
Timestamp time.Time `json:"timestamp"`
// the execution duration of the last check
Duration time.Duration `json:"duration,omitempty"`
// the number of failures that occurred in a row
ContiguousFailures int64 `json:"contiguousFailures"`
// the time of the initial transitional failure
TimeOfFirstFailure *time.Time `json:"timeOfFirstFailure"`
}
// IsHealthy returns true iff the check result snapshot was a success
func (r Result) IsHealthy() bool {
return r.Error == nil
}
func (r Result) String() string {
return fmt.Sprintf("Result{details: %s, err: %s, time: %s, contiguousFailures: %d, timeOfFirstFailure:%s}",
r.Details, r.Error, r.Timestamp, r.ContiguousFailures, r.TimeOfFirstFailure)
}
type marshalableError struct {
Message string `json:"message,omitempty"`
Cause error `json:"cause,omitempty"`
}
func newMarshalableError(err error) error {
if err == nil {
return nil
}
mr := &marshalableError{
Message: err.Error(),
}
cause := errors.Cause(err)
if cause != err {
mr.Cause = newMarshalableError(cause)
}
return mr
}
func (e *marshalableError) Error() string {
return e.Message
}