Skip to content

Commit 1e1dbbf

Browse files
authored
Return a StatusCodeError when a workspace's message limit is exceeded (#1383)
Currently, this library throws a `strconv.NumError` when attempting to post messages to a Slack workspace that has exceeded its message limit. It does so because Slack returns a 429 without a `Retry-After` header for those requests. This PR improves the situation by instead returning a `StatusCodeError`. I couldn't see any obvious way to provide a more detailed error without creating a new error class. To only additional information returned from Slack for these errors is a body that reads `message_limit_exceeded`.
2 parents 8f006d1 + e356192 commit 1e1dbbf

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

misc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func timerReset(t *time.Timer, d time.Duration) {
306306
}
307307

308308
func checkStatusCode(resp *http.Response, d Debug) error {
309-
if resp.StatusCode == http.StatusTooManyRequests {
309+
if resp.StatusCode == http.StatusTooManyRequests && resp.Header.Get("Retry-After") != "" {
310310
retry, err := strconv.ParseInt(resp.Header.Get("Retry-After"), 10, 64)
311311
if err != nil {
312312
return err

webhooks_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ func TestPostWebhook_NotOK(t *testing.T) {
6666
}
6767
}
6868

69+
func TestPostWebhook_MessageLimitExceeded(t *testing.T) {
70+
once.Do(startServer)
71+
72+
http.HandleFunc("/message_limit_exceeded", func(rw http.ResponseWriter, r *http.Request) {
73+
// When a workspace's message limit is exceeded we get a 429 without a Retry-After header
74+
rw.WriteHeader(http.StatusTooManyRequests)
75+
rw.Write([]byte("message_limit_exceeded"))
76+
})
77+
78+
url := "http://" + serverAddr + "/message_limit_exceeded"
79+
80+
err := PostWebhook(url, &WebhookMessage{})
81+
82+
if err == nil {
83+
t.Errorf("Expected to receive error")
84+
}
85+
assert.IsType(t, StatusCodeError{}, err)
86+
}
87+
6988
func TestWebhookMessage_WithBlocks(t *testing.T) {
7089
textBlockObject := NewTextBlockObject("plain_text", "text", false, false)
7190
sectionBlock := NewSectionBlock(textBlockObject, nil, nil)

0 commit comments

Comments
 (0)