present errors that occur

This commit is contained in:
yequari 2024-03-03 14:51:19 -07:00
parent bdc93d5596
commit 01f7904b14
5 changed files with 16 additions and 9 deletions

View File

@ -44,6 +44,7 @@ func (app *application) generateRss(w http.ResponseWriter, r *http.Request) {
feed := feedInfo.GenerateRSS() feed := feedInfo.GenerateRSS()
data := newTemplateData(r) data := newTemplateData(r)
data.Errors = feedInfo.Errors
data.Feeds = append(data.Feeds, feed) data.Feeds = append(data.Feeds, feed)
app.renderElem(w, http.StatusOK, "feed-output.tmpl.html", data) app.renderElem(w, http.StatusOK, "feed-output.tmpl.html", data)
} }

View File

@ -11,6 +11,7 @@ type templateData struct {
CurrentYear int CurrentYear int
Feeds []string Feeds []string
Title string Title string
Errors map[string]string
} }
func newTemplateCache() (map[string]*template.Template, error) { func newTemplateCache() (map[string]*template.Template, error) {

View File

@ -36,7 +36,7 @@ type FeedInfo struct {
SiteDesc string SiteDesc string
PageUrls []string PageUrls []string
Items []*FeedItem Items []*FeedItem
errors map[string]error Errors map[string]string
} }
type FeedItem struct { type FeedItem struct {
@ -99,7 +99,7 @@ func getHtmlElement(doc *html.Node, tag string) (*html.Node, error) {
} }
f(doc, tag) f(doc, tag)
if element == nil { if element == nil {
return nil, fmt.Errorf("no <%s> element found", tag) return nil, fmt.Errorf("no &lt;%s&gt; element found", tag)
} }
return element, nil return element, nil
} }
@ -148,7 +148,7 @@ func NewFeedItem(url string) (*FeedItem, error) {
item := FeedItem{ item := FeedItem{
Url: url, Url: url,
} }
item.ParseContent(rawhtml); err = item.ParseContent(rawhtml);
if err != nil { if err != nil {
return nil, fmt.Errorf("Could not parse feed item: %w", err) return nil, fmt.Errorf("Could not parse feed item: %w", err)
} }
@ -161,11 +161,12 @@ func NewFeedInfo(name, base_url, desc, author string, page_urls...string) (*Feed
SiteUrl: base_url, SiteUrl: base_url,
SiteDesc: desc, SiteDesc: desc,
PageUrls: page_urls, PageUrls: page_urls,
Errors: make(map[string]string, 10),
} }
for _,url := range info.PageUrls { for _,url := range info.PageUrls {
item, err := NewFeedItem(url) item, err := NewFeedItem(url)
if err != nil { if err != nil {
info.errors[url] = err info.Errors[url] = err.Error()
} else { } else {
info.Items = append(info.Items, item) info.Items = append(info.Items, item)
} }

View File

@ -1,5 +1,10 @@
{{ range .Feeds }}
<h2>Output</h2> <h2>Output</h2>
{{ range $url, $error := .Errors }}
<p class="error">
Error parsing page at {{ $url }}: {{ $error }}
</p>
{{end}}
{{ range .Feeds }}
<button id="copy" onclick="copyToClipboard()">Copy to Clipboard</button> <button id="copy" onclick="copyToClipboard()">Copy to Clipboard</button>
<textarea readonly="true" cols="80" rows="120" id="feed-output"> <textarea readonly="true" cols="80" rows="120" id="feed-output">
{{ . }} {{ . }}

View File

@ -1,7 +1,3 @@
#output {
white-space: pre;
}
body { body {
font-size: 16px; font-size: 16px;
} }
@ -19,3 +15,6 @@ input, textarea {
margin: 0 auto; margin: 0 auto;
} }
.error {
color: red;
}