-
Notifications
You must be signed in to change notification settings - Fork 263
/
error_and_warnings.go
73 lines (61 loc) · 1.53 KB
/
error_and_warnings.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
69
70
71
72
73
package error_helpers
import (
"github.com/hashicorp/hcl/v2"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/sperr"
)
type ErrorAndWarnings struct {
Error error
Warnings []string
}
func DiagsToErrorsAndWarnings(errPrefix string, diags hcl.Diagnostics) *ErrorAndWarnings {
return NewErrorsAndWarning(
plugin.DiagsToError(errPrefix, diags),
plugin.DiagsToWarnings(diags)...,
)
}
func EmptyErrorsAndWarning() *ErrorAndWarnings {
return NewErrorsAndWarning(nil)
}
func NewErrorsAndWarning(err error, warnings ...string) *ErrorAndWarnings {
return &ErrorAndWarnings{
Error: err, Warnings: warnings,
}
}
func (r *ErrorAndWarnings) WrapErrorWithMessage(msg string) *ErrorAndWarnings {
if r.Error != nil {
r.Error = sperr.WrapWithMessage(r.Error, msg)
}
return r
}
func (r *ErrorAndWarnings) AddWarning(warnings ...string) {
r.Warnings = append(r.Warnings, warnings...)
}
func (r *ErrorAndWarnings) ShowWarnings() {
for _, w := range r.Warnings {
ShowWarning(w)
}
}
func (r *ErrorAndWarnings) GetError() error {
if r == nil {
return nil
}
return r.Error
}
func (r *ErrorAndWarnings) Merge(other *ErrorAndWarnings) *ErrorAndWarnings {
if other == nil {
return r
}
// TODO: Restructure ErrorsAndWarning
// [issue](https://github.com/turbot/steampipe/issues/3845)
if r.Error == nil {
r.Error = other.Error
}
if len(other.Warnings) > 0 {
r.AddWarning(other.Warnings...)
}
return r
}
func (r *ErrorAndWarnings) Empty() bool {
return r.Error == nil && len(r.Warnings) == 0
}