From 8f27f4c116f591114bf896bc04439f4fae982867 Mon Sep 17 00:00:00 2001 From: Chris Battarbee Date: Thu, 28 Mar 2024 10:43:32 +0000 Subject: [PATCH] Chang wording of error messages --- apiserver/internal/server/current_status.go | 37 ++++++++++----------- apiserver/internal/server/incidents.go | 20 ++++++++++- apiserver/internal/server/status_page.go | 4 +-- common/api/api.go | 34 +++++++++---------- 4 files changed, 55 insertions(+), 40 deletions(-) diff --git a/apiserver/internal/server/current_status.go b/apiserver/internal/server/current_status.go index 036be38..707f031 100644 --- a/apiserver/internal/server/current_status.go +++ b/apiserver/internal/server/current_status.go @@ -37,6 +37,23 @@ func (s *Server) currentStatus(context *gin.Context) { return } + statusPageInterface, found := s.statusPageCache.Get(statusPageUrl) + if !found { + context.JSON(http.StatusNotFound, gin.H{"error": "status page not known to statusphere"}) + return + } + + statusPageInterfaceCasted, ok := statusPageInterface.(api.StatusPage) + if !ok { + context.JSON(http.StatusInternalServerError, gin.H{"error": "failed to cast status page to api.StatusPage"}) + return + } + + if !statusPageInterfaceCasted.IsIndexed { + context.JSON(http.StatusOK, CurrentStatusResponse{Status: StatusUnknown, IsIndexed: false}) + return + } + // Attempt to get the incidents from the cache incidents, found, err := s.getCurrentIncidentsFromCache(ctx, statusPageUrl) if err != nil { @@ -70,26 +87,6 @@ func (s *Server) currentStatus(context *gin.Context) { context.JSON(http.StatusOK, CurrentStatusResponse{Status: StatusDegraded, IsIndexed: true}) return } - // Is the status page indexed? - // If not, return unknown - // If it is, return up - - statusPageInterface, found := s.statusPageCache.Get(statusPageUrl) - if !found { - context.JSON(http.StatusNotFound, gin.H{"error": "status page not known to statusphere"}) - return - } - - statusPageInterfaceCasted, ok := statusPageInterface.(api.StatusPage) - if !ok { - context.JSON(http.StatusInternalServerError, gin.H{"error": "failed to cast status page to api.StatusPage"}) - return - } - - if !statusPageInterfaceCasted.IsIndexed { - context.JSON(http.StatusOK, CurrentStatusResponse{Status: StatusUnknown, IsIndexed: false}) - return - } context.JSON(http.StatusOK, CurrentStatusResponse{Status: StatusUp, IsIndexed: true}) } diff --git a/apiserver/internal/server/incidents.go b/apiserver/internal/server/incidents.go index 9300bbc..051bf18 100644 --- a/apiserver/internal/server/incidents.go +++ b/apiserver/internal/server/incidents.go @@ -12,6 +12,7 @@ import ( type IncidentsResponse struct { Incidents []api.Incident `json:"incidents"` + IsIndexed bool `json:"isIndexed"` } // incidents is a handler for the /incidents endpoint. @@ -24,6 +25,23 @@ func (s *Server) incidents(context *gin.Context) { return } + statusPage, found := s.statusPageCache.Get(statusPageUrl) + if !found { + context.JSON(http.StatusNotFound, gin.H{"error": "status page not known to statusphere"}) + return + } + + statusPageCasted, ok := statusPage.(api.StatusPage) + if !ok { + context.JSON(http.StatusInternalServerError, gin.H{"error": "failed to cast status page"}) + return + } + + if !statusPageCasted.IsIndexed { + context.JSON(http.StatusOK, IncidentsResponse{Incidents: []api.Incident{}, IsIndexed: false}) + return + } + // Attempt to get the incidents from the cache incidents, found, err := s.getIncidentsFromCache(ctx, statusPageUrl) if err != nil { @@ -44,7 +62,7 @@ func (s *Server) incidents(context *gin.Context) { return } if !found { - context.JSON(http.StatusNotFound, gin.H{"error": "status page not indexed"}) + context.JSON(http.StatusNotFound, gin.H{"error": "status page not known to statusphere"}) return } diff --git a/apiserver/internal/server/status_page.go b/apiserver/internal/server/status_page.go index bfc1cac..f3b4f8f 100644 --- a/apiserver/internal/server/status_page.go +++ b/apiserver/internal/server/status_page.go @@ -30,7 +30,7 @@ func (s *Server) statusPage(context *gin.Context) { if statusPageUrl != "" { statusPage, found := s.statusPageCache.Get(statusPageUrl) if !found { - context.JSON(http.StatusNotFound, gin.H{"error": "status page not indexed"}) + context.JSON(http.StatusNotFound, gin.H{"error": "status page not known to statusphere"}) return } context.JSON(http.StatusOK, StatusPageResponse{StatusPage: statusPage.(api.StatusPage)}) @@ -44,6 +44,6 @@ func (s *Server) statusPage(context *gin.Context) { return } } - context.JSON(http.StatusNotFound, gin.H{"error": "status page not indexed"}) + context.JSON(http.StatusNotFound, gin.H{"error": "status page not known to statusphere"}) } } diff --git a/common/api/api.go b/common/api/api.go index 4ed127c..362aba4 100644 --- a/common/api/api.go +++ b/common/api/api.go @@ -28,9 +28,9 @@ func (sla IncidentEventArray) Value() (driver.Value, error) { } type IncidentEvent struct { - Title string - Description string - Time time.Time + Title string `json:"title"` + Description string `json:"description"` + Time time.Time `json:"time"` } func NewIncidentEvent(title string, description string, time time.Time) IncidentEvent { @@ -42,15 +42,15 @@ func NewIncidentEvent(title string, description string, time time.Time) Incident } type Incident struct { - Title string - Components []string `gorm:"column:components;type:jsonb"` - Events IncidentEventArray `gorm:"column:events;type:jsonb"` - StartTime time.Time `gorm:"secondarykey"` - EndTime *time.Time `gorm:"secondarykey"` - Description *string - DeepLink string `gorm:"primarykey"` - Impact Impact `gorm:"secondarykey"` - StatusPageUrl string `gorm:"secondarykey"` + Title string `json:"title"` + Components []string `gorm:"column:components;type:jsonb" json:"components"` + Events IncidentEventArray `gorm:"column:events;type:jsonb" json:"events"` + StartTime time.Time `gorm:"secondarykey" json:"startTime"` + EndTime *time.Time `gorm:"secondarykey" json:"endTime"` + Description *string `json:"description"` + DeepLink string `gorm:"primarykey" json:"deepLink"` + Impact Impact `gorm:"secondarykey" json:"impact"` + StatusPageUrl string `gorm:"secondarykey" json:"statusPageUrl"` } func NewIncident(title string, components []string, events []IncidentEvent, startTime time.Time, endTime *time.Time, description *string, deepLink string, impact Impact, statusPageUrl string) Incident { @@ -68,13 +68,13 @@ func NewIncident(title string, components []string, events []IncidentEvent, star } type StatusPage struct { - Name string `gorm:"secondarykey"` - URL string `gorm:"primarykey"` + Name string `gorm:"secondarykey" json:"name"` + URL string `gorm:"primarykey" json:"url"` // Used to determine if we should run a scrape for this status page - LastHistoricallyScraped time.Time - LastCurrentlyScraped time.Time + LastHistoricallyScraped time.Time `json:"lastHistoricallyScraped"` + LastCurrentlyScraped time.Time `json:"lastCurrentlyScraped"` // IsIndexed is used to determine if the status page has ever been indexed in the search engine successfully - IsIndexed bool + IsIndexed bool `json:"isIndexed"` } func NewStatusPage(name string, url string) StatusPage {