-
Notifications
You must be signed in to change notification settings - Fork 245
/
termination.go
66 lines (56 loc) · 2.09 KB
/
termination.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
package spiceerrors
import "time"
// TerminationError represents an error that captures contextual information to make
// available on process termination. The error will be marshalled as JSON and
// serialized into a file-path specified via CLI arguments
type TerminationError struct {
error
Component string `json:"component"`
Timestamp time.Time `json:"timestamp"`
ErrorString string `json:"error"`
Metadata map[string]string `json:"metadata"`
exitCode int
}
// ExitCode returns the exit code to be returned on process termination
func (e TerminationError) ExitCode() int {
return e.exitCode
}
// ErrorBuilder is a fluent-style builder for TerminationError
type ErrorBuilder struct {
terminationErr TerminationError
}
// TerminationError returns the built termination TerminationError
func (eb *ErrorBuilder) Error() TerminationError {
return eb.terminationErr
}
// Component specifies the component in SpiceDB that
func (eb *ErrorBuilder) Component(component string) *ErrorBuilder {
eb.terminationErr.Component = component
return eb
}
// Metadata adds a new key-value pair of metadata to the termination TerminationError being built
func (eb *ErrorBuilder) Metadata(key, value string) *ErrorBuilder {
eb.terminationErr.Metadata[key] = value
return eb
}
// ExitCode defines the ExitCode to be used upon process termination. Defaults to 1 if not specified.
func (eb *ErrorBuilder) ExitCode(exitCode int) *ErrorBuilder {
eb.terminationErr.exitCode = exitCode
return eb
}
// Timestamp defines the time of the error. Defaults to time.Now().UTC() if not specified.
func (eb *ErrorBuilder) Timestamp(timestamp time.Time) *ErrorBuilder {
eb.terminationErr.Timestamp = timestamp
return eb
}
// NewTerminationErrorBuilder returns a new ErrorBuilder for a termination.TerminationError.
func NewTerminationErrorBuilder(err error) *ErrorBuilder {
return &ErrorBuilder{terminationErr: TerminationError{
error: err,
Component: "unspecified",
Timestamp: time.Now().UTC(),
ErrorString: err.Error(),
Metadata: make(map[string]string, 0),
exitCode: 1,
}}
}