...
1 package apierror
2
3 import (
4 "context"
5 "errors"
6
7 "edge-infra.dev/pkg/lib/fog"
8
9 "edge-infra.dev/pkg/lib/runtime/version"
10
11 "github.com/99designs/gqlgen/graphql"
12 "github.com/vektah/gqlparser/v2/ast"
13 )
14
15 const (
16
17 OperationIDKey = "operationID"
18
19 VersionKey = "version"
20
21 AdditionalKey = "additional"
22
23 BannerCheckKey = "bannerCheckDetails"
24
25 SiteCheckKey = "siteCheckDetails"
26
27 DeleteClustersKey = "deleteClusterDetails"
28
29 BannerCheckMessage = "Mismatches in bsl and sql banners"
30
31 SiteCheckMessage = "Mismatches in bsl and bsl-info config map site"
32
33 DeleteClustersMessage = "DeleteClustersErrors"
34
35 HelmChartError = "Error getting helm chart information"
36
37 DetailedHelmError = "helmApiErrors"
38
39 CreateChannelIAMPolicyError = "CreateChannelIAMPolicyErrors"
40 CreateChannelIAMPolicyErrorMessage = "there was an error creating channel iam policy member for: %s"
41
42
43 GetChannelsAPIError = "GetChannelsAPIError"
44 GetChannelsAPIErrorMsg = "there was an error getting list of channels"
45 GetChannelAPIErrorMsg = "there was an error getting channel: %s"
46
47
48 CreateChannelAPIError = "CreateChannelAPIError"
49 CreateChannelAPIErrorMsg = "there was an error creating channel: %s"
50
51
52 ChannelRotateError = "ChannelRotateError"
53 ChannelRotateErrorMsg = "failed to fetch new rotated channel: %s"
54 )
55
56
57 type Error struct {
58
59 Err error `json:"-"`
60
61 path ast.Path `json:"-"`
62
63 Message string `json:"message"`
64
65
66 OperationID string `json:"operationID"`
67
68 Version string `json:"version"`
69
70 Ext map[string]interface{} `json:"Extensions"`
71 }
72
73
74 func Wrap(err error) *Error {
75 if err == nil {
76 return nil
77 }
78 return &Error{
79 Err: err,
80 Message: err.Error(),
81 Version: version.New().SemVer,
82 Ext: make(map[string]interface{}),
83 }
84 }
85
86
87
88
89
90 func New(msg string) *Error {
91 return Wrap(errors.New(msg))
92 }
93
94
95 func (g *Error) Error() string {
96 return g.Message
97 }
98
99
100 func (g *Error) Unwrap() error {
101 if g == nil {
102 return nil
103 }
104 return g.Err
105 }
106
107
108 func (g *Error) SetPath(ctx context.Context) *Error {
109 g.path = graphql.GetPath(ctx)
110 return g
111 }
112
113
114 func (g *Error) SetMessage(msg string) *Error {
115 g.Message = msg
116 return g
117 }
118
119
120 func (g *Error) SetOperationID(ctx context.Context) *Error {
121 g.OperationID = fog.OperationID(ctx)
122 return g
123 }
124
125
126 func (g *Error) SetVersion(Version string) *Error {
127 g.Version = Version
128 return g
129 }
130
131
132 func (g *Error) SetGenericErrorExtension(exts map[string]interface{}) *Error {
133 g.Ext = exts
134 return g
135 }
136
137
138 func (g *Error) AddGenericErrorExtension(key string, value interface{}) *Error {
139 if g.Ext == nil {
140 g.Ext = map[string]interface{}{}
141 }
142 g.Ext[key] = value
143 return g
144 }
145
146
147 func (g *Error) GetGenericErrorExtension(key string) interface{} {
148 return g.Ext[key]
149 }
150
151
152 func (g *Error) Extensions() map[string]interface{} {
153 additional := make(map[string]interface{})
154 for key, value := range g.Ext {
155 if key != AdditionalKey && key != VersionKey {
156 additional[key] = value
157 }
158 }
159 return additional
160 }
161
View as plain text