/
error_state.go
67 lines (53 loc) · 1.55 KB
/
error_state.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
/*
* UpdateHub
* Copyright (C) 2017
* O.S. Systems Sofware LTDA: contato@ossystems.com.br
*
* SPDX-License-Identifier: Apache-2.0
*/
package updatehub
import (
"errors"
"github.com/OSSystems/pkg/log"
"github.com/UpdateHub/updatehub/client"
"github.com/UpdateHub/updatehub/metadata"
)
// ErrorState is the State interface implementation for the UpdateHubStateError
type ErrorState struct {
BaseState
cause UpdateHubErrorReporter
ReportableState
updateMetadata *metadata.UpdateMetadata
}
// UpdateMetadata is the ReportableState interface implementation
func (state *ErrorState) UpdateMetadata() *metadata.UpdateMetadata {
return state.updateMetadata
}
// Handle for ErrorState calls "panic" if the error is fatal or
// triggers a poll state otherwise
func (state *ErrorState) Handle(uh *UpdateHub) (State, bool) {
log.Warn(state.cause)
if state.cause.IsFatal() {
return NewExitState(1), false
}
return NewIdleState(), false
}
// ToMap is for the State interface implementation
func (state *ErrorState) ToMap() map[string]interface{} {
m := state.BaseState.ToMap()
m["error"] = state.cause.Error()
return m
}
// NewErrorState creates a new ErrorState from a UpdateHubErrorReporter
func NewErrorState(apiClient *client.ApiClient, updateMetadata *metadata.UpdateMetadata, err UpdateHubErrorReporter) State {
if err == nil {
err = NewFatalError(errors.New("generic error"))
}
s := &ErrorState{
BaseState: BaseState{id: UpdateHubStateError},
cause: err,
updateMetadata: updateMetadata,
}
s.apiClient = apiClient
return s
}