Skip to content

Commit

Permalink
function client: VerifySignature return wrapped error
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaiba authored and jchappelow committed Apr 24, 2024
1 parent 57cb1d6 commit 4863d69
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
1 change: 0 additions & 1 deletion core/rpc/client/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

// The following errors may be detected by consumers using errors.Is.
var (
ErrInvalidSignature = errors.New("invalid signature")
// ErrUnauthorized is returned when the client is not authenticated
// It is the equivalent of http status code 401
ErrUnauthorized = errors.New("unauthorized")
Expand Down
13 changes: 10 additions & 3 deletions core/rpc/client/function/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/base64"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
Expand All @@ -12,6 +13,8 @@ import (
httpFunction "github.com/kwilteam/kwil-db/core/rpc/http/function"
)

var ErrInvalidSignature = errors.New("invalid signature")

type Client struct {
conn *httpFunction.APIClient
url *url.URL
Expand Down Expand Up @@ -51,17 +54,21 @@ func (c *Client) VerifySignature(ctx context.Context, sender []byte, signature *
SignatureType: signature.Type,
},
})
if err != nil {
if err != nil { // communication error
return err
}
defer res.Body.Close()

// server logic error
if result.Error_ != "" {
return errors.New(result.Error_)
return fmt.Errorf("%w: %s", ErrInvalidSignature, result.Error_)
}

// NOTE: Forget why I put both `valid` and `error` in the response.
// if `valid` is false, `error` should not be empty.
// This might be not needed, but I just keep it here.
if !result.Valid {
return errors.New("invalid signature")
return ErrInvalidSignature
}

return nil
Expand Down

0 comments on commit 4863d69

Please sign in to comment.