Improve error handling when the response is empty

This commit is contained in:
Frédéric Guillot 2018-02-07 18:47:47 -08:00
parent 1e70ca1a19
commit 0fb87eba3f
5 changed files with 28 additions and 6 deletions

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2018-02-07 12:24:51.492778 +0100 CET m=+0.025245211
// 2018-02-07 18:40:34.783083402 -0800 PST m=+0.035977512
package locale
@ -439,7 +439,8 @@ var translations = map[string]string{
"Fever API endpoint:": "Point de terminaison de l'API Fever :",
"Miniflux API": "API de Miniflux",
"API Endpoint": "Point de terminaison de l'API",
"Your account password": "Le mot de passe de votre compte"
"Your account password": "Le mot de passe de votre compte",
"This web page is empty": "Cette page web est vide"
}
`,
}
@ -447,5 +448,5 @@ var translations = map[string]string{
var translationsChecksums = map[string]string{
"de_DE": "f4a66bffedb7bf99294281da5a1fa78901509db67a9fba15a160b656feb1425a",
"en_US": "6fe95384260941e8a5a3c695a655a932e0a8a6a572c1e45cb2b1ae8baa01b897",
"fr_FR": "eb05f9d8cf36bf943388f4027fede1c565576ccdd38b71c5ed1bb18cb60744f4",
"fr_FR": "3b8f4d329ca851f461c973bbdb77459006ba7b4d9bd49139d4002af67b4c3d4e",
}

View File

@ -209,5 +209,6 @@
"Fever API endpoint:": "Point de terminaison de l'API Fever :",
"Miniflux API": "API de Miniflux",
"API Endpoint": "Point de terminaison de l'API",
"Your account password": "Le mot de passe de votre compte"
"Your account password": "Le mot de passe de votre compte",
"This web page is empty": "Cette page web est vide"
}

View File

@ -69,7 +69,10 @@ func parseFeed(r io.Reader) (*model.Feed, error) {
defer timer.ExecutionTime(time.Now(), "[Feed:ParseFeed]")
var buffer bytes.Buffer
io.Copy(&buffer, r)
size, _ := io.Copy(&buffer, r)
if size == 0 {
return nil, errors.New("This feed is empty")
}
reader := bytes.NewReader(buffer.Bytes())
format := DetectFeedFormat(reader)

View File

@ -205,3 +205,10 @@ func TestParseUnknownFeed(t *testing.T) {
t.Error("ParseFeed must returns an error")
}
}
func TestParseEmptyFeed(t *testing.T) {
_, err := parseFeed(bytes.NewBufferString(""))
if err == nil {
t.Error("ParseFeed must returns an error")
}
}

View File

@ -23,6 +23,7 @@ import (
var (
errConnectionFailure = "Unable to open this link: %v"
errUnreadableDoc = "Unable to analyze this page: %v"
errEmptyBody = "This web page is empty"
)
// FindSubscriptions downloads and try to find one or more subscriptions from an URL.
@ -35,13 +36,22 @@ func FindSubscriptions(websiteURL string) (Subscriptions, error) {
return nil, errors.NewLocalizedError(errConnectionFailure, err)
}
// Content-Length = -1 when no Content-Length header is sent
if response.ContentLength == 0 {
return nil, errors.NewLocalizedError(errEmptyBody)
}
body, err := response.NormalizeBodyEncoding()
if err != nil {
return nil, err
}
var buffer bytes.Buffer
io.Copy(&buffer, body)
size, _ := io.Copy(&buffer, body)
if size == 0 {
return nil, errors.NewLocalizedError(errEmptyBody)
}
reader := bytes.NewReader(buffer.Bytes())
if format := feed.DetectFeedFormat(reader); format != feed.FormatUnknown {