feat(garmin): expose ErrNotFound for a wrapper-reported 404
roundTrip now wraps the returned error with the new ErrNotFound sentinel whenever the wrapper's response set not_found (Task 1), letting any Client method's caller distinguish a definitive 404 from a transient failure via errors.Is, regardless of which garminconnect method was called.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os/exec"
|
||||
@@ -18,8 +19,9 @@ import (
|
||||
// wireResponsePayload is what a fake wrapper handler returns for one
|
||||
// request; the harness fills in the response ID.
|
||||
type wireResponsePayload struct {
|
||||
result json.RawMessage
|
||||
err string
|
||||
result json.RawMessage
|
||||
err string
|
||||
notFound bool
|
||||
}
|
||||
|
||||
func fakeResult(v any) wireResponsePayload {
|
||||
@@ -34,6 +36,10 @@ func fakeError(msg string) wireResponsePayload {
|
||||
return wireResponsePayload{err: msg}
|
||||
}
|
||||
|
||||
func fakeNotFoundError(msg string) wireResponsePayload {
|
||||
return wireResponsePayload{err: msg, notFound: true}
|
||||
}
|
||||
|
||||
// newFakeWrapperClient wires a subprocessClient to an in-process goroutine
|
||||
// that plays the Python wrapper's role, so protocol-level Go logic can be
|
||||
// tested without python3/garminconnect installed.
|
||||
@@ -61,7 +67,7 @@ func newFakeWrapperClient(t *testing.T, handle func(cmd string, params json.RawM
|
||||
continue
|
||||
}
|
||||
payload := handle(req.Cmd, req.Params)
|
||||
resp := wireResponse{ID: req.ID, Result: payload.result, Error: payload.err}
|
||||
resp := wireResponse{ID: req.ID, Result: payload.result, Error: payload.err, NotFound: payload.notFound}
|
||||
if err := enc.Encode(resp); err != nil {
|
||||
return
|
||||
}
|
||||
@@ -134,6 +140,31 @@ func TestSubprocessClient_RoundTrip_WrapperErrorPropagates(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubprocessClient_RoundTrip_NotFoundWrapsErrNotFound(t *testing.T) {
|
||||
c := newFakeWrapperClient(t, func(cmd string, params json.RawMessage) wireResponsePayload {
|
||||
return fakeNotFoundError("API Error 404")
|
||||
})
|
||||
|
||||
_, err := c.roundTrip(context.Background(), "call", nil)
|
||||
if !errors.Is(err, ErrNotFound) {
|
||||
t.Fatalf("roundTrip error = %v, want errors.Is(err, ErrNotFound)", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "API Error 404") {
|
||||
t.Errorf("roundTrip error = %v, want it to still contain the original message", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubprocessClient_RoundTrip_OrdinaryErrorDoesNotWrapErrNotFound(t *testing.T) {
|
||||
c := newFakeWrapperClient(t, func(cmd string, params json.RawMessage) wireResponsePayload {
|
||||
return fakeError("boom")
|
||||
})
|
||||
|
||||
_, err := c.roundTrip(context.Background(), "call", nil)
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
t.Fatalf("roundTrip error = %v, want errors.Is(err, ErrNotFound) to be false", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubprocessClient_UpdateCredentials_ResetsStartedState(t *testing.T) {
|
||||
c := &subprocessClient{cfg: Config{GarminEmail: "old@example.com", GarminPassword: "old"}}
|
||||
c.started = true // simulate an already-spawned subprocess, no real cmd/pipes
|
||||
|
||||
Reference in New Issue
Block a user