@@ -35,6 +35,19 @@ func NewClient(gatewayURL *url.URL, auth ClientAuth, client *http.Client) *Clien
3535 }
3636}
3737
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+
3851// GetNamespaces get openfaas namespaces
3952func (s * Client ) GetNamespaces (ctx context.Context ) ([]string , error ) {
4053 u := s .GatewayURL
@@ -54,7 +67,7 @@ func (s *Client) GetNamespaces(ctx context.Context) ([]string, error) {
5467
5568 res , err := s .Client .Do (req )
5669 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 )
5871 }
5972
6073 if res .Body != nil {
@@ -66,8 +79,15 @@ func (s *Client) GetNamespaces(ctx context.Context) ([]string, error) {
6679 return namespaces , err
6780 }
6881
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 )
7191 }
7292
7393 if len (bytesOut ) == 0 {
@@ -106,7 +126,7 @@ func (s *Client) GetFunctions(ctx context.Context, namespace string) ([]types.Fu
106126
107127 res , err := s .Client .Do (req )
108128 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 )
110130 }
111131
112132 if res .Body != nil {
@@ -115,6 +135,17 @@ func (s *Client) GetFunctions(ctx context.Context, namespace string) ([]types.Fu
115135
116136 body , _ := io .ReadAll (res .Body )
117137
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+
118149 functions := []types.FunctionStatus {}
119150 if err := json .Unmarshal (body , & functions ); err != nil {
120151 return []types.FunctionStatus {},
@@ -142,7 +173,7 @@ func (s *Client) GetInfo(ctx context.Context) (SystemInfo, error) {
142173
143174 res , err := s .Client .Do (req )
144175 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 )
146177 }
147178
148179 if res .Body != nil {
@@ -185,7 +216,7 @@ func (s *Client) GetFunction(ctx context.Context, name, namespace string) (types
185216
186217 res , err := s .Client .Do (req )
187218 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 )
189220 }
190221
191222 if res .Body != nil {
@@ -194,6 +225,17 @@ func (s *Client) GetFunction(ctx context.Context, name, namespace string) (types
194225
195226 body , _ := io .ReadAll (res .Body )
196227
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+
197239 functions := types.FunctionDeployment {}
198240 if err := json .Unmarshal (body , & functions ); err != nil {
199241 return types.FunctionDeployment {},
@@ -203,20 +245,30 @@ func (s *Client) GetFunction(ctx context.Context, name, namespace string) (types
203245 return functions , nil
204246}
205247
206- func (s * Client ) Deploy (ctx context.Context , spec types.FunctionDeployment ) (int , error ) {
207- return s .deploy (ctx , http .MethodPost , spec )
248+ // func (s *Client) Deploy(ctx context.Context, spec types.FunctionDeployment) (int, error) {
249+ // return s.deploy(ctx, http.MethodPost, spec)
208250
209- }
251+ // }
210252
211- func (s * Client ) Update (ctx context.Context , spec types.FunctionDeployment ) (int , error ) {
253+ // func (s *Client) Update(ctx context.Context, spec types.FunctionDeployment) (int, error) {
254+ // return s.deploy(ctx, http.MethodPut, spec)
255+ // }
256+
257+ // func (s *Client) deploy(ctx context.Context, method string, spec types.FunctionDeployment) (int, error) {
258+ func (s * Client ) Deploy (ctx context.Context , spec types.FunctionDeployment ) error {
212259 return s .deploy (ctx , http .MethodPut , spec )
260+
213261}
214262
215- func (s * Client ) deploy (ctx context.Context , method string , spec types.FunctionDeployment ) (int , error ) {
263+ func (s * Client ) Update (ctx context.Context , spec types.FunctionDeployment ) error {
264+ return s .deploy (ctx , http .MethodPost , spec )
265+ }
266+
267+ func (s * Client ) deploy (ctx context.Context , method string , spec types.FunctionDeployment ) error {
216268
217269 bodyBytes , err := json .Marshal (spec )
218270 if err != nil {
219- return http .StatusBadRequest , err
271+ return createHttpError ( err , http .StatusBadRequest )
220272 }
221273
222274 bodyReader := bytes .NewReader (bodyBytes )
@@ -226,18 +278,18 @@ func (s *Client) deploy(ctx context.Context, method string, spec types.FunctionD
226278
227279 req , err := http .NewRequestWithContext (ctx , method , u .String (), bodyReader )
228280 if err != nil {
229- return http . StatusBadGateway , err
281+ return fmt . Errorf ( "unable to create request for %s, error: %w" , u . String (), err )
230282 }
231283
232284 if s .ClientAuth != nil {
233285 if err := s .ClientAuth .Set (req ); err != nil {
234- return http . StatusInternalServerError , fmt .Errorf ("unable to set Authorization header: %w" , err )
286+ return fmt .Errorf ("unable to set Authorization header: %w" , err )
235287 }
236288 }
237289
238290 res , err := s .Client .Do (req )
239291 if err != nil {
240- return http . StatusBadGateway , err
292+ return fmt . Errorf ( "unable to make HTTP request to OpenFaaS on URL: %s, error: %s" , s . GatewayURL , err )
241293 }
242294
243295 var body []byte
@@ -248,13 +300,13 @@ func (s *Client) deploy(ctx context.Context, method string, spec types.FunctionD
248300
249301 switch res .StatusCode {
250302 case http .StatusAccepted , http .StatusOK , http .StatusCreated :
251- return res . StatusCode , nil
303+ return nil
252304
253305 case http .StatusUnauthorized :
254- return res . StatusCode , fmt .Errorf ("unauthorized action, please setup authentication for this server" )
306+ return createHttpError ( fmt .Errorf ("unauthorized action, please setup authentication for this server" ), res . StatusCode )
255307
256308 default :
257- return res . StatusCode , fmt .Errorf ("unexpected status code: %d, message: %q" , res .StatusCode , string (body ))
309+ return createHttpError ( fmt .Errorf ("unexpected status code: %d, message: %q" , res .StatusCode , string (body )), res . StatusCode )
258310 }
259311}
260312
@@ -280,7 +332,7 @@ func (s *Client) ScaleFunction(ctx context.Context, functionName, namespace stri
280332
281333 req , err := http .NewRequestWithContext (ctx , http .MethodPost , u .String (), bodyReader )
282334 if err != nil {
283- return fmt .Errorf ("cannot connect to OpenFaaS on URL: %s, error: %s " , u .String (), err )
335+ return fmt .Errorf ("unable to create request for %s, error: %w " , u .String (), err )
284336 }
285337
286338 if s .ClientAuth != nil {
@@ -290,7 +342,7 @@ func (s *Client) ScaleFunction(ctx context.Context, functionName, namespace stri
290342 }
291343 res , err := http .DefaultClient .Do (req )
292344 if err != nil {
293- return fmt .Errorf ("cannot connect to OpenFaaS on URL: %s, error: %s" , s .GatewayURL , err )
345+ return fmt .Errorf ("unable to make HTTP request to OpenFaaS on URL: %s, error: %s" , s .GatewayURL , err )
294346
295347 }
296348
@@ -303,10 +355,10 @@ func (s *Client) ScaleFunction(ctx context.Context, functionName, namespace stri
303355 break
304356
305357 case http .StatusNotFound :
306- return fmt .Errorf ("function %s not found" , functionName )
358+ return createHttpError ( fmt .Errorf ("function %s not found" , functionName ), res . StatusCode )
307359
308360 case http .StatusUnauthorized :
309- return fmt .Errorf ("unauthorized action, please setup authentication for this server" )
361+ return createHttpError ( fmt .Errorf ("unauthorized action, please setup authentication for this server" ), res . StatusCode )
310362
311363 default :
312364 var err error
@@ -315,7 +367,7 @@ func (s *Client) ScaleFunction(ctx context.Context, functionName, namespace stri
315367 return err
316368 }
317369
318- return fmt .Errorf ("server returned unexpected status code %d, message: %q" , res .StatusCode , string (bytesOut ))
370+ return createHttpError ( fmt .Errorf ("server returned unexpected status code %d, message: %q" , res .StatusCode , string (bytesOut )), res . StatusCode )
319371 }
320372 return nil
321373}
@@ -338,7 +390,7 @@ func (s *Client) DeleteFunction(ctx context.Context, functionName, namespace str
338390
339391 req , err := http .NewRequestWithContext (ctx , http .MethodDelete , u .String (), bodyReader )
340392 if err != nil {
341- return fmt .Errorf ("cannot connect to OpenFaaS on URL: %s, error: %s " , u .String (), err )
393+ return fmt .Errorf ("unable to create request for %s, error: %w " , u .String (), err )
342394 }
343395
344396 if s .ClientAuth != nil {
@@ -348,7 +400,7 @@ func (s *Client) DeleteFunction(ctx context.Context, functionName, namespace str
348400 }
349401 res , err := http .DefaultClient .Do (req )
350402 if err != nil {
351- return fmt .Errorf ("cannot connect to OpenFaaS on URL: %s, error: %s" , s .GatewayURL , err )
403+ return fmt .Errorf ("unable to make HTTP request to OpenFaaS on URL: %s, error: %s" , s .GatewayURL , err )
352404
353405 }
354406
@@ -361,10 +413,10 @@ func (s *Client) DeleteFunction(ctx context.Context, functionName, namespace str
361413 break
362414
363415 case http .StatusNotFound :
364- return fmt .Errorf ("function %s not found" , functionName )
416+ return createHttpError ( fmt .Errorf ("function %s not found" , functionName ), res . StatusCode )
365417
366418 case http .StatusUnauthorized :
367- return fmt .Errorf ("unauthorized action, please setup authentication for this server" )
419+ return createHttpError ( fmt .Errorf ("unauthorized action, please setup authentication for this server" ), res . StatusCode )
368420
369421 default :
370422 var err error
@@ -373,7 +425,7 @@ func (s *Client) DeleteFunction(ctx context.Context, functionName, namespace str
373425 return err
374426 }
375427
376- return fmt .Errorf ("server returned unexpected status code %d, message: %q" , res .StatusCode , string (bytesOut ))
428+ return createHttpError ( fmt .Errorf ("server returned unexpected status code %d, message: %q" , res .StatusCode , string (bytesOut )), res . StatusCode )
377429 }
378430 return nil
379431}
0 commit comments