Prevent nil pointer if endpoint is wrong

This commit is contained in:
Sung 2025-10-18 14:00:10 -07:00
commit 44af0f9e31
2 changed files with 18 additions and 4 deletions

View file

@ -579,10 +579,12 @@ func Signin(ctx context.DnoteCtx, email, password string) (SigninResponse, error
return SigninResponse{}, errors.Wrap(err, "marshaling payload")
}
res, err := doReq(ctx, "POST", "/v3/signin", string(b), nil)
if res.StatusCode == http.StatusUnauthorized {
return SigninResponse{}, ErrInvalidLogin
} else if err != nil {
if err != nil {
// Check if this is a 401 Unauthorized error
var httpErr *HTTPError
if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusUnauthorized {
return SigninResponse{}, ErrInvalidLogin
}
return SigninResponse{}, errors.Wrap(err, "making http request")
}

View file

@ -124,6 +124,18 @@ func TestSignIn(t *testing.T) {
assert.Equal(t, result.Key, "", "Key mismatch")
assert.Equal(t, result.ExpiresAt, int64(0), "ExpiresAt mismatch")
})
t.Run("network error", func(t *testing.T) {
// Use an invalid endpoint that will fail to connect
endpoint := "http://localhost:99999/api"
result, err := Signin(context.DnoteCtx{APIEndpoint: endpoint, HTTPClient: testClient}, "alice@example.com", "pass1234")
if err == nil {
t.Error("error should have been returned for network failure")
}
assert.Equal(t, result.Key, "", "Key mismatch")
assert.Equal(t, result.ExpiresAt, int64(0), "ExpiresAt mismatch")
})
}
func TestSignOut(t *testing.T) {