chore: custom error handler with automatic logging

This commit is contained in:
2025-06-10 17:26:20 +01:00
parent fae46f4bf7
commit 3234e5f4da
9 changed files with 142 additions and 140 deletions
+27 -29
View File
@@ -1,8 +1,7 @@
package api
import (
"fmt"
"log"
"errors"
"net/http"
"github.com/google/uuid"
@@ -15,20 +14,19 @@ func (api *API) CreateClientHandler(context echo.Context) error {
userId, ok := context.Get("id").(string)
if !ok {
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}}
}
id, err := uuid.Parse(userId)
if err != nil {
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}}
}
var body types.CreateClientRequestBody
if err := context.Bind(&body); err != nil {
log.Println(fmt.Sprintf("Error creating Client: %v.", err))
return context.String(http.StatusBadRequest, "Invalid JSON!")
return types.Error{Code: http.StatusBadRequest, Messages: []string{"Invalid JSON!"}}
}
if err := context.Validate(body); err != nil {
@@ -37,7 +35,6 @@ func (api *API) CreateClientHandler(context echo.Context) error {
client := api.db.CreateClient(id, body)
log.Println(fmt.Sprintf("Client created with ID %s.", client.ID))
return context.JSON(http.StatusOK, client)
}
@@ -45,22 +42,21 @@ func (api *API) GetAllClientsHandler(context echo.Context) error {
userId, ok := context.Get("id").(string)
if !ok {
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}}
}
id, err := uuid.Parse(userId)
if err != nil {
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}}
}
clients, err := api.db.GetClients(id)
if err != nil {
return context.String(http.StatusInternalServerError, "Unexpected error getting Clients!")
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting Clients!"}}
}
log.Println("Got all Clients.")
return context.JSON(http.StatusOK, clients)
}
@@ -68,28 +64,27 @@ func (api *API) GetClientHandler(context echo.Context) error {
userIdString, ok := context.Get("id").(string)
if !ok {
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}}
}
userId, err := uuid.Parse(userIdString)
if err != nil {
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}}
}
id, err := uuid.Parse(context.Param("id"))
if err != nil {
return context.String(http.StatusBadRequest, "ID is required to get a Client!")
return types.Error{Code: http.StatusBadRequest, Messages: []string{"ID is required to get a Client!"}}
}
var client types.Client
if err := api.db.GetClientById(userId, id, &client); err != nil {
return context.String(http.StatusNotFound, "Client not found!")
return types.Error{Code: http.StatusNotFound, Messages: []string{"Client not found!"}}
}
log.Println(fmt.Sprintf("Got User with ID %s.", client.ID))
return context.JSON(http.StatusOK, client)
}
@@ -97,26 +92,31 @@ func (api *API) UpdateClientHandler(context echo.Context) error {
userIdString, ok := context.Get("id").(string)
if !ok {
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}}
}
userId, err := uuid.Parse(userIdString)
if err != nil {
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}}
}
id, err := uuid.Parse(context.Param("id"))
if err != nil {
return context.String(http.StatusBadRequest, "ID is required to update a Client!")
return types.Error{Code: http.StatusBadRequest, Messages: []string{"ID is required to update a Client!"}}
}
var body types.UpdateClientRequestBody
if err := context.Bind(&body); err != nil {
log.Println(fmt.Sprintf("Error updating Client: %v.", err))
return context.String(http.StatusBadRequest, "Invalid JSON!")
return types.Error{Code: http.StatusBadRequest, Messages: []string{"Invalid JSON!"}}
}
empty := body == types.UpdateClientRequestBody{}
if empty {
return types.Error{Code: http.StatusBadRequest, Messages: []string{"You must update at least one field!"}}
}
if err := context.Validate(body); err != nil {
@@ -126,10 +126,9 @@ func (api *API) UpdateClientHandler(context echo.Context) error {
var client types.Client
if err := api.db.UpdateClient(userId, id, body, &client); err != nil {
return context.String(http.StatusNotFound, "Client not found!")
return types.Error{Code: http.StatusNotFound, Messages: []string{"Client not found!"}}
}
log.Println(fmt.Sprintf("Updated Client with ID %s.", client.ID))
return context.JSON(http.StatusOK, client)
}
@@ -137,25 +136,24 @@ func (api *API) DeleteClientHandler(context echo.Context) error {
userIdString, ok := context.Get("id").(string)
if !ok {
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}}
}
userId, err := uuid.Parse(userIdString)
if err != nil {
return context.String(http.StatusInternalServerError, "Unexpected error getting User!")
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}}
}
id, err := uuid.Parse(context.Param("id"))
if err != nil {
return context.String(http.StatusBadRequest, "ID is required to delete a Client!")
return types.Error{Code: http.StatusBadRequest, Messages: []string{"ID is required to delete a Client!"}}
}
if err := api.db.DeleteClient(userId, id); err != nil {
return context.String(http.StatusNotFound, "Client not found!")
return types.Error{Code: http.StatusNotFound, Messages: []string{"Client not found!"}}
}
log.Println(fmt.Sprintf("Deleted Client with ID %s.", id))
return context.String(http.StatusOK, "Client deleted successfully!")
return context.JSON(http.StatusOK, map[string]string{"message": "Client deleted successfully!"})
}