From afa9507ee24e756442ba199673336aa563498a48 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Wed, 4 Oct 2023 17:13:27 -0700 Subject: [PATCH] Remove panic from ClientLogin --- lemmy.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lemmy.go b/lemmy.go index 5bbd3f7..7cff447 100644 --- a/lemmy.go +++ b/lemmy.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "io" "net/http" "net/url" @@ -11,6 +12,8 @@ import ( "github.com/google/go-querystring/query" ) +var ErrNoToken = errors.New("the server didn't provide a token value in its response") + // Client is a client for Lemmy's HTTP API type Client struct { client *http.Client @@ -40,7 +43,12 @@ func (c *Client) ClientLogin(ctx context.Context, l Login) error { if err != nil { return err } - c.Token = lr.JWT.ValueOrZero() + + token, ok := lr.JWT.Value() + if !ok || token == "" { + return ErrNoToken + } + c.Token = token return nil }