Ensure that LocalizedError are returned by parsers

This commit is contained in:
Frédéric Guillot 2017-11-20 16:11:55 -08:00
parent 557cf9c21d
commit 0e6717b7c8
15 changed files with 71 additions and 30 deletions

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // 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 package locale
@ -126,12 +126,15 @@ var Translations = map[string]string{
"Unable to execute request: %v": "Impossible d'exécuter cette requête: %v", "Unable to execute request: %v": "Impossible d'exécuter cette requête: %v",
"Last Parsing Error": "Dernière erreur d'analyse", "Last Parsing Error": "Dernière erreur d'analyse",
"There is a problem with this feed": "Il y a un problème avec cet abonnement", "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{ var TranslationsChecksums = map[string]string{
"en_US": "6fe95384260941e8a5a3c695a655a932e0a8a6a572c1e45cb2b1ae8baa01b897", "en_US": "6fe95384260941e8a5a3c695a655a932e0a8a6a572c1e45cb2b1ae8baa01b897",
"fr_FR": "ec81374ce72a1cf3a06bda027faa338745a77035f8bd3504ad4d452958260525", "fr_FR": "e9b3753645cb83a338f48bdc24825e629d568ebd3a65a4be2978ff6b4f3bc380",
} }

View file

@ -110,5 +110,8 @@
"Unable to execute request: %v": "Impossible d'exécuter cette requête: %v", "Unable to execute request: %v": "Impossible d'exécuter cette requête: %v",
"Last Parsing Error": "Dernière erreur d'analyse", "Last Parsing Error": "Dernière erreur d'analyse",
"There is a problem with this feed": "Il y a un problème avec cet abonnement", "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"
} }

View file

@ -6,10 +6,11 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"fmt"
"github.com/miniflux/miniflux2/model"
"io" "io"
"github.com/miniflux/miniflux2/errors"
"github.com/miniflux/miniflux2/model"
"golang.org/x/net/html/charset" "golang.org/x/net/html/charset"
) )
@ -21,7 +22,7 @@ func Parse(data io.Reader) (*model.Feed, error) {
err := decoder.Decode(atomFeed) err := decoder.Decode(atomFeed)
if err != nil { 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 return atomFeed.Transform(), nil

View file

@ -8,6 +8,8 @@ import (
"bytes" "bytes"
"testing" "testing"
"time" "time"
"github.com/miniflux/miniflux2/errors"
) )
func TestParseAtomSample(t *testing.T) { 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) 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")
}
}

View file

@ -6,9 +6,10 @@ package json
import ( import (
"encoding/json" "encoding/json"
"fmt"
"github.com/miniflux/miniflux2/model"
"io" "io"
"github.com/miniflux/miniflux2/errors"
"github.com/miniflux/miniflux2/model"
) )
// Parse returns a normalized feed struct. // Parse returns a normalized feed struct.
@ -16,7 +17,7 @@ func Parse(data io.Reader) (*model.Feed, error) {
jsonFeed := new(JsonFeed) jsonFeed := new(JsonFeed)
decoder := json.NewDecoder(data) decoder := json.NewDecoder(data)
if err := decoder.Decode(&jsonFeed); err != nil { 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 return jsonFeed.Transform(), nil

View file

@ -9,6 +9,8 @@ import (
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/miniflux/miniflux2/errors"
) )
func TestParseJsonFeed(t *testing.T) { 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) 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")
}
}

View file

@ -6,9 +6,9 @@ package rss
import ( import (
"encoding/xml" "encoding/xml"
"fmt"
"io" "io"
"github.com/miniflux/miniflux2/errors"
"github.com/miniflux/miniflux2/model" "github.com/miniflux/miniflux2/model"
"golang.org/x/net/html/charset" "golang.org/x/net/html/charset"
@ -22,7 +22,7 @@ func Parse(data io.Reader) (*model.Feed, error) {
err := decoder.Decode(feed) err := decoder.Decode(feed)
if err != nil { 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 return feed.Transform(), nil

View file

@ -8,6 +8,8 @@ import (
"bytes" "bytes"
"testing" "testing"
"time" "time"
"github.com/miniflux/miniflux2/errors"
) )
func TestParseRss2Sample(t *testing.T) { func TestParseRss2Sample(t *testing.T) {
@ -541,4 +543,8 @@ func TestParseInvalidXml(t *testing.T) {
if err == nil { if err == nil {
t.Error("Parse should returns an error") t.Error("Parse should returns an error")
} }
if _, ok := err.(errors.LocalizedError); !ok {
t.Error("The error returned must be a LocalizedError")
}
} }

View file

@ -4,8 +4,12 @@
package opml package opml
import "testing" import (
import "bytes" "bytes"
"testing"
"github.com/miniflux/miniflux2/errors"
)
func TestParseOpmlWithoutCategories(t *testing.T) { func TestParseOpmlWithoutCategories(t *testing.T) {
data := `<?xml version="1.0" encoding="ISO-8859-1"?> data := `<?xml version="1.0" encoding="ISO-8859-1"?>
@ -121,18 +125,13 @@ func TestParseOpmlWithEmptyTitleAndEmptySiteURL(t *testing.T) {
} }
func TestParseInvalidXML(t *testing.T) { func TestParseInvalidXML(t *testing.T) {
data := `<?xml version="1.0" encoding="ISO-8859-1"?> data := `garbage`
<opml version="2.0">
<head>
</head>
<body>
<outline
</body>
</opml>
`
_, err := Parse(bytes.NewBufferString(data)) _, err := Parse(bytes.NewBufferString(data))
if err == nil { if err == nil {
t.Error("Parse should generate an error") t.Error("Parse should generate an error")
} }
if _, ok := err.(errors.LocalizedError); !ok {
t.Error("The error returned must be a LocalizedError")
}
} }

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // 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 package static

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // 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 package static

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // 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 package static

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // 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 package template

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // 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 package template

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // 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 package sql