mirror of
https://github.com/dnote/dnote
synced 2026-03-14 22:45:50 +01:00
Prevent nil pointer if endpoint is wrong
This commit is contained in:
parent
190fcd8ffc
commit
44af0f9e31
2 changed files with 18 additions and 4 deletions
|
|
@ -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")
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue