mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
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:
@ -321,7 +321,14 @@ func (c *DeviceAuth) AuthorizeDevice(ctx context.Context) (*codersdk.ExternalAut
|
|||||||
}
|
}
|
||||||
err = json.NewDecoder(resp.Body).Decode(&r)
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
||||||
if err != nil {
|
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 != "" {
|
if r.ErrorDescription != "" {
|
||||||
return nil, xerrors.New(r.ErrorDescription)
|
return nil, xerrors.New(r.ErrorDescription)
|
||||||
|
@ -279,6 +279,28 @@ func TestExternalAuthDevice(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.True(t, auth.Authenticated)
|
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
|
// nolint:bodyclose
|
||||||
|
Reference in New Issue
Block a user