Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable httperror to return status code from server #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nitishkumar71
Copy link
Member

@nitishkumar71 nitishkumar71 commented Jul 16, 2023

Description

Return HttpError which provides status code returned by openfaas API. We are only returning HttpError for errors in case of server APIs only.

There are lot of breaking changes in the API.

Motivation and Context

How Has This Been Tested?

  1. Created a test code script and same has been tested against faas-netes and faasd
package main

import (
	"fmt"
	"log"
	"net/http"
	"net/url"
	"os"
	"time"

	"github.com/openfaas/faas-provider/types"
	"github.com/openfaas/go-sdk"
)

func main() {
	username := os.Getenv("OPENFAAS_USERNAME")
	password := os.Getenv("OPENFAAS_PASSWORD")

	gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
	auth := &sdk.BasicAuth{
		Username: username,
		Password: password,
	}
	fnName := "test-fn"
	nsName := "openfaas-fn"

	client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)

	// get namespace
	namespaces, err := client.GetNamespaces()
	fmt.Printf("Namespaces: %q\n", namespaces)

	// deploy
	err = client.Deploy(types.FunctionDeployment{
		Service:   fnName,
		Image:     fmt.Sprintf("ttl.sh/nitishkumar71/%s:latest", fnName),
		Namespace: nsName,
	})

	if err != nil {
		log.Fatalf("Deploy Failed: %s", err)
	}

	fmt.Printf("Sleep....\n")
	time.Sleep(15 * time.Second)
	fmt.Printf("Sleep Finish\n")

	fn, err := client.GetFunction(fnName, nsName)
	fmt.Printf("Function: %v\n", fn)
	if err != nil {
		log.Fatalf("GetFunction Failed: %s", err)
	}

	fmt.Println("Scale to Zero")
	err = client.ScaleFunction(fnName, nsName, 0)
	if err != nil {
		log.Fatalf("ScaleFunction Failed: %s", err)
	}

	fmt.Printf("Sleep....\n")
	time.Sleep(15 * time.Second)
	fmt.Printf("Sleep Finish\n")

	// delete
	err = client.DeleteFunction(fnName, nsName)
	if err != nil {
		log.Fatalf("DeleteFunction Failed: %s", err)
	}
	fmt.Print("Function Deleted")
}


Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

Commits:

  • My commit message has a body and describe how this was tested and why it is required.
  • I have signed-off my commits with git commit -s for the Developer Certificate of Origin (DCO)

Code:

  • My code follows the code style of this project.
  • I have added tests to cover my changes.

Docs:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@nitishkumar71 nitishkumar71 force-pushed the http_error branch 2 times, most recently from 821a9bb to cec6749 Compare July 27, 2023 09:28
@nitishkumar71
Copy link
Member Author

Branch was re-based against master branch and below script was used to run test against both faas-netes and faasd. Tests were successful against both the faas implementation.

package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"os"
	"time"

	"github.com/openfaas/faas-provider/types"
	"github.com/openfaas/go-sdk"
)

func main() {
	username := os.Getenv("OPENFAAS_USERNAME")
	password := os.Getenv("OPENFAAS_PASSWORD")

	gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
	auth := &sdk.BasicAuth{
		Username: username,
		Password: password,
	}
	fnName := "test-fn"
	nsName := "openfaas-fn"

	client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)

	// get namespace
	namespaces, err := client.GetNamespaces(context.Background())
	fmt.Printf("Namespaces: %q\n", namespaces)

	// deploy
	err = client.Deploy(context.Background(), types.FunctionDeployment{
		Service:   fnName,
		Image:     fmt.Sprintf("ttl.sh/nitishkumar71/%s:latest", fnName),
		Namespace: nsName,
	})

	if err != nil {
		log.Fatalf("Deploy Failed: %s", err)
	}

	fmt.Printf("Sleep....\n")
	time.Sleep(15 * time.Second)
	fmt.Printf("Sleep Finish\n")

	fn, err := client.GetFunction(context.Background(), fnName, nsName)
	fmt.Printf("Function: %v\n", fn)
	if err != nil {
		log.Fatalf("GetFunction Failed: %s", err)
	}

	fmt.Println("Scale to Zero")
	err = client.ScaleFunction(context.Background(), fnName, nsName, 0)
	if err != nil {
		log.Fatalf("ScaleFunction Failed: %s", err)
	}

	fmt.Printf("Sleep....\n")
	time.Sleep(15 * time.Second)
	fmt.Printf("Sleep Finish\n")

	// delete
	err = client.DeleteFunction(context.Background(), fnName, nsName)
	if err != nil {
		log.Fatalf("DeleteFunction Failed: %s", err)
	}
	fmt.Print("Function Deleted")
}

@alexellis
Copy link
Member

I wonder if it's obvious that error is a wrapped HTTPError that can be unwraped?

func (s *Client) deploy(ctx context.Context, method string, spec types.FunctionDeployment) (int, error) {
func (s *Client) deploy(ctx context.Context, method string, spec types.FunctionDeployment) error {

Would it make sense to return HTTPError instead of error given that the int has now been removed?

cc @LucasRoesler

@nitishkumar71
Copy link
Member Author

nitishkumar71 commented Aug 2, 2023

I wonder if it's obvious that error is a wrapped HTTPError that can be unwraped?

func (s *Client) deploy(ctx context.Context, method string, spec types.FunctionDeployment) (int, error) {
func (s *Client) deploy(ctx context.Context, method string, spec types.FunctionDeployment) error {

Would it make sense to return HTTPError instead of error given that the int has now been removed?

cc @LucasRoesler

We are also returning generic error sometimes. So calling all errors as HttpError, doesn't sound correct to me.

https://github.com/openfaas/go-sdk/pull/8/files#diff-4b667feae66c9d46b21b9ecc19e8958cf4472d162ce0a47ac3e8386af8bbd8cfR219
https://github.com/openfaas/go-sdk/pull/8/files#diff-4b667feae66c9d46b21b9ecc19e8958cf4472d162ce0a47ac3e8386af8bbd8cfR270

@LucasRoesler
Copy link
Member

I am not sure i understand the situation here. If the error is caused by some kind of http based error, return an HttpError, if not return the (potentially wrapped) other error. Ideally HttpPError should have an interface that allows you to check for it or you use errors.As to check for it

var httpErr *HttpError
if errors.Is(err, &httpErr) {
   // special http handling
}

See https://pkg.go.dev/errors#example-As

or

type HttpError struct {
	Err    error
	Status int
}

func (e *HttpError) Error() string {
	return e.Err.Error()
}

func (e HttpError) Status() int {
   return e.Status
}

// Then later
type Statuser interface {
    Status() int
}


// then later
if e, ok := err.(Statuser); ok {
    fmt.Printf("we have a status code %d" e.Status())
}

@nitishkumar71
Copy link
Member Author

I am not sure i understand the situation here. If the error is caused by some kind of http based error, return an HttpError, if not return the (potentially wrapped) other error. Ideally HttpPError should have an interface that allows you to check for it or you use errors.As to check for it

var httpErr *HttpError
if errors.Is(err, &httpErr) {
   // special http handling
}

See https://pkg.go.dev/errors#example-As

or

type HttpError struct {
	Err    error
	Status int
}

func (e *HttpError) Error() string {
	return e.Err.Error()
}

func (e HttpError) Status() int {
   return e.Status
}

// Then later
type Statuser interface {
    Status() int
}


// then later
if e, ok := err.(Statuser); ok {
    fmt.Printf("we have a status code %d" e.Status())
}

Agree. This is what I have thought of.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change from returning error to HttpError
3 participants