generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_recorder.go
68 lines (56 loc) · 1.49 KB
/
error_recorder.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
package cli
import (
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
// errorRecorder records errors that take place during ThelmaCommand execution and returns a RunError
type errorRecorder struct {
err *RunError
count int
}
func newErrorRecorder(key commandKey) *errorRecorder {
return &errorRecorder{
err: &RunError{
CommandName: key.description(),
},
count: 0,
}
}
func (r *errorRecorder) error() *RunError {
if !r.hasErrors() {
return nil
}
return r.err
}
func (r *errorRecorder) hasErrors() bool {
return r.count > 0
}
func (r *errorRecorder) setRunError(key commandKey, err error) {
r.recordError(key, runHook, err)
}
func (r *errorRecorder) setPreRunError(key commandKey, err error) {
r.recordError(key, preRunHook, err)
}
func (r *errorRecorder) addPostRunError(key commandKey, err error) {
r.recordError(key, postRunHook, err)
}
func (r *errorRecorder) recordError(key commandKey, hookType hookType, err error) {
hookErr := &HookError{
CommandName: key.description(),
Err: err,
hookType: hookType,
}
// Log error at debug level as it is recorded
log.Debug().Str("phase", hookType.String()).Str("command", key.description()).Err(err).Msgf("error executing hook: %s", err.Error())
switch hookType {
case preRunHook:
r.err.PreRunError = hookErr
case runHook:
r.err.RunError = hookErr
case postRunHook:
r.err.PostRunErrors = append(r.err.PostRunErrors, hookErr)
default:
panic(errors.Errorf("Unknown hook type: %v", err))
}
r.count++
}