Skip to content

Commit 04afb88

Browse files
authored
fix: return a more sophisticated error for device failure on 429 (#11554)
* fix: return a more sophisticated error for device failure on 429
1 parent b1d53a6 commit 04afb88

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

coderd/externalauth/externalauth.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,14 @@ func (c *DeviceAuth) AuthorizeDevice(ctx context.Context) (*codersdk.ExternalAut
321321
}
322322
err = json.NewDecoder(resp.Body).Decode(&r)
323323
if err != nil {
324-
return nil, err
324+
// Some status codes do not return json payloads, and we should
325+
// return a better error.
326+
switch resp.StatusCode {
327+
case http.StatusTooManyRequests:
328+
return nil, fmt.Errorf("rate limit hit, unable to authorize device. please try again later")
329+
default:
330+
return nil, err
331+
}
325332
}
326333
if r.ErrorDescription != "" {
327334
return nil, xerrors.New(r.ErrorDescription)

coderd/externalauth_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,28 @@ func TestExternalAuthDevice(t *testing.T) {
279279
require.NoError(t, err)
280280
require.True(t, auth.Authenticated)
281281
})
282+
t.Run("TooManyRequests", func(t *testing.T) {
283+
t.Parallel()
284+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
285+
w.WriteHeader(http.StatusTooManyRequests)
286+
// Github returns an html payload for this error.
287+
_, _ = w.Write([]byte(`Please wait a few minutes before you try again`))
288+
}))
289+
defer srv.Close()
290+
client := coderdtest.New(t, &coderdtest.Options{
291+
ExternalAuthConfigs: []*externalauth.Config{{
292+
ID: "test",
293+
DeviceAuth: &externalauth.DeviceAuth{
294+
ClientID: "test",
295+
CodeURL: srv.URL,
296+
Scopes: []string{"repo"},
297+
},
298+
}},
299+
})
300+
coderdtest.CreateFirstUser(t, client)
301+
_, err := client.ExternalAuthDeviceByID(context.Background(), "test")
302+
require.ErrorContains(t, err, "rate limit hit")
303+
})
282304
}
283305

284306
// nolint:bodyclose

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy