From 0e6717b7c86a1761b1ae515dd212a78f1d1e7c8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Mon, 20 Nov 2017 16:11:55 -0800 Subject: [PATCH] Ensure that LocalizedError are returned by parsers --- locale/translations.go | 9 ++++++--- locale/translations/fr_FR.json | 5 ++++- reader/feed/atom/parser.go | 7 ++++--- reader/feed/atom/parser_test.go | 14 ++++++++++++++ reader/feed/json/parser.go | 7 ++++--- reader/feed/json/parser_test.go | 14 ++++++++++++++ reader/feed/rss/parser.go | 4 ++-- reader/feed/rss/parser_test.go | 6 ++++++ reader/opml/parser_test.go | 23 +++++++++++------------ server/static/bin.go | 2 +- server/static/css.go | 2 +- server/static/js.go | 2 +- server/template/common.go | 2 +- server/template/views.go | 2 +- sql/sql.go | 2 +- 15 files changed, 71 insertions(+), 30 deletions(-) diff --git a/locale/translations.go b/locale/translations.go index 551c06f7..402afe58 100644 --- a/locale/translations.go +++ b/locale/translations.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-20 15:11:09.541312146 -0800 PST m=+0.018445746 +// 2017-11-20 16:03:46.536096032 -0800 PST m=+0.030567687 package locale @@ -126,12 +126,15 @@ var Translations = map[string]string{ "Unable to execute request: %v": "Impossible d'exécuter cette requête: %v", "Last Parsing Error": "Dernière erreur d'analyse", "There is a problem with this feed": "Il y a un problème avec cet abonnement", - "Unable to parse OPML file: %v": "Impossible de lire le fichier OPML : %v" + "Unable to parse OPML file: %v": "Impossible de lire le fichier OPML : %v", + "Unable to parse RSS feed: %v": "Impossible de lire ce flux RSS: %v", + "Unable to parse Atom feed: %v": "Impossible de lire ce flux Atom: %v", + "Unable to parse JSON feed: %v": "Impossible de lire ce flux Json: %v" } `, } var TranslationsChecksums = map[string]string{ "en_US": "6fe95384260941e8a5a3c695a655a932e0a8a6a572c1e45cb2b1ae8baa01b897", - "fr_FR": "ec81374ce72a1cf3a06bda027faa338745a77035f8bd3504ad4d452958260525", + "fr_FR": "e9b3753645cb83a338f48bdc24825e629d568ebd3a65a4be2978ff6b4f3bc380", } diff --git a/locale/translations/fr_FR.json b/locale/translations/fr_FR.json index c2054c98..f7536f68 100644 --- a/locale/translations/fr_FR.json +++ b/locale/translations/fr_FR.json @@ -110,5 +110,8 @@ "Unable to execute request: %v": "Impossible d'exécuter cette requête: %v", "Last Parsing Error": "Dernière erreur d'analyse", "There is a problem with this feed": "Il y a un problème avec cet abonnement", - "Unable to parse OPML file: %v": "Impossible de lire le fichier OPML : %v" + "Unable to parse OPML file: %v": "Impossible de lire le fichier OPML : %v", + "Unable to parse RSS feed: %v": "Impossible de lire ce flux RSS: %v", + "Unable to parse Atom feed: %v": "Impossible de lire ce flux Atom: %v", + "Unable to parse JSON feed: %v": "Impossible de lire ce flux Json: %v" } diff --git a/reader/feed/atom/parser.go b/reader/feed/atom/parser.go index 0b8f0d74..cb21c041 100644 --- a/reader/feed/atom/parser.go +++ b/reader/feed/atom/parser.go @@ -6,10 +6,11 @@ package atom import ( "encoding/xml" - "fmt" - "github.com/miniflux/miniflux2/model" "io" + "github.com/miniflux/miniflux2/errors" + "github.com/miniflux/miniflux2/model" + "golang.org/x/net/html/charset" ) @@ -21,7 +22,7 @@ func Parse(data io.Reader) (*model.Feed, error) { err := decoder.Decode(atomFeed) if err != nil { - return nil, fmt.Errorf("Unable to parse Atom feed: %v\n", err) + return nil, errors.NewLocalizedError("Unable to parse Atom feed: %v", err) } return atomFeed.Transform(), nil diff --git a/reader/feed/atom/parser_test.go b/reader/feed/atom/parser_test.go index 39d94206..d5eea23f 100644 --- a/reader/feed/atom/parser_test.go +++ b/reader/feed/atom/parser_test.go @@ -8,6 +8,8 @@ import ( "bytes" "testing" "time" + + "github.com/miniflux/miniflux2/errors" ) func TestParseAtomSample(t *testing.T) { @@ -317,3 +319,15 @@ func TestParseEntryWithEnclosures(t *testing.T) { t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[1].Size) } } + +func TestParseInvalidXml(t *testing.T) { + data := `garbage` + _, err := Parse(bytes.NewBufferString(data)) + if err == nil { + t.Error("Parse should returns an error") + } + + if _, ok := err.(errors.LocalizedError); !ok { + t.Error("The error returned must be a LocalizedError") + } +} diff --git a/reader/feed/json/parser.go b/reader/feed/json/parser.go index 816d28e6..6dbc91be 100644 --- a/reader/feed/json/parser.go +++ b/reader/feed/json/parser.go @@ -6,9 +6,10 @@ package json import ( "encoding/json" - "fmt" - "github.com/miniflux/miniflux2/model" "io" + + "github.com/miniflux/miniflux2/errors" + "github.com/miniflux/miniflux2/model" ) // Parse returns a normalized feed struct. @@ -16,7 +17,7 @@ func Parse(data io.Reader) (*model.Feed, error) { jsonFeed := new(JsonFeed) decoder := json.NewDecoder(data) if err := decoder.Decode(&jsonFeed); err != nil { - return nil, fmt.Errorf("Unable to parse JSON Feed: %v", err) + return nil, errors.NewLocalizedError("Unable to parse JSON Feed: %v", err) } return jsonFeed.Transform(), nil diff --git a/reader/feed/json/parser_test.go b/reader/feed/json/parser_test.go index f2c97fc9..ecb11a18 100644 --- a/reader/feed/json/parser_test.go +++ b/reader/feed/json/parser_test.go @@ -9,6 +9,8 @@ import ( "strings" "testing" "time" + + "github.com/miniflux/miniflux2/errors" ) func TestParseJsonFeed(t *testing.T) { @@ -343,3 +345,15 @@ func TestParseTruncateItemTitle(t *testing.T) { t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title) } } + +func TestParseInvalidJSON(t *testing.T) { + data := `garbage` + _, err := Parse(bytes.NewBufferString(data)) + if err == nil { + t.Error("Parse should returns an error") + } + + if _, ok := err.(errors.LocalizedError); !ok { + t.Error("The error returned must be a LocalizedError") + } +} diff --git a/reader/feed/rss/parser.go b/reader/feed/rss/parser.go index 4718dc09..4eceb455 100644 --- a/reader/feed/rss/parser.go +++ b/reader/feed/rss/parser.go @@ -6,9 +6,9 @@ package rss import ( "encoding/xml" - "fmt" "io" + "github.com/miniflux/miniflux2/errors" "github.com/miniflux/miniflux2/model" "golang.org/x/net/html/charset" @@ -22,7 +22,7 @@ func Parse(data io.Reader) (*model.Feed, error) { err := decoder.Decode(feed) if err != nil { - return nil, fmt.Errorf("unable to parse RSS feed: %v", err) + return nil, errors.NewLocalizedError("Unable to parse RSS feed: %v", err) } return feed.Transform(), nil diff --git a/reader/feed/rss/parser_test.go b/reader/feed/rss/parser_test.go index a0a8c93c..9f1a557e 100644 --- a/reader/feed/rss/parser_test.go +++ b/reader/feed/rss/parser_test.go @@ -8,6 +8,8 @@ import ( "bytes" "testing" "time" + + "github.com/miniflux/miniflux2/errors" ) func TestParseRss2Sample(t *testing.T) { @@ -541,4 +543,8 @@ func TestParseInvalidXml(t *testing.T) { if err == nil { t.Error("Parse should returns an error") } + + if _, ok := err.(errors.LocalizedError); !ok { + t.Error("The error returned must be a LocalizedError") + } } diff --git a/reader/opml/parser_test.go b/reader/opml/parser_test.go index 95a832b3..8d56df5b 100644 --- a/reader/opml/parser_test.go +++ b/reader/opml/parser_test.go @@ -4,8 +4,12 @@ package opml -import "testing" -import "bytes" +import ( + "bytes" + "testing" + + "github.com/miniflux/miniflux2/errors" +) func TestParseOpmlWithoutCategories(t *testing.T) { data := ` @@ -121,18 +125,13 @@ func TestParseOpmlWithEmptyTitleAndEmptySiteURL(t *testing.T) { } func TestParseInvalidXML(t *testing.T) { - data := ` - - - - - - - ` - + data := `garbage` _, err := Parse(bytes.NewBufferString(data)) if err == nil { t.Error("Parse should generate an error") } + + if _, ok := err.(errors.LocalizedError); !ok { + t.Error("The error returned must be a LocalizedError") + } } diff --git a/server/static/bin.go b/server/static/bin.go index 5ca3974a..0a7f6798 100644 --- a/server/static/bin.go +++ b/server/static/bin.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-20 15:11:09.5282708 -0800 PST m=+0.005404400 +// 2017-11-20 16:03:46.511191455 -0800 PST m=+0.005663110 package static diff --git a/server/static/css.go b/server/static/css.go index b69ae83b..50c0ccab 100644 --- a/server/static/css.go +++ b/server/static/css.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-20 15:11:09.529138622 -0800 PST m=+0.006272222 +// 2017-11-20 16:03:46.51268594 -0800 PST m=+0.007157595 package static diff --git a/server/static/js.go b/server/static/js.go index b908f6ab..f8524454 100644 --- a/server/static/js.go +++ b/server/static/js.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-20 15:11:09.530927342 -0800 PST m=+0.008060942 +// 2017-11-20 16:03:46.51596478 -0800 PST m=+0.010436435 package static diff --git a/server/template/common.go b/server/template/common.go index 57259cd6..acb1db94 100644 --- a/server/template/common.go +++ b/server/template/common.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-20 15:11:09.54007082 -0800 PST m=+0.017204420 +// 2017-11-20 16:03:46.53440477 -0800 PST m=+0.028876425 package template diff --git a/server/template/views.go b/server/template/views.go index 3f1722f4..856927f6 100644 --- a/server/template/views.go +++ b/server/template/views.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-20 15:11:09.531826622 -0800 PST m=+0.008960222 +// 2017-11-20 16:03:46.517489275 -0800 PST m=+0.011960930 package template diff --git a/sql/sql.go b/sql/sql.go index 8fb8ca70..1200ea42 100644 --- a/sql/sql.go +++ b/sql/sql.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-20 15:11:09.527012847 -0800 PST m=+0.004146447 +// 2017-11-20 16:03:46.509724835 -0800 PST m=+0.004196490 package sql