Handle "entity too large" errors immediately instead of retrying.

This commit is contained in:
Justin Hawkins 2021-10-11 22:48:49 +10:30
parent b9cacf6d33
commit 2c4c9fdde6
2 changed files with 29 additions and 2 deletions

View File

@ -141,6 +141,11 @@ func (u *Upload) processUpload() error {
sleepForRetries(retriesRemaining) sleepForRetries(retriesRemaining)
continue continue
} else { } else {
if resp.StatusCode == 413 {
// just fail immediately, we know this means the file was too big
daulog.SendLog("413 received - file too large", daulog.LogTypeError)
return errors.New("received 413 - file too large")
}
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
// {"message": "Request entity too large", "code": 40005} // {"message": "Request entity too large", "code": 40005}

View File

@ -4,8 +4,8 @@ import (
"bytes" "bytes"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"testing"
"os" "os"
"testing"
// "github.com/tardisx/discord-auto-upload/config" // "github.com/tardisx/discord-auto-upload/config"
) )
@ -26,6 +26,14 @@ func DoGoodUpload(req *http.Request) (*http.Response, error) {
}, nil }, nil
} }
func DoTooBigUpload(req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(bytes.NewReader([]byte(`{"message": "Request entity too large", "code": 40005}`)))
return &http.Response{
StatusCode: 413,
Body: r,
}, nil
}
func TestSuccessfulUpload(t *testing.T) { func TestSuccessfulUpload(t *testing.T) {
// create temporary file, processUpload requires that it exists, even though // create temporary file, processUpload requires that it exists, even though
// we will not really be uploading it here // we will not really be uploading it here
@ -43,5 +51,19 @@ func TestSuccessfulUpload(t *testing.T) {
if u.Url != "https://cdn.discordapp.com/attachments/849615269706203171/851092588332449812/dau480457962.png" { if u.Url != "https://cdn.discordapp.com/attachments/849615269706203171/851092588332449812/dau480457962.png" {
t.Error("URL wrong") t.Error("URL wrong")
} }
}
func TestTooBigUpload(t *testing.T) {
// create temporary file, processUpload requires that it exists, even though
// we will not really be uploading it here
f, _ := os.CreateTemp("", "dautest-upload-*")
defer os.Remove(f.Name())
u := Upload{webhookURL: "https://127.0.0.1/", originalFilename: f.Name()}
u.Client = &MockClient{DoFunc: DoTooBigUpload}
err := u.processUpload()
if err == nil {
t.Error("error did not occur?")
} else if err.Error() != "received 413 - file too large" {
t.Errorf("wrong error occurred: %s", err.Error())
}
} }