fix: return a more sophisticated error for device failure on 429 (#11554)

* fix: return a more sophisticated error for device failure on 429
This commit is contained in:
Steven Masley
2024-01-10 11:29:44 -06:00
committed by GitHub
parent b1d53a68c2
commit 04afb88e6f
2 changed files with 30 additions and 1 deletions

View File

@ -321,7 +321,14 @@ func (c *DeviceAuth) AuthorizeDevice(ctx context.Context) (*codersdk.ExternalAut
}
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return nil, err
// Some status codes do not return json payloads, and we should
// return a better error.
switch resp.StatusCode {
case http.StatusTooManyRequests:
return nil, fmt.Errorf("rate limit hit, unable to authorize device. please try again later")
default:
return nil, err
}
}
if r.ErrorDescription != "" {
return nil, xerrors.New(r.ErrorDescription)

View File

@ -279,6 +279,28 @@ func TestExternalAuthDevice(t *testing.T) {
require.NoError(t, err)
require.True(t, auth.Authenticated)
})
t.Run("TooManyRequests", func(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusTooManyRequests)
// Github returns an html payload for this error.
_, _ = w.Write([]byte(`Please wait a few minutes before you try again`))
}))
defer srv.Close()
client := coderdtest.New(t, &coderdtest.Options{
ExternalAuthConfigs: []*externalauth.Config{{
ID: "test",
DeviceAuth: &externalauth.DeviceAuth{
ClientID: "test",
CodeURL: srv.URL,
Scopes: []string{"repo"},
},
}},
})
coderdtest.CreateFirstUser(t, client)
_, err := client.ExternalAuthDeviceByID(context.Background(), "test")
require.ErrorContains(t, err, "rate limit hit")
})
}
// nolint:bodyclose