@@ -35,6 +35,19 @@ func NewClient(gatewayURL *url.URL, auth ClientAuth, client *http.Client) *Clien
35
35
}
36
36
}
37
37
38
+ type HttpError struct {
39
+ Err error
40
+ Status int
41
+ }
42
+
43
+ func (e * HttpError ) Error () string {
44
+ return e .Err .Error ()
45
+ }
46
+
47
+ func createHttpError (err error , statusCode int ) * HttpError {
48
+ return & HttpError {Err : err , Status : statusCode }
49
+ }
50
+
38
51
// GetNamespaces get openfaas namespaces
39
52
func (s * Client ) GetNamespaces (ctx context.Context ) ([]string , error ) {
40
53
u := s .GatewayURL
@@ -54,7 +67,7 @@ func (s *Client) GetNamespaces(ctx context.Context) ([]string, error) {
54
67
55
68
res , err := s .Client .Do (req )
56
69
if err != nil {
57
- return namespaces , fmt .Errorf ("unable to make request: %w" , err )
70
+ return namespaces , fmt .Errorf ("unable to make HTTP request to OpenFaaS on URL : %s, error: %s" , s . GatewayURL , err )
58
71
}
59
72
60
73
if res .Body != nil {
@@ -66,8 +79,15 @@ func (s *Client) GetNamespaces(ctx context.Context) ([]string, error) {
66
79
return namespaces , err
67
80
}
68
81
69
- if res .StatusCode == http .StatusUnauthorized {
70
- return namespaces , fmt .Errorf ("check authorization, status code: %d" , res .StatusCode )
82
+ switch res .StatusCode {
83
+ case http .StatusOK :
84
+ break
85
+
86
+ case http .StatusUnauthorized :
87
+ return namespaces , createHttpError (fmt .Errorf ("unauthorized action, please setup authentication for this server" ), res .StatusCode )
88
+
89
+ default :
90
+ return namespaces , createHttpError (fmt .Errorf ("server returned unexpected status code %d, message: %q" , res .StatusCode , string (bytesOut )), res .StatusCode )
71
91
}
72
92
73
93
if len (bytesOut ) == 0 {
@@ -106,7 +126,7 @@ func (s *Client) GetFunctions(ctx context.Context, namespace string) ([]types.Fu
106
126
107
127
res , err := s .Client .Do (req )
108
128
if err != nil {
109
- return []types.FunctionStatus {}, fmt .Errorf ("unable to make HTTP request: %w" , err )
129
+ return []types.FunctionStatus {}, fmt .Errorf ("unable to make HTTP request to OpenFaaS on URL : %s, error: %s" , s . GatewayURL , err )
110
130
}
111
131
112
132
if res .Body != nil {
@@ -115,6 +135,17 @@ func (s *Client) GetFunctions(ctx context.Context, namespace string) ([]types.Fu
115
135
116
136
body , _ := io .ReadAll (res .Body )
117
137
138
+ switch res .StatusCode {
139
+ case http .StatusAccepted , http .StatusOK :
140
+ break
141
+
142
+ case http .StatusUnauthorized :
143
+ return nil , createHttpError (fmt .Errorf ("unauthorized action, please setup authentication for this server" ), res .StatusCode )
144
+
145
+ default :
146
+ return nil , createHttpError (fmt .Errorf ("server returned unexpected status code %d, message: %q" , res .StatusCode , string (body )), res .StatusCode )
147
+ }
148
+
118
149
functions := []types.FunctionStatus {}
119
150
if err := json .Unmarshal (body , & functions ); err != nil {
120
151
return []types.FunctionStatus {},
@@ -142,7 +173,7 @@ func (s *Client) GetInfo(ctx context.Context) (SystemInfo, error) {
142
173
143
174
res , err := s .Client .Do (req )
144
175
if err != nil {
145
- return SystemInfo {}, fmt .Errorf ("unable to make HTTP request: %w" , err )
176
+ return SystemInfo {}, fmt .Errorf ("unable to make HTTP request to OpenFaaS on URL : %s, error: %s" , s . GatewayURL , err )
146
177
}
147
178
148
179
if res .Body != nil {
@@ -185,7 +216,7 @@ func (s *Client) GetFunction(ctx context.Context, name, namespace string) (types
185
216
186
217
res , err := s .Client .Do (req )
187
218
if err != nil {
188
- return types.FunctionDeployment {}, fmt .Errorf ("unable to make HTTP request: %w" , err )
219
+ return types.FunctionDeployment {}, fmt .Errorf ("unable to make HTTP request to OpenFaaS on URL : %s, error: %s" , s . GatewayURL , err )
189
220
}
190
221
191
222
if res .Body != nil {
@@ -194,6 +225,17 @@ func (s *Client) GetFunction(ctx context.Context, name, namespace string) (types
194
225
195
226
body , _ := io .ReadAll (res .Body )
196
227
228
+ switch res .StatusCode {
229
+ case http .StatusOK :
230
+ break
231
+
232
+ case http .StatusUnauthorized :
233
+ return types.FunctionDeployment {}, createHttpError (fmt .Errorf ("unauthorized action, please setup authentication for this server" ), res .StatusCode )
234
+
235
+ default :
236
+ return types.FunctionDeployment {}, createHttpError (fmt .Errorf ("server returned unexpected status code %d, message: %q" , res .StatusCode , string (body )), res .StatusCode )
237
+ }
238
+
197
239
functions := types.FunctionDeployment {}
198
240
if err := json .Unmarshal (body , & functions ); err != nil {
199
241
return types.FunctionDeployment {},
@@ -203,20 +245,20 @@ func (s *Client) GetFunction(ctx context.Context, name, namespace string) (types
203
245
return functions , nil
204
246
}
205
247
206
- func (s * Client ) Deploy (ctx context.Context , spec types.FunctionDeployment ) (int , error ) {
248
+ // func (s *Client) deploy(ctx context.Context, method string, spec types.FunctionDeployment) (int, error) {
249
+ func (s * Client ) Deploy (ctx context.Context , spec types.FunctionDeployment ) error {
207
250
return s .deploy (ctx , http .MethodPost , spec )
208
-
209
251
}
210
252
211
- func (s * Client ) Update (ctx context.Context , spec types.FunctionDeployment ) ( int , error ) {
253
+ func (s * Client ) Update (ctx context.Context , spec types.FunctionDeployment ) error {
212
254
return s .deploy (ctx , http .MethodPut , spec )
213
255
}
214
256
215
- func (s * Client ) deploy (ctx context.Context , method string , spec types.FunctionDeployment ) ( int , error ) {
257
+ func (s * Client ) deploy (ctx context.Context , method string , spec types.FunctionDeployment ) error {
216
258
217
259
bodyBytes , err := json .Marshal (spec )
218
260
if err != nil {
219
- return http .StatusBadRequest , err
261
+ return createHttpError ( err , http .StatusBadRequest )
220
262
}
221
263
222
264
bodyReader := bytes .NewReader (bodyBytes )
@@ -226,18 +268,18 @@ func (s *Client) deploy(ctx context.Context, method string, spec types.FunctionD
226
268
227
269
req , err := http .NewRequestWithContext (ctx , method , u .String (), bodyReader )
228
270
if err != nil {
229
- return http . StatusBadGateway , err
271
+ return fmt . Errorf ( "unable to create request for %s, error: %w" , u . String (), err )
230
272
}
231
273
232
274
if s .ClientAuth != nil {
233
275
if err := s .ClientAuth .Set (req ); err != nil {
234
- return http . StatusInternalServerError , fmt .Errorf ("unable to set Authorization header: %w" , err )
276
+ return fmt .Errorf ("unable to set Authorization header: %w" , err )
235
277
}
236
278
}
237
279
238
280
res , err := s .Client .Do (req )
239
281
if err != nil {
240
- return http . StatusBadGateway , err
282
+ return fmt . Errorf ( "unable to make HTTP request to OpenFaaS on URL: %s, error: %s" , s . GatewayURL , err )
241
283
}
242
284
243
285
var body []byte
@@ -248,13 +290,13 @@ func (s *Client) deploy(ctx context.Context, method string, spec types.FunctionD
248
290
249
291
switch res .StatusCode {
250
292
case http .StatusAccepted , http .StatusOK , http .StatusCreated :
251
- return res . StatusCode , nil
293
+ return nil
252
294
253
295
case http .StatusUnauthorized :
254
- return res . StatusCode , fmt .Errorf ("unauthorized action, please setup authentication for this server" )
296
+ return createHttpError ( fmt .Errorf ("unauthorized action, please setup authentication for this server" ), res . StatusCode )
255
297
256
298
default :
257
- return res . StatusCode , fmt .Errorf ("unexpected status code: %d, message: %q" , res .StatusCode , string (body ))
299
+ return createHttpError ( fmt .Errorf ("unexpected status code: %d, message: %q" , res .StatusCode , string (body )), res . StatusCode )
258
300
}
259
301
}
260
302
@@ -280,7 +322,7 @@ func (s *Client) ScaleFunction(ctx context.Context, functionName, namespace stri
280
322
281
323
req , err := http .NewRequestWithContext (ctx , http .MethodPost , u .String (), bodyReader )
282
324
if err != nil {
283
- return fmt .Errorf ("cannot connect to OpenFaaS on URL: %s, error: %s " , u .String (), err )
325
+ return fmt .Errorf ("unable to create request for %s, error: %w " , u .String (), err )
284
326
}
285
327
286
328
if s .ClientAuth != nil {
@@ -290,7 +332,7 @@ func (s *Client) ScaleFunction(ctx context.Context, functionName, namespace stri
290
332
}
291
333
res , err := http .DefaultClient .Do (req )
292
334
if err != nil {
293
- return fmt .Errorf ("cannot connect to OpenFaaS on URL: %s, error: %s" , s .GatewayURL , err )
335
+ return fmt .Errorf ("unable to make HTTP request to OpenFaaS on URL: %s, error: %s" , s .GatewayURL , err )
294
336
295
337
}
296
338
@@ -303,10 +345,10 @@ func (s *Client) ScaleFunction(ctx context.Context, functionName, namespace stri
303
345
break
304
346
305
347
case http .StatusNotFound :
306
- return fmt .Errorf ("function %s not found" , functionName )
348
+ return createHttpError ( fmt .Errorf ("function %s not found" , functionName ), res . StatusCode )
307
349
308
350
case http .StatusUnauthorized :
309
- return fmt .Errorf ("unauthorized action, please setup authentication for this server" )
351
+ return createHttpError ( fmt .Errorf ("unauthorized action, please setup authentication for this server" ), res . StatusCode )
310
352
311
353
default :
312
354
var err error
@@ -315,7 +357,7 @@ func (s *Client) ScaleFunction(ctx context.Context, functionName, namespace stri
315
357
return err
316
358
}
317
359
318
- return fmt .Errorf ("server returned unexpected status code %d, message: %q" , res .StatusCode , string (bytesOut ))
360
+ return createHttpError ( fmt .Errorf ("server returned unexpected status code %d, message: %q" , res .StatusCode , string (bytesOut )), res . StatusCode )
319
361
}
320
362
return nil
321
363
}
@@ -338,7 +380,7 @@ func (s *Client) DeleteFunction(ctx context.Context, functionName, namespace str
338
380
339
381
req , err := http .NewRequestWithContext (ctx , http .MethodDelete , u .String (), bodyReader )
340
382
if err != nil {
341
- return fmt .Errorf ("cannot connect to OpenFaaS on URL: %s, error: %s " , u .String (), err )
383
+ return fmt .Errorf ("unable to create request for %s, error: %w " , u .String (), err )
342
384
}
343
385
344
386
if s .ClientAuth != nil {
@@ -348,7 +390,7 @@ func (s *Client) DeleteFunction(ctx context.Context, functionName, namespace str
348
390
}
349
391
res , err := http .DefaultClient .Do (req )
350
392
if err != nil {
351
- return fmt .Errorf ("cannot connect to OpenFaaS on URL: %s, error: %s" , s .GatewayURL , err )
393
+ return fmt .Errorf ("unable to make HTTP request to OpenFaaS on URL: %s, error: %s" , s .GatewayURL , err )
352
394
353
395
}
354
396
@@ -361,10 +403,10 @@ func (s *Client) DeleteFunction(ctx context.Context, functionName, namespace str
361
403
break
362
404
363
405
case http .StatusNotFound :
364
- return fmt .Errorf ("function %s not found" , functionName )
406
+ return createHttpError ( fmt .Errorf ("function %s not found" , functionName ), res . StatusCode )
365
407
366
408
case http .StatusUnauthorized :
367
- return fmt .Errorf ("unauthorized action, please setup authentication for this server" )
409
+ return createHttpError ( fmt .Errorf ("unauthorized action, please setup authentication for this server" ), res . StatusCode )
368
410
369
411
default :
370
412
var err error
@@ -373,7 +415,7 @@ func (s *Client) DeleteFunction(ctx context.Context, functionName, namespace str
373
415
return err
374
416
}
375
417
376
- return fmt .Errorf ("server returned unexpected status code %d, message: %q" , res .StatusCode , string (bytesOut ))
418
+ return createHttpError ( fmt .Errorf ("server returned unexpected status code %d, message: %q" , res .StatusCode , string (bytesOut )), res . StatusCode )
377
419
}
378
420
return nil
379
421
}
0 commit comments