Unify error messages

This commit is contained in:
Jason Streifling 2024-10-18 19:04:08 +02:00
parent 411cd89b7c
commit d4e7bce5e2
20 changed files with 92 additions and 110 deletions

View File

@ -2,7 +2,6 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"html" "html"
) )
@ -29,17 +28,17 @@ func (c *Category) SetLabel(label string) {
// error. // error.
func (c *Category) Check() error { func (c *Category) Check() error {
if c.Term == "" { if c.Term == "" {
return errors.New("term attribute of category empty") return fmt.Errorf("term attribute of category %v empty", c)
} }
if c.Scheme != "" { if c.Scheme != "" {
if !isValidIRI(c.Scheme) { if !isValidIRI(c.Scheme) {
return fmt.Errorf("scheme attribute %v of category not correctly formatted", c.Scheme) return fmt.Errorf("scheme attribute of category %v not correctly formatted", c)
} }
} }
if !isCorrectlyEscaped(c.Label) { if !isCorrectlyEscaped(c.Label) {
return fmt.Errorf("label attribute %v of category not correctly escaped", c.Label) return fmt.Errorf("label attribute of category %v not correctly escaped", c)
} }
return nil return nil

View File

@ -1,8 +1,6 @@
package atom package atom
import ( import "encoding/xml"
"encoding/xml"
)
type CommonAttributes struct { type CommonAttributes struct {
Base IRI `xml:"base,attr,omitempty"` Base IRI `xml:"base,attr,omitempty"`

View File

@ -1,8 +1,6 @@
package atom package atom
import ( import "fmt"
"fmt"
)
const ( const (
InlineText = iota InlineText = iota

View File

@ -2,7 +2,6 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"strings" "strings"
"time" "time"
@ -40,16 +39,16 @@ func (e *Entry) checkAuthors(authorInFeed bool) error {
if e.Authors == nil { if e.Authors == nil {
if !authorInFeed { if !authorInFeed {
if e.Source == nil { if e.Source == nil {
return errors.New("no authors set in entry") return fmt.Errorf("no authors set in entry %v", e)
} }
if e.Source.Authors == nil { if e.Source.Authors == nil {
return errors.New("no authors set in entry") return fmt.Errorf("no authors set in entry %v", e)
} }
} }
} else { } else {
for i, a := range e.Authors { for i, a := range e.Authors {
if err := a.Check(); err != nil { if err := a.Check(); err != nil {
return fmt.Errorf("author element %v of entry: %v", i, err) return fmt.Errorf("author element %v of entry %v: %v", i, e, err)
} }
} }
} }
@ -140,72 +139,72 @@ func (e *Entry) AddExtension(x *ExtensionElement) {
// error. // error.
func (e *Entry) Check() error { func (e *Entry) Check() error {
if e.ID == nil { if e.ID == nil {
return errors.New("no id element of entry") return fmt.Errorf("no id element of entry %v", e)
} else { } else {
if err := e.ID.Check(); err != nil { if err := e.ID.Check(); err != nil {
return fmt.Errorf("id element of entry: %v", err) return fmt.Errorf("id element of entry %v: %v", e, err)
} }
} }
if err := e.checkAuthors(true); err != nil { if err := e.checkAuthors(true); err != nil {
return fmt.Errorf("entry %v: %v", e.ID.URI, err) return fmt.Errorf("entry %v: %v", e, err)
} }
for i, c := range e.Categories { for i, c := range e.Categories {
if err := c.Check(); err != nil { if err := c.Check(); err != nil {
return fmt.Errorf("category element %v of entry %v: %v", i, e.ID.URI, err) return fmt.Errorf("category element %v of entry %v: %v", i, e, err)
} }
} }
if e.Content != nil { if e.Content != nil {
if err := e.Content.Check(); err != nil { if err := e.Content.Check(); err != nil {
return fmt.Errorf("content element of entry %v: %v", e.ID.URI, err) return fmt.Errorf("content element of entry %v: %v", e, err)
} }
} else { } else {
// atom:entry elements that contain no child atom:content element MUST // atom:entry elements that contain no child atom:content element MUST
// contain at least one atom:link element with a rel attribute value of // contain at least one atom:link element with a rel attribute value of
// "alternate". // "alternate".
if !alternateRelExists(e.Links) { if !alternateRelExists(e.Links) {
return errors.New("no content element of entry %v and no link element with rel \"alternate\"") return fmt.Errorf("no content element of entry %v and no link element with rel \"alternate\"", e)
} }
} }
for i, c := range e.Contributors { for i, c := range e.Contributors {
if err := c.Check(); err != nil { if err := c.Check(); err != nil {
return fmt.Errorf("contributor element %v of entry %v: %v", i, e.ID.URI, err) return fmt.Errorf("contributor element %v of entry %v: %v", i, e, err)
} }
} }
for i, l := range e.Links { for i, l := range e.Links {
if err := l.Check(); err != nil { if err := l.Check(); err != nil {
return fmt.Errorf("link element %v of entry %v: %v", i, e.ID.URI, err) return fmt.Errorf("link element %v of entry %v: %v", i, e, err)
} }
} }
if hasAlternateDuplicateLinks(e.Links) { if hasAlternateDuplicateLinks(e.Links) {
return fmt.Errorf("links with with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in entry %v", e.ID.URI) return fmt.Errorf("links with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in entry %v", e)
} }
if e.Published != nil { if e.Published != nil {
if err := e.Published.Check(); err != nil { if err := e.Published.Check(); err != nil {
return fmt.Errorf("published element of entry %v: %v", e.ID.URI, err) return fmt.Errorf("published element of entry %v: %v", e, err)
} }
} }
if e.Rights != nil { if e.Rights != nil {
if err := e.Rights.Check(); err != nil { if err := e.Rights.Check(); err != nil {
return fmt.Errorf("rights element of entry %v: %v", e.ID.URI, err) return fmt.Errorf("rights element of entry %v: %v", e, err)
} }
} }
if e.Source != nil { if e.Source != nil {
if err := e.Source.Check(); err != nil { if err := e.Source.Check(); err != nil {
return fmt.Errorf("source element of entry %v: %v", e.ID.URI, err) return fmt.Errorf("source element of entry %v: %v", e, err)
} }
} }
if e.Summary != nil { if e.Summary != nil {
if err := e.Summary.Check(); err != nil { if err := e.Summary.Check(); err != nil {
return fmt.Errorf("summary element of entry %v: %v", e.ID.URI, err) return fmt.Errorf("summary element of entry %v: %v", e, err)
} }
} else { } else {
// atom:entry elements MUST contain an atom:summary element in either // atom:entry elements MUST contain an atom:summary element in either
@ -213,7 +212,7 @@ func (e *Entry) Check() error {
// the atom:entry contains an atom:content that has a "src" attribute // the atom:entry contains an atom:content that has a "src" attribute
// (and is thus empty). // (and is thus empty).
if e.Content.hasSRC() { if e.Content.hasSRC() {
return fmt.Errorf("no summary element of entry %v but content of type out of line content", e.ID.URI) return fmt.Errorf("no summary element of entry %v but content of type out of line content", e)
} }
// the atom:entry contains content that is encoded in Base64; i.e., the // the atom:entry contains content that is encoded in Base64; i.e., the
// "type" attribute of atom:content is a MIME media type [MIMEREG], but // "type" attribute of atom:content is a MIME media type [MIMEREG], but
@ -221,29 +220,29 @@ func (e *Entry) Check() error {
// does not end with "/xml" or "+xml". // does not end with "/xml" or "+xml".
mediaType := e.Content.getType() mediaType := e.Content.getType()
if isValidMediaType(mediaType) && !isXMLMediaType(mediaType) && !strings.HasPrefix(mediaType, "text/") { if isValidMediaType(mediaType) && !isXMLMediaType(mediaType) && !strings.HasPrefix(mediaType, "text/") {
return fmt.Errorf("no summary element of entry %v but media type not xml", e.ID.URI) return fmt.Errorf("no summary element of entry %v but media type not xml", e)
} }
} }
if e.Title == nil { if e.Title == nil {
return fmt.Errorf("no title element of entry %v", e.ID.URI) return fmt.Errorf("no title element of entry %v", e)
} else { } else {
if err := e.Title.Check(); err != nil { if err := e.Title.Check(); err != nil {
return fmt.Errorf("title element of entry %v: %v", e.ID.URI, err) return fmt.Errorf("title element of entry %v: %v", e, err)
} }
} }
if e.Updated == nil { if e.Updated == nil {
return fmt.Errorf("no updated element of entry %v", e.ID.URI) return fmt.Errorf("no updated element of entry %v", e)
} else { } else {
if err := e.Updated.Check(); err != nil { if err := e.Updated.Check(); err != nil {
return fmt.Errorf("updated element of entry %v: %v", e.ID.URI, err) return fmt.Errorf("updated element of entry %v: %v", e, err)
} }
} }
for i, x := range e.Extensions { for i, x := range e.Extensions {
if err := x.Check(); err != nil { if err := x.Check(); err != nil {
return fmt.Errorf("extension element %v of entry %v: %v", i, e.ID.URI, err) return fmt.Errorf("extension element %v of entry %v: %v", i, e, err)
} }
} }

View File

@ -20,7 +20,7 @@ func NewExtensionElement(name string, value any) *ExtensionElement {
// returns an error. // returns an error.
func (e *ExtensionElement) Check() error { func (e *ExtensionElement) Check() error {
if e.Value == nil { if e.Value == nil {
return errors.New("value element of extension element empty") return errors.New("value element of extension empty")
} }
return nil return nil

39
feed.go
View File

@ -2,7 +2,6 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"time" "time"
) )
@ -121,10 +120,10 @@ func (f *Feed) AddEntry(e *Entry) {
// error. // error.
func (f *Feed) Check() error { func (f *Feed) Check() error {
if f.ID == nil { if f.ID == nil {
return errors.New("no id element of feed") return fmt.Errorf("no id element of feed %v", f)
} else { } else {
if err := f.ID.Check(); err != nil { if err := f.ID.Check(); err != nil {
return fmt.Errorf("id element of feed: %v", err) return fmt.Errorf("id element of feed %v: %v", f, err)
} }
} }
@ -134,93 +133,93 @@ func (f *Feed) Check() error {
if f.Authors == nil { if f.Authors == nil {
for _, e := range f.Entries { for _, e := range f.Entries {
if err := e.checkAuthors(false); err != nil { if err := e.checkAuthors(false); err != nil {
return fmt.Errorf("no authors set in feed %v: %v", f.ID.URI, err) return fmt.Errorf("no authors set in feed %v: %v", f, err)
} }
} }
} else { } else {
for i, a := range f.Authors { for i, a := range f.Authors {
if err := a.Check(); err != nil { if err := a.Check(); err != nil {
return fmt.Errorf("author element %v of feed %v: %v", i, f.ID.URI, err) return fmt.Errorf("author element %v of feed %v: %v", i, f, err)
} }
} }
} }
for i, c := range f.Categories { for i, c := range f.Categories {
if err := c.Check(); err != nil { if err := c.Check(); err != nil {
return fmt.Errorf("category element %v of feed %v: %v", i, f.ID.URI, err) return fmt.Errorf("category element %v of feed %v: %v", i, f, err)
} }
} }
for i, c := range f.Contributors { for i, c := range f.Contributors {
if err := c.Check(); err != nil { if err := c.Check(); err != nil {
return fmt.Errorf("contributor element %v of feed %v: %v", i, f.ID.URI, err) return fmt.Errorf("contributor element %v of feed %v: %v", i, f, err)
} }
} }
if f.Generator != nil { if f.Generator != nil {
if err := f.Generator.Check(); err != nil { if err := f.Generator.Check(); err != nil {
return fmt.Errorf("generator element of feed %v: %v", f.ID.URI, err) return fmt.Errorf("generator element of feed %v: %v", f, err)
} }
} }
if f.Icon != nil { if f.Icon != nil {
if err := f.Icon.Check(); err != nil { if err := f.Icon.Check(); err != nil {
return fmt.Errorf("icon element of feed %v: %v", f.ID.URI, err) return fmt.Errorf("icon element of feed %v: %v", f, err)
} }
} }
for i, l := range f.Links { for i, l := range f.Links {
if err := l.Check(); err != nil { if err := l.Check(); err != nil {
return fmt.Errorf("link element %v of feed %v: %v", i, f.ID.URI, err) return fmt.Errorf("link element %v of feed %v: %v", i, f, err)
} }
} }
if hasAlternateDuplicateLinks(f.Links) { if hasAlternateDuplicateLinks(f.Links) {
return fmt.Errorf("links with with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in feed %v", f.ID.URI) return fmt.Errorf("links with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in feed %v", f)
} }
if f.Logo != nil { if f.Logo != nil {
if err := f.Logo.Check(); err != nil { if err := f.Logo.Check(); err != nil {
return fmt.Errorf("logo element of feed %v: %v", f.ID.URI, err) return fmt.Errorf("logo element of feed %v: %v", f, err)
} }
} }
if f.Rights != nil { if f.Rights != nil {
if err := f.Rights.Check(); err != nil { if err := f.Rights.Check(); err != nil {
return fmt.Errorf("rights element of feed %v: %v", f.ID.URI, err) return fmt.Errorf("rights element of feed %v: %v", f, err)
} }
} }
if f.Subtitle != nil { if f.Subtitle != nil {
if err := f.Subtitle.Check(); err != nil { if err := f.Subtitle.Check(); err != nil {
return fmt.Errorf("subtitle element of feed %v: %v", f.ID.URI, err) return fmt.Errorf("subtitle element of feed %v: %v", f, err)
} }
} }
if f.Title == nil { if f.Title == nil {
return fmt.Errorf("no title element of feed %v", f.ID.URI) return fmt.Errorf("no title element of feed %v", f)
} else { } else {
if err := f.Title.Check(); err != nil { if err := f.Title.Check(); err != nil {
return fmt.Errorf("title element of feed %v: %v", f.ID.URI, err) return fmt.Errorf("title element of feed %v: %v", f, err)
} }
} }
if f.Updated == nil { if f.Updated == nil {
return fmt.Errorf("no updated element of feed %v", f.ID) return fmt.Errorf("no updated element of feed %v", f)
} else { } else {
if err := f.Updated.Check(); err != nil { if err := f.Updated.Check(); err != nil {
return fmt.Errorf("updated element of feed %v: %v", f.ID.URI, err) return fmt.Errorf("updated element of feed %v: %v", f, err)
} }
} }
for i, x := range f.Extensions { for i, x := range f.Extensions {
if err := x.Check(); err != nil { if err := x.Check(); err != nil {
return fmt.Errorf("extension element %v of feed %v: %v", i, f.ID.URI, err) return fmt.Errorf("extension element %v of feed %v: %v", i, f, err)
} }
} }
for i, n := range f.Entries { for i, n := range f.Entries {
if err := n.Check(); err != nil { if err := n.Check(); err != nil {
return fmt.Errorf("entry element %v of feed %v: %v", i, f.ID.URI, err) return fmt.Errorf("entry element %v of feed %v: %v", i, f, err)
} }
} }

View File

@ -2,7 +2,6 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"html" "html"
) )
@ -25,16 +24,16 @@ func NewGenerator(text string) *Generator {
func (g *Generator) Check() error { func (g *Generator) Check() error {
if g.URI != "" { if g.URI != "" {
if !isValidIRI(g.URI) { if !isValidIRI(g.URI) {
return fmt.Errorf("uri attribute %v of generator not correctly formatted", g.URI) return fmt.Errorf("uri attribute of generator %v not correctly formatted", g)
} }
} }
if g.Text == "" { if g.Text == "" {
return errors.New("text element of generator empty") return fmt.Errorf("text element of generator %v empty", g)
} }
if !isCorrectlyEscaped(g.Text) { if !isCorrectlyEscaped(g.Text) {
return fmt.Errorf("text element %v of generator not correctly escaped", g.Text) return fmt.Errorf("text element of generator %v not correctly escaped", g)
} }
return nil return nil

View File

@ -28,7 +28,7 @@ func (i *Icon) Check() error {
return errors.New("uri element of icon empty") return errors.New("uri element of icon empty")
} else { } else {
if !isValidIRI(i.URI) { if !isValidIRI(i.URI) {
return fmt.Errorf("uri attribute %v of icon not correctly formatted", i.URI) return fmt.Errorf("uri attribute of icon %v not correctly formatted", i)
} }
} }

2
id.go
View File

@ -27,7 +27,7 @@ func (i *ID) Check() error {
return errors.New("uri element of id empty") return errors.New("uri element of id empty")
} else { } else {
if !isValidIRI(i.URI) { if !isValidIRI(i.URI) {
return fmt.Errorf("uri element %v of id not correctly formatted", i.URI) return fmt.Errorf("uri element of id %v not correctly formatted", i)
} }
} }

View File

@ -2,7 +2,6 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"mime" "mime"
) )
@ -45,7 +44,7 @@ func (i *InlineOtherContent) Check() error {
} }
if isCompositeMediaType(mediaType) { if isCompositeMediaType(mediaType) {
return errors.New("type attribute of inline other content must not be a composite type") return fmt.Errorf("type attribute of inline other content %v must not be a composite type", i)
} }
return nil return nil

View File

@ -2,7 +2,6 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
) )
@ -43,7 +42,7 @@ func (i *InlineTextContent) getType() string { return i.Type }
// returns an error. // returns an error.
func (i *InlineTextContent) Check() error { func (i *InlineTextContent) Check() error {
if i.Type != "" && i.Type != "text" && i.Type != "html" { if i.Type != "" && i.Type != "text" && i.Type != "html" {
return errors.New("type attribute of inline text content must be text or html if not omitted") return fmt.Errorf("type attribute of inline text content %v must be text or html if not omitted", i)
} }
return nil return nil

View File

@ -2,7 +2,6 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
) )
@ -43,11 +42,11 @@ func (i *InlineXHTMLContent) getType() string { return i.Type }
// returns an error. // returns an error.
func (i *InlineXHTMLContent) Check() error { func (i *InlineXHTMLContent) Check() error {
if i.Type != "xhtml" { if i.Type != "xhtml" {
return errors.New("type attribute of inline xhtml content must be xhtml") return fmt.Errorf("type attribute of inline xhtml content %v must be xhtml", i)
} }
if err := i.XHTMLDiv.Check(); err != nil { if err := i.XHTMLDiv.Check(); err != nil {
return fmt.Errorf("xhtml div element %v of inline xhtml content %v: %v", i.XHTMLDiv, i, err) return fmt.Errorf("xhtml div element of inline xhtml content %v: %v", i, err)
} }
return nil return nil

11
link.go
View File

@ -2,7 +2,6 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"strings" "strings"
) )
@ -27,28 +26,28 @@ func NewLink(href string) *Link {
// error. // error.
func (l *Link) Check() error { func (l *Link) Check() error {
if l.Href == "" { if l.Href == "" {
return errors.New("href attribute of link empty") return fmt.Errorf("href attribute of link %v empty", l)
} else { } else {
if !isValidIRI(l.Href) { if !isValidIRI(l.Href) {
return fmt.Errorf("href attribute %v of link not correctly formatted", l.Href) return fmt.Errorf("href attribute of link %v not correctly formatted", l)
} }
} }
if l.Rel != "" { if l.Rel != "" {
if strings.Contains(l.Rel, ":") && !isValidIRI(IRI(l.Rel)) { if strings.Contains(l.Rel, ":") && !isValidIRI(IRI(l.Rel)) {
return fmt.Errorf("rel attribute %v of link %v not correctly formatted", l.Rel, l.Href) return fmt.Errorf("rel attribute of link %v not correctly formatted", l)
} }
} }
if l.Type != "" { if l.Type != "" {
if !isValidMediaType(string(l.Type)) { if !isValidMediaType(string(l.Type)) {
return fmt.Errorf("type attribute %v of link %v invalid media type", l.Type, l.Href) return fmt.Errorf("type attribute of link %v invalid media type", l)
} }
} }
if l.HrefLang != "" { if l.HrefLang != "" {
if !isValidLanguageTag(l.HrefLang) { if !isValidLanguageTag(l.HrefLang) {
return fmt.Errorf("hreflang attribute %v of link %v invalid language tag", l.Type, l.HrefLang) return fmt.Errorf("hreflang attribute of link %v invalid language tag", l)
} }
} }

View File

@ -2,7 +2,6 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
) )
@ -25,10 +24,10 @@ func NewLogo(uri IRI) (*Logo, error) {
// error. // error.
func (l *Logo) Check() error { func (l *Logo) Check() error {
if l.URI == "" { if l.URI == "" {
return errors.New("uri element of logo empty") return fmt.Errorf("uri element of logo %v empty", l)
} else { } else {
if !isValidIRI(l.URI) { if !isValidIRI(l.URI) {
return fmt.Errorf("uri element %v of logo not correctly formatted", l.URI) return fmt.Errorf("uri element of logo %v not correctly formatted", l)
} }
} }

View File

@ -2,7 +2,6 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"mime" "mime"
) )
@ -27,7 +26,7 @@ func newOutOfLineContent(mediaType string, content any) (*OutOfLineContent, erro
} }
if !isValidIRI(iri) { if !isValidIRI(iri) {
return nil, errors.New("content not a valid uri") return nil, fmt.Errorf("content %v not a valid uri", iri)
} }
return &OutOfLineContent{Type: MediaType(mediaType), SRC: iri}, nil return &OutOfLineContent{Type: MediaType(mediaType), SRC: iri}, nil
@ -54,11 +53,11 @@ func (o *OutOfLineContent) Check() error {
} }
if isCompositeMediaType(mediaType) { if isCompositeMediaType(mediaType) {
return errors.New("type attribute of out of line content must not be a composite type") return fmt.Errorf("type attribute of out of line content %v must not be a composite type", o)
} }
if o.SRC == "" { if o.SRC == "" {
return errors.New("src attribute of out of line content empty") return fmt.Errorf("src attribute of out of line content %v empty", o)
} }
return nil return nil

View File

@ -1,7 +1,6 @@
package atom package atom
import ( import (
"errors"
"fmt" "fmt"
"net/mail" "net/mail"
) )
@ -33,25 +32,25 @@ func (p *Person) AddExtension(e *ExtensionElement) {
// error. // error.
func (p *Person) Check() error { func (p *Person) Check() error {
if p.Name == "" { if p.Name == "" {
return errors.New("name element of person element empty") return fmt.Errorf("name element of person %v empty", p)
} }
if p.URI != "" { if p.URI != "" {
if !isValidIRI(p.URI) { if !isValidIRI(p.URI) {
return fmt.Errorf("uri element of person %v not correctly formatted", p.Name) return fmt.Errorf("uri element of person %v not correctly formatted", p)
} }
} }
if p.Email != "" { if p.Email != "" {
if _, err := mail.ParseAddress(string(p.Email)); err != nil { if _, err := mail.ParseAddress(string(p.Email)); err != nil {
return fmt.Errorf("email element of person %v not correctly formatted", p.Name) return fmt.Errorf("email element of person %v not correctly formatted", p)
} }
} }
if p.Extensions != nil { if p.Extensions != nil {
for i, e := range p.Extensions { for i, e := range p.Extensions {
if err := e.Check(); err != nil { if err := e.Check(); err != nil {
return fmt.Errorf("extension element %v of person %v: %v", i, p.Name, err) return fmt.Errorf("extension element %v of person %v: %v", i, p, err)
} }
} }
} }

View File

@ -1,6 +1,6 @@
package atom package atom
import "errors" import "fmt"
type PlainText struct { type PlainText struct {
*CommonAttributes *CommonAttributes
@ -15,15 +15,15 @@ func (p *PlainText) isText() bool { return true }
// error. // error.
func (p *PlainText) Check() error { func (p *PlainText) Check() error {
if p.Type != "" && p.Type != "text" && p.Type != "html" { if p.Type != "" && p.Type != "text" && p.Type != "html" {
return errors.New("type attribute of plain text must be text or html if not omitted") return fmt.Errorf("type attribute of plain text %v must be text or html if not omitted", p)
} }
if p.Type == "html" && !isCorrectlyEscaped(p.Text) { if p.Type == "html" && !isCorrectlyEscaped(p.Text) {
return errors.New("text element of plain text not correctly escaped") return fmt.Errorf("text element of plain text %v not correctly escaped", p)
} }
if p.Text == "" { if p.Text == "" {
return errors.New("text element of plain text empty") return fmt.Errorf("text element of plain text %v empty", p)
} }
return nil return nil

View File

@ -28,79 +28,79 @@ type Source struct {
func (s *Source) Check() error { func (s *Source) Check() error {
for i, a := range s.Authors { for i, a := range s.Authors {
if err := a.Check(); err != nil { if err := a.Check(); err != nil {
return fmt.Errorf("author element %v of source %v: %v", i, s.ID.URI, err) return fmt.Errorf("author element %v of source %v: %v", i, s, err)
} }
} }
for i, c := range s.Categories { for i, c := range s.Categories {
if err := c.Check(); err != nil { if err := c.Check(); err != nil {
return fmt.Errorf("category element %v of source %v: %v", i, s.ID.URI, err) return fmt.Errorf("category element %v of source %v: %v", i, s, err)
} }
} }
for i, c := range s.Contributors { for i, c := range s.Contributors {
if err := c.Check(); err != nil { if err := c.Check(); err != nil {
return fmt.Errorf("contributor element %v of source %v: %v", i, s.ID.URI, err) return fmt.Errorf("contributor element %v of source %v: %v", i, s, err)
} }
} }
if s.Generator != nil { if s.Generator != nil {
if err := s.Generator.Check(); err != nil { if err := s.Generator.Check(); err != nil {
return fmt.Errorf("generator element of source %v: %v", s.ID.URI, err) return fmt.Errorf("generator element of source %v: %v", s, err)
} }
} }
if s.Icon != nil { if s.Icon != nil {
if err := s.Icon.Check(); err != nil { if err := s.Icon.Check(); err != nil {
return fmt.Errorf("icon element of source %v: %v", s.ID.URI, err) return fmt.Errorf("icon element of source %v: %v", s, err)
} }
} }
if s.ID != nil { if s.ID != nil {
if err := s.ID.Check(); err != nil { if err := s.ID.Check(); err != nil {
return fmt.Errorf("id element of source %v: %v", s.ID.URI, err) return fmt.Errorf("id element of source %v: %v", s, err)
} }
} }
for i, l := range s.Links { for i, l := range s.Links {
if err := l.Check(); err != nil { if err := l.Check(); err != nil {
return fmt.Errorf("link element %v of source %v: %v", i, s.ID.URI, err) return fmt.Errorf("link element %v of source %v: %v", i, s, err)
} }
} }
if s.Logo != nil { if s.Logo != nil {
if err := s.Logo.Check(); err != nil { if err := s.Logo.Check(); err != nil {
return fmt.Errorf("logo element of source %v: %v", s.ID.URI, err) return fmt.Errorf("logo element of source %v: %v", s, err)
} }
} }
if s.Rights != nil { if s.Rights != nil {
if err := s.Rights.Check(); err != nil { if err := s.Rights.Check(); err != nil {
return fmt.Errorf("rights element of source %v: %v", s.ID.URI, err) return fmt.Errorf("rights element of source %v: %v", s, err)
} }
} }
if s.Subtitle != nil { if s.Subtitle != nil {
if err := s.Subtitle.Check(); err != nil { if err := s.Subtitle.Check(); err != nil {
return fmt.Errorf("subtitle element of source %v: %v", s.ID.URI, err) return fmt.Errorf("subtitle element of source %v: %v", s, err)
} }
} }
if s.Title != nil { if s.Title != nil {
if err := s.Title.Check(); err != nil { if err := s.Title.Check(); err != nil {
return fmt.Errorf("title element of source %v: %v", s.ID.URI, err) return fmt.Errorf("title element of source %v: %v", s, err)
} }
} }
if s.Updated != nil { if s.Updated != nil {
if err := s.Updated.Check(); err != nil { if err := s.Updated.Check(); err != nil {
return fmt.Errorf("updated element of source %v: %v", s.ID.URI, err) return fmt.Errorf("updated element of source %v: %v", s, err)
} }
} }
for i, e := range s.Extensions { for i, e := range s.Extensions {
if err := e.Check(); err != nil { if err := e.Check(); err != nil {
return fmt.Errorf("extension element %v of source %v: %v", i, s.ID.URI, err) return fmt.Errorf("extension element %v of source %v: %v", i, s, err)
} }
} }

View File

@ -2,7 +2,7 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors" "fmt"
) )
type XHTMLDiv struct { type XHTMLDiv struct {
@ -23,7 +23,7 @@ func NewXHTMLDiv(content string) *XHTMLDiv {
// error. // error.
func (x *XHTMLDiv) Check() error { func (x *XHTMLDiv) Check() error {
if x.XMLNS != "http://www.w3.org/1999/xhtml" { if x.XMLNS != "http://www.w3.org/1999/xhtml" {
return errors.New("xmlns attribute of xhtml text must be http://www.w3.org/1999/xhtml") return fmt.Errorf("xmlns attribute of xhtml text %v must be http://www.w3.org/1999/xhtml", x)
} }
return nil return nil

View File

@ -1,9 +1,6 @@
package atom package atom
import ( import "fmt"
"errors"
"fmt"
)
type XHTMLText struct { type XHTMLText struct {
*CommonAttributes *CommonAttributes
@ -18,7 +15,7 @@ func (x *XHTMLText) isText() bool { return true }
// error. // error.
func (x *XHTMLText) Check() error { func (x *XHTMLText) Check() error {
if x.Type != "xhtml" { if x.Type != "xhtml" {
return errors.New("type attribute of xhtml text must be xhtml") return fmt.Errorf("type attribute of xhtml text %v must be xhtml", x)
} }
if err := x.XHTMLDiv.Check(); err != nil { if err := x.XHTMLDiv.Check(); err != nil {