Added specific error types rather than generic error types

This commit is contained in:
Ben Davies 2014-09-25 13:27:05 +01:00
parent 326d76d501
commit d4964c8450
2 changed files with 15 additions and 6 deletions

13
feed.go
View File

@ -26,7 +26,6 @@
package feeder package feeder
import ( import (
"errors"
"fmt" "fmt"
xmlx "github.com/jteeuwen/go-pkg-xmlx" xmlx "github.com/jteeuwen/go-pkg-xmlx"
"net/http" "net/http"
@ -35,6 +34,15 @@ import (
"time" "time"
) )
type UnsupportedFeedError struct {
Type string
Version [2]int
}
func (err *UnsupportedFeedError) Error() string {
return fmt.Sprintf("Unsupported feed: %s, version: %+v", err.Type, err.Version)
}
type ChannelHandler func(f *Feed, newchannels []*Channel) type ChannelHandler func(f *Feed, newchannels []*Channel)
type ItemHandler func(f *Feed, ch *Channel, newitems []*Item) type ItemHandler func(f *Feed, ch *Channel, newitems []*Item)
@ -150,8 +158,7 @@ func (this *Feed) makeFeed(doc *xmlx.Document) (err error) {
this.Type, this.Version = this.GetVersionInfo(doc) this.Type, this.Version = this.GetVersionInfo(doc)
if ok := this.testVersions(); !ok { if ok := this.testVersions(); !ok {
err = errors.New(fmt.Sprintf("Unsupported feed: %s, version: %+v", this.Type, this.Version)) return &UnsupportedFeedError{Type: this.Type, Version: this.Version}
return
} }
if err = this.buildFeed(doc); err != nil || len(this.Channels) == 0 { if err = this.buildFeed(doc); err != nil || len(this.Channels) == 0 {

8
rss.go
View File

@ -1,11 +1,13 @@
package feeder package feeder
import ( import (
"errors"
xmlx "github.com/jteeuwen/go-pkg-xmlx" xmlx "github.com/jteeuwen/go-pkg-xmlx"
) )
type MissingRssNodeError struct{}
func (err *MissingRssNodeError) Error() string { return "Failed to find rss/rdf node in XML." }
type Extension struct { type Extension struct {
Name string Name string
Value string Value string
@ -37,7 +39,7 @@ func (this *Feed) readRss2(doc *xmlx.Document) (err error) {
} }
if root == nil { if root == nil {
return errors.New("Failed to find rss/rdf node in XML.") return &MissingRssNodeError{}
} }
channels := root.SelectNodes(ns, "channel") channels := root.SelectNodes(ns, "channel")