diff --git a/internal/database/store.go b/internal/database/store.go index da78241..4f2f2c0 100644 --- a/internal/database/store.go +++ b/internal/database/store.go @@ -37,6 +37,7 @@ type ( CatalogName string `gorm:"primaryKey" json:"-"` CatalogTag string `gorm:"primaryKey" json:"-"` CurrentVersion string `json:"current_version,omitempty"` + Error string `json:"error,omitempty"` LastChecked *time.Time `json:"last_checked,omitempty"` VersionTime *time.Time `json:"version_time,omitempty"` } diff --git a/internal/fetcher/html.go b/internal/fetcher/html.go index b920baf..851c107 100644 --- a/internal/fetcher/html.go +++ b/internal/fetcher/html.go @@ -33,7 +33,11 @@ func (h HTMLFetcher) FetchVersion(ctx context.Context, attrs *fieldcollection.Fi return "", time.Time{}, errors.Wrap(err, "querying xpath") } - if node.Type == html.ElementNode && node.FirstChild.Type == html.TextNode { + if node == nil { + return "", time.Time{}, errors.New("xpath expression lead to nil-node") + } + + if node.Type == html.ElementNode && node.FirstChild != nil && node.FirstChild.Type == html.TextNode { node = node.FirstChild } diff --git a/scheduler.go b/scheduler.go index 0eebd3b..821c14a 100644 --- a/scheduler.go +++ b/scheduler.go @@ -60,13 +60,21 @@ func checkForUpdates(ce *database.CatalogEntry) error { logger.Debug("Checking for updates") ver, vertime, err := fetcher.Get(ce.Fetcher).FetchVersion(context.Background(), &ce.FetcherConfig) - if err != nil { - return errors.Wrap(err, "fetching version") - } - ver = strings.TrimPrefix(ver, "v") - if cm.CurrentVersion != ver { + switch { + + case err != nil: + log.WithField("entry", ce.Key()).WithError(err).Error("Fetcher caused error, error is stored in entry") + cm.Error = err.Error() + + case cm.CurrentVersion != ver: + + logger.WithFields(log.Fields{ + "from": cm.CurrentVersion, + "to": ver, + }).Info("Entry had version update") + if err = storage.Logs.Add(&database.LogEntry{ CatalogName: ce.Name, CatalogTag: ce.Tag, @@ -76,15 +84,16 @@ func checkForUpdates(ce *database.CatalogEntry) error { }); err != nil { return errors.Wrap(err, "adding log entry") } - logger.WithFields(log.Fields{ - "from": cm.CurrentVersion, - "to": ver, - }).Info("Entry had version update") + cm.VersionTime = func(v time.Time) *time.Time { return &v }(vertime) + cm.CurrentVersion = ver + fallthrough + + default: + cm.Error = "" + } - cm.CurrentVersion = ver cm.LastChecked = func(v time.Time) *time.Time { return &v }(time.Now()) - return errors.Wrap(storage.Catalog.PutMeta(cm), "updating meta entry") }