From 8489933c6ccb24b0c49b914d738bc109246aaabb Mon Sep 17 00:00:00 2001 From: Nicholas Westerhausen <2317381+nwesterhausen@users.noreply.github.com> Date: Tue, 9 Apr 2024 16:51:49 +0000 Subject: [PATCH] fix: provide an error message in the dashboard when WHOIS cache misses --- handlers/whois.handler.go | 11 +++++++++-- service/whois.service.go | 9 +++++---- views/domains/domain-dashboard.templ | 10 +++++----- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/handlers/whois.handler.go b/handlers/whois.handler.go index cef9be1..065b7b1 100644 --- a/handlers/whois.handler.go +++ b/handlers/whois.handler.go @@ -3,6 +3,7 @@ package handlers import ( "errors" + "github.com/a-h/templ" "github.com/labstack/echo/v4" "github.com/nwesterhausen/domain-monitor/service" "github.com/nwesterhausen/domain-monitor/views/domains" @@ -25,8 +26,14 @@ func (h *WhoisHandler) GetCard(c echo.Context) error { return errors.New("invalid domain to fetch (FQDN required)") } - whois := h.WhoisService.GetWhois(fqdn) - card := domains.WhoisDetail(whois) + var card templ.Component + whois, err := h.WhoisService.GetWhois(fqdn) + + if err != nil { + card = domains.WhoisError(err) + } else { + card = domains.WhoisDetail(whois) + } return View(c, card) } diff --git a/service/whois.service.go b/service/whois.service.go index 4717c10..bf13e9b 100644 --- a/service/whois.service.go +++ b/service/whois.service.go @@ -1,6 +1,7 @@ package service import ( + "errors" "log" "github.com/nwesterhausen/domain-monitor/configuration" @@ -14,15 +15,15 @@ func NewWhoisService(store configuration.WhoisCacheStorage) *ServicesWhois { return &ServicesWhois{store: store} } -func (s *ServicesWhois) GetWhois(fqdn string) configuration.WhoisCache { +func (s *ServicesWhois) GetWhois(fqdn string) (configuration.WhoisCache, error) { for _, entry := range s.store.FileContents.Entries { if entry.FQDN == fqdn { - return entry + return entry, nil } } - log.Println("WHOIS entry cache miss for", fqdn) + log.Println("🙅 WHOIS entry cache miss for", fqdn) - return configuration.WhoisCache{} + return configuration.WhoisCache{}, errors.New("WHOIS entry not found") } func (s *ServicesWhois) MarkAlertSent(fqdn string, alert configuration.Alert) bool { diff --git a/views/domains/domain-dashboard.templ b/views/domains/domain-dashboard.templ index 5ebe526..3e809fb 100644 --- a/views/domains/domain-dashboard.templ +++ b/views/domains/domain-dashboard.templ @@ -4,6 +4,7 @@ import ( "github.com/nwesterhausen/domain-monitor/configuration" "strings" "time" + "fmt" "github.com/hako/durafmt" ) @@ -32,12 +33,11 @@ templ DomainCards(domains []configuration.Domain) { } -templ WhoisDetail(whois configuration.WhoisCache) { - if (configuration.WhoisCache{}) == whois { -