From efd5b043d87d43e212f477ee622e72625e17eba0 Mon Sep 17 00:00:00 2001 From: david may <1301201+wass3r@users.noreply.github.com> Date: Mon, 26 Aug 2024 16:46:20 +0000 Subject: [PATCH] fix(api): return 404 when log doesn't exist (#1167) --- api/log/get_service.go | 12 ++++++++++-- api/log/get_step.go | 12 ++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/api/log/get_service.go b/api/log/get_service.go index de223da08..a82f268ec 100644 --- a/api/log/get_service.go +++ b/api/log/get_service.go @@ -4,11 +4,13 @@ package log import ( + "errors" "fmt" "net/http" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" + "gorm.io/gorm" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" @@ -85,9 +87,15 @@ func GetServiceLog(c *gin.Context) { // send API call to capture the service logs sl, err := database.FromContext(c).GetLogForService(ctx, s) if err != nil { - retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err) + var status int + if errors.Is(err, gorm.ErrRecordNotFound) { + status = http.StatusNotFound + } else { + status = http.StatusInternalServerError + } - util.HandleError(c, http.StatusInternalServerError, retErr) + retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err) + util.HandleError(c, status, retErr) return } diff --git a/api/log/get_step.go b/api/log/get_step.go index 1bf6c4191..2a8f0f5e0 100644 --- a/api/log/get_step.go +++ b/api/log/get_step.go @@ -4,11 +4,13 @@ package log import ( + "errors" "fmt" "net/http" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" + "gorm.io/gorm" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" @@ -86,9 +88,15 @@ func GetStepLog(c *gin.Context) { // send API call to capture the step logs sl, err := database.FromContext(c).GetLogForStep(ctx, s) if err != nil { - retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err) + var status int + if errors.Is(err, gorm.ErrRecordNotFound) { + status = http.StatusNotFound + } else { + status = http.StatusInternalServerError + } - util.HandleError(c, http.StatusInternalServerError, retErr) + retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err) + util.HandleError(c, status, retErr) return }