Skip to content

Commit

Permalink
Print API errors in the logger
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinguidee committed Sep 21, 2023
1 parent 3490450 commit 67cab8f
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 49 deletions.
13 changes: 13 additions & 0 deletions pkg/ginutils/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ginutils

import "github.com/gin-gonic/gin"

func ErrorHandler() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()

if len(c.Errors) > 0 {
c.JSON(-1, c.Errors[0].Err)
}
}
}
7 changes: 6 additions & 1 deletion pkg/ginutils/logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ginutils

import (
"strings"

"github.com/gin-gonic/gin"
"github.com/vertex-center/vertex/pkg/log"
"github.com/vertex-center/vlog"
Expand All @@ -9,6 +11,9 @@ import (
func Logger(router string) gin.HandlerFunc {
return gin.LoggerWithFormatter(func(params gin.LogFormatterParams) string {
if params.ErrorMessage != "" {
errorMessage := strings.TrimSuffix(params.ErrorMessage, "\n")
errorMessage = strings.ReplaceAll(errorMessage, "Error #01: ", "")

log.Request("request",
vlog.String("router", router),
vlog.String("method", params.Method),
Expand All @@ -17,7 +22,7 @@ func Logger(router string) gin.HandlerFunc {
vlog.String("latency", params.Latency.String()),
vlog.String("ip", params.ClientIP),
vlog.Int("size", params.BodySize),
vlog.String("error", params.ErrorMessage),
vlog.String("error", errorMessage),
)
} else {
log.Request("request",
Expand Down
11 changes: 5 additions & 6 deletions router/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func handleGetDependencies(c *gin.Context) {
var err error
dependencies, err = dependenciesService.CheckForUpdates()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_check_for_updates",
Message: fmt.Sprintf("failed to check for updates: %v", err),
})
Expand Down Expand Up @@ -53,7 +53,7 @@ func handleUpdateDependencies(c *gin.Context) {
var body executeUpdatesBody
err := c.BindJSON(&body)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, types.APIError{
_ = c.AbortWithError(http.StatusBadRequest, types.APIError{
Code: "failed_to_parse_body",
Message: fmt.Sprintf("failed to parse request body: %v", err),
})
Expand All @@ -69,7 +69,7 @@ func handleUpdateDependencies(c *gin.Context) {

err = dependenciesService.InstallUpdates(updates)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_install_updates",
Message: fmt.Sprintf("failed to install updates: %v", err),
})
Expand All @@ -78,7 +78,7 @@ func handleUpdateDependencies(c *gin.Context) {

err = serviceService.Reload()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_reload_services",
Message: fmt.Sprintf("failed to reload services: %v", err),
})
Expand All @@ -87,8 +87,7 @@ func handleUpdateDependencies(c *gin.Context) {

err = packageService.Reload()
if err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_reload_packages",
Message: fmt.Sprintf("failed to reload packages: %v", err),
})
Expand Down
4 changes: 3 additions & 1 deletion router/hardware.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package router

import "github.com/gin-gonic/gin"
import (
"github.com/gin-gonic/gin"
)

func addHardwareRoutes(r *gin.RouterGroup) {
r.GET("", handleGetHardware)
Expand Down
58 changes: 29 additions & 29 deletions router/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func addInstanceRoutes(r *gin.RouterGroup) {
func getParamInstanceUUID(c *gin.Context) *uuid.UUID {
p := c.Param("instance_uuid")
if p == "" {
c.AbortWithStatusJSON(http.StatusBadRequest, types.APIError{
_ = c.AbortWithError(http.StatusBadRequest, types.APIError{
Code: "missing_instance_uuid",
Message: "'instance_uuid' was missing in the URL",
})
Expand All @@ -45,7 +45,7 @@ func getParamInstanceUUID(c *gin.Context) *uuid.UUID {

uid, err := uuid.Parse(p)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, types.APIError{
_ = c.AbortWithError(http.StatusBadRequest, types.APIError{
Code: "invalid_instance_uuid",
Message: fmt.Sprintf("'%s' is not a valid UUID", p),
})
Expand All @@ -69,13 +69,13 @@ func getInstance(c *gin.Context) *types.Instance {

instance, err := instanceService.Get(*instanceUUID)
if err != nil && errors.Is(err, types.ErrInstanceNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, types.APIError{
_ = c.AbortWithError(http.StatusNotFound, types.APIError{
Code: "instance_not_found",
Message: fmt.Sprintf("instance %s not found", instanceUUID),
})
return nil
} else if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_retrieve_instance",
Message: fmt.Sprintf("failed to retrieve instance %s: %v", instanceUUID, err),
})
Expand Down Expand Up @@ -112,19 +112,19 @@ func handleDeleteInstance(c *gin.Context) {

err := instanceService.Delete(*uid)
if err != nil && errors.Is(err, types.ErrInstanceNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, types.APIError{
_ = c.AbortWithError(http.StatusNotFound, types.APIError{
Code: "instance_not_found",
Message: fmt.Sprintf("instance %s not found", uid),
})
return
} else if err != nil && errors.Is(err, types.ErrInstanceStillRunning) {
c.AbortWithStatusJSON(http.StatusConflict, types.APIError{
_ = c.AbortWithError(http.StatusConflict, types.APIError{
Code: "instance_still_running",
Message: fmt.Sprintf("instance %s is still running", uid),
})
return
} else if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_delete_instance",
Message: fmt.Sprintf("failed to delete instance %s, %v", uid, err),
})
Expand Down Expand Up @@ -157,7 +157,7 @@ func handlePatchInstance(c *gin.Context) {
var body handlePatchInstanceBody
err := c.BindJSON(&body)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, types.APIError{
_ = c.AbortWithError(http.StatusBadRequest, types.APIError{
Code: "failed_to_parse_body",
Message: fmt.Sprintf("failed to parse request body: %v", err),
})
Expand All @@ -167,13 +167,13 @@ func handlePatchInstance(c *gin.Context) {
if body.LaunchOnStartup != nil {
err = instanceService.SetLaunchOnStartup(*uid, *body.LaunchOnStartup)
if err != nil && errors.Is(err, types.ErrInstanceNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, types.APIError{
_ = c.AbortWithError(http.StatusNotFound, types.APIError{
Code: "instance_not_found",
Message: fmt.Sprintf("instance %s not found", uid),
})
return
} else if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_set_launch_on_startup",
Message: fmt.Sprintf("failed to set launch on startup: %v", err),
})
Expand All @@ -184,13 +184,13 @@ func handlePatchInstance(c *gin.Context) {
if body.DisplayName != nil {
err = instanceService.SetDisplayName(*uid, *body.DisplayName)
if err != nil && errors.Is(err, types.ErrInstanceNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, types.APIError{
_ = c.AbortWithError(http.StatusNotFound, types.APIError{
Code: "instance_not_found",
Message: fmt.Sprintf("instance %s not found", uid),
})
return
} else if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_set_display_name",
Message: fmt.Sprintf("failed to set display name: %v", err),
})
Expand All @@ -201,13 +201,13 @@ func handlePatchInstance(c *gin.Context) {
if body.Databases != nil {
err = instanceService.SetDatabases(*uid, body.Databases)
if err != nil && errors.Is(err, types.ErrInstanceNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, types.APIError{
_ = c.AbortWithError(http.StatusNotFound, types.APIError{
Code: "instance_not_found",
Message: fmt.Sprintf("instance %s not found", uid),
})
return
} else if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_set_databases",
Message: fmt.Sprintf("failed to set databases: %v", err),
})
Expand All @@ -233,19 +233,19 @@ func handleStartInstance(c *gin.Context) {

err := instanceService.Start(*uid)
if err != nil && errors.Is(err, types.ErrInstanceNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, types.APIError{
_ = c.AbortWithError(http.StatusNotFound, types.APIError{
Code: "instance_not_found",
Message: fmt.Sprintf("instance %s not found", uid),
})
return
} else if err != nil && errors.Is(err, services.ErrInstanceAlreadyRunning) {
c.AbortWithStatusJSON(http.StatusConflict, types.APIError{
_ = c.AbortWithError(http.StatusConflict, types.APIError{
Code: "instance_already_running",
Message: fmt.Sprintf("instance %s is already running", uid),
})
return
} else if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_start_instance",
Message: fmt.Sprintf("failed to start instance %s: %v", uid, err),
})
Expand All @@ -270,19 +270,19 @@ func handleStopInstance(c *gin.Context) {

err := instanceService.Stop(*uid)
if err != nil && errors.Is(err, types.ErrInstanceNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, types.APIError{
_ = c.AbortWithError(http.StatusNotFound, types.APIError{
Code: "instance_not_found",
Message: fmt.Sprintf("instance %s not found", uid),
})
return
} else if err != nil && errors.Is(err, services.ErrInstanceNotRunning) {
c.AbortWithStatusJSON(http.StatusConflict, types.APIError{
_ = c.AbortWithError(http.StatusConflict, types.APIError{
Code: "instance_not_running",
Message: fmt.Sprintf("instance %s is not running", uid),
})
return
} else if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_stop_instance",
Message: fmt.Sprintf("failed to stop instance %s: %v", uid, err),
})
Expand All @@ -304,7 +304,7 @@ func handlePatchEnvironment(c *gin.Context) {
var environment map[string]string
err := c.BindJSON(&environment)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, types.APIError{
_ = c.AbortWithError(http.StatusBadRequest, types.APIError{
Code: "failed_to_parse_body",
Message: fmt.Sprintf("failed to parse request body: %v", err),
})
Expand All @@ -318,13 +318,13 @@ func handlePatchEnvironment(c *gin.Context) {

err = instanceService.WriteEnv(i.UUID, environment)
if err != nil && errors.Is(err, types.ErrInstanceNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, types.APIError{
_ = c.AbortWithError(http.StatusNotFound, types.APIError{
Code: "instance_not_found",
Message: fmt.Sprintf("instance %s not found", i.UUID),
})
return
} else if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_save_environment",
Message: fmt.Sprintf("failed to save environment: %v", err),
})
Expand All @@ -333,7 +333,7 @@ func handlePatchEnvironment(c *gin.Context) {

err = instanceService.RecreateContainer(i)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_recreate_container",
Message: fmt.Sprintf("failed to recreate container: %v", err),
})
Expand Down Expand Up @@ -433,7 +433,7 @@ func handleGetPackages(c *gin.Context) {
}

if i.Service.Methods.Script == nil {
c.AbortWithStatusJSON(http.StatusNotFound, types.APIError{
_ = c.AbortWithError(http.StatusNotFound, types.APIError{
Code: "instance_doesnt_use_scripts",
Message: "this service doesn't use scripts, so it doesn't have dependencies",
})
Expand Down Expand Up @@ -469,7 +469,7 @@ func handleGetDocker(c *gin.Context) {

info, err := instanceService.GetDockerContainerInfo(*uid)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_get_docker_container_info",
Message: fmt.Sprintf("failed to get docker container info: %v", err),
})
Expand All @@ -492,7 +492,7 @@ func handleRecreateDockerContainer(c *gin.Context) {

err := instanceService.RecreateContainer(i)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_recreate_container",
Message: fmt.Sprintf("failed to recreate container: %v", err),
})
Expand All @@ -515,7 +515,7 @@ func handleGetLogs(c *gin.Context) {

logs, err := instanceService.GetLatestLogs(*uid)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_get_logs",
Message: fmt.Sprintf("failed to get logs: %v", err),
})
Expand All @@ -536,7 +536,7 @@ func handleUpdateService(c *gin.Context) {

err := instanceService.UpdateService(*uid)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_update_service",
Message: fmt.Sprintf("failed to update service: %v", err),
})
Expand Down
2 changes: 1 addition & 1 deletion router/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func handleSearchInstances(c *gin.Context) {
func handleCheckForUpdates(c *gin.Context) {
instances, err := instanceService.CheckForUpdates()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "check_for_updates_failed",
Message: err.Error(),
})
Expand Down
2 changes: 1 addition & 1 deletion router/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func handleInstallPackages(c *gin.Context) {
var body InstallPackagesBody
err := c.BindJSON(&body)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, types.APIError{
_ = c.AbortWithError(http.StatusBadRequest, types.APIError{
Code: "failed_to_parse_body",
Message: fmt.Sprintf("failed to parse request body: %v", err),
})
Expand Down
10 changes: 5 additions & 5 deletions router/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func handleAddRedirect(c *gin.Context) {
var body handleAddRedirectBody
err := c.BindJSON(&body)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, types.APIError{
_ = c.AbortWithError(http.StatusBadRequest, types.APIError{
Code: "failed_to_parse_body",
Message: fmt.Sprintf("failed to parse request body: %v", err),
})
Expand All @@ -48,7 +48,7 @@ func handleAddRedirect(c *gin.Context) {

err = proxyService.AddRedirect(redirect)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_add_redirect",
Message: fmt.Sprintf("failed to add redirect: %v", err),
})
Expand All @@ -66,7 +66,7 @@ func handleAddRedirect(c *gin.Context) {
func handleRemoveRedirect(c *gin.Context) {
idString := c.Param("id")
if idString == "" {
c.AbortWithStatusJSON(http.StatusBadRequest, types.APIError{
_ = c.AbortWithError(http.StatusBadRequest, types.APIError{
Code: "missing_redirect_uuid",
Message: "missing redirect uuid",
})
Expand All @@ -75,7 +75,7 @@ func handleRemoveRedirect(c *gin.Context) {

id, err := uuid.Parse(idString)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, types.APIError{
_ = c.AbortWithError(http.StatusBadRequest, types.APIError{
Code: "invalid_redirect_uuid",
Message: "invalid redirect uuid",
})
Expand All @@ -84,7 +84,7 @@ func handleRemoveRedirect(c *gin.Context) {

err = proxyService.RemoveRedirect(id)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.APIError{
_ = c.AbortWithError(http.StatusInternalServerError, types.APIError{
Code: "failed_to_remove_redirect",
Message: fmt.Sprintf("failed to remove redirect: %v", err),
})
Expand Down
Loading

0 comments on commit 67cab8f

Please sign in to comment.