Tweak error handling

This commit is contained in:
Neil Alexander 2022-08-08 16:35:51 +01:00
parent 75d443203b
commit 1f0f3e8e7c
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
3 changed files with 12 additions and 11 deletions

View file

@ -72,8 +72,8 @@ func PostJSON[reqtype, restype any, errtype error](
} }
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
var errorBody errtype var errorBody errtype
if msgerr := json.NewDecoder(res.Body).Decode(&errorBody); msgerr == nil { if msgerr := json.NewDecoder(res.Body).Decode(&errorBody); msgerr != nil {
return fmt.Errorf("internal API: %d from %s with unparsable error body", res.StatusCode, apiURL) return fmt.Errorf("internal API: %d from %s (failed to decode error: %s)", res.StatusCode, apiURL, msgerr)
} }
return errorBody return errorBody
} }

View file

@ -63,8 +63,5 @@ func CallInternalProxyAPI[req, res any, errtype error](name, url string, client
defer span.Finish() defer span.Finish()
var response res var response res
if err := PostJSON[req, res, errtype](ctx, span, client, url, request, &response); err != nil { return response, PostJSON[req, res, errtype](ctx, span, client, url, request, &response)
return response, err
}
return response, nil
} }

View file

@ -117,16 +117,20 @@ func TestQueryProfile(t *testing.T) {
}, },
} }
runCases := func(testAPI api.UserInternalAPI) { runCases := func(testAPI api.UserInternalAPI, http bool) {
mode := "monolith"
if http {
mode = "HTTP"
}
for _, tc := range testCases { for _, tc := range testCases {
var gotRes api.QueryProfileResponse var gotRes api.QueryProfileResponse
gotErr := testAPI.QueryProfile(context.TODO(), &tc.req, &gotRes) gotErr := testAPI.QueryProfile(context.TODO(), &tc.req, &gotRes)
if tc.wantErr == nil && gotErr != nil || tc.wantErr != nil && gotErr == nil { if tc.wantErr == nil && gotErr != nil || tc.wantErr != nil && gotErr == nil {
t.Errorf("QueryProfile error, got %s want %s", gotErr, tc.wantErr) t.Errorf("QueryProfile %s error, got %s want %s", mode, gotErr, tc.wantErr)
continue continue
} }
if !reflect.DeepEqual(tc.wantRes, gotRes) { if !reflect.DeepEqual(tc.wantRes, gotRes) {
t.Errorf("QueryProfile response got %+v want %+v", gotRes, tc.wantRes) t.Errorf("QueryProfile %s response got %+v want %+v", mode, gotRes, tc.wantRes)
} }
} }
} }
@ -140,10 +144,10 @@ func TestQueryProfile(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("failed to create HTTP client") t.Fatalf("failed to create HTTP client")
} }
runCases(httpAPI) runCases(httpAPI, true)
}) })
t.Run("Monolith", func(t *testing.T) { t.Run("Monolith", func(t *testing.T) {
runCases(userAPI) runCases(userAPI, false)
}) })
} }