Unify error messages

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

11
link.go
View File

@@ -2,7 +2,6 @@ package atom
import (
"encoding/xml"
"errors"
"fmt"
"strings"
)
@@ -27,28 +26,28 @@ func NewLink(href string) *Link {
// error.
func (l *Link) Check() error {
if l.Href == "" {
return errors.New("href attribute of link empty")
return fmt.Errorf("href attribute of link %v empty", l)
} else {
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 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 !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 !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)
}
}