mirror of
https://github.com/hazemKrimi/crimson-vault.git
synced 2026-05-01 18:20:27 +00:00
chore: custom error handler with automatic logging
This commit is contained in:
+45
-58
@@ -1,9 +1,9 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -21,8 +21,7 @@ func (api *API) CreateUserHandler(context echo.Context) error {
|
||||
var body types.CreateUserRequestBody
|
||||
|
||||
if err := context.Bind(&body); err != nil {
|
||||
log.Println(fmt.Sprintf("Error creating User: %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 {
|
||||
@@ -32,23 +31,19 @@ func (api *API) CreateUserHandler(context echo.Context) error {
|
||||
user, err := api.db.CreateUser(body)
|
||||
|
||||
if err != nil {
|
||||
log.Println(fmt.Sprintf("Error creating User: %v.", err))
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error creating User!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error creating User!"}}
|
||||
}
|
||||
|
||||
sess, err := session.Get("session", context)
|
||||
|
||||
if err != nil {
|
||||
log.Println(fmt.Sprintf("Error creating User session: %v.", err))
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error creating User session!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error creating User session!"}}
|
||||
}
|
||||
|
||||
if err := lib.CreateSession(sess, context, &user); err != nil {
|
||||
log.Println(fmt.Sprintf("Error creating User session: %v.", err))
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error creating User session!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error creating User session!"}}
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("User created with ID %s.", user.ID))
|
||||
return context.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
@@ -56,10 +51,9 @@ func (api *API) GetAllUsersHandler(context echo.Context) error {
|
||||
users, err := api.db.GetUsers()
|
||||
|
||||
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!"}}
|
||||
}
|
||||
|
||||
log.Println("Got all Users.")
|
||||
return context.JSON(http.StatusOK, users)
|
||||
}
|
||||
|
||||
@@ -67,22 +61,21 @@ func (api *API) GetUserHandler(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 user types.User
|
||||
|
||||
if err := api.db.GetUserById(id, &user); err != nil {
|
||||
return context.String(http.StatusNotFound, "User not found!")
|
||||
return types.Error{Code: http.StatusNotFound, Messages: []string{"User not found!"}}
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("Got User with ID %s.", user.ID))
|
||||
return context.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
@@ -90,20 +83,25 @@ func (api *API) UpdateUserHandler(context echo.Context) error {
|
||||
userId, ok := context.Get("id").(string)
|
||||
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error updating User!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error updating User!"}}
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(userId)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error updating User!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error updating User!"}}
|
||||
}
|
||||
|
||||
var body types.UpdateUserRequestBody
|
||||
|
||||
if err := context.Bind(&body); err != nil {
|
||||
log.Println(fmt.Sprintf("Error updating User: %v.", err))
|
||||
return context.String(http.StatusBadRequest, "Invalid JSON!")
|
||||
return types.Error{Code: http.StatusBadRequest, Messages: []string{"Invalid JSON!"}}
|
||||
}
|
||||
|
||||
empty := body == types.UpdateUserRequestBody{}
|
||||
|
||||
if empty {
|
||||
return types.Error{Code: http.StatusBadRequest, Messages: []string{"You must update at least one field!"}}
|
||||
}
|
||||
|
||||
if err := context.Validate(body); err != nil {
|
||||
@@ -113,10 +111,9 @@ func (api *API) UpdateUserHandler(context echo.Context) error {
|
||||
var user types.User
|
||||
|
||||
if err := api.db.UpdateUser(id, body, &user); err != nil {
|
||||
return context.String(http.StatusNotFound, "User not found!")
|
||||
return types.Error{Code: http.StatusNotFound, Messages: []string{"User not found!"}}
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("Updated user with ID %s.", user.ID))
|
||||
return context.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
@@ -124,20 +121,19 @@ func (api *API) UpdateUserSecurityCredentialsHandler(context echo.Context) error
|
||||
userId, ok := context.Get("id").(string)
|
||||
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error updating User security credentials!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error updating User security credentials!"}}
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(userId)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error updating User security credentials!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error updating User security credentials!"}}
|
||||
}
|
||||
|
||||
var body types.UpdateUserSecurityCredentialsBody
|
||||
|
||||
if err := context.Bind(&body); err != nil {
|
||||
log.Println(fmt.Sprintf("Error creating security details for User: %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 {
|
||||
@@ -147,10 +143,9 @@ func (api *API) UpdateUserSecurityCredentialsHandler(context echo.Context) error
|
||||
var user types.User
|
||||
|
||||
if err := api.db.UpdateUserSecurityCredentials(id, body, &user); err != nil {
|
||||
return context.String(http.StatusNotFound, "User not found!")
|
||||
return types.Error{Code: http.StatusBadRequest, Messages: []string{"Invalid JSON!"}}
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("Updated security details of user with ID %s.", user.ID))
|
||||
return context.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
@@ -158,29 +153,29 @@ func (api *API) UpdateUserLogoHandler(context echo.Context) error {
|
||||
userId, ok := context.Get("id").(string)
|
||||
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error updating User logo!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error updating User logo!"}}
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(userId)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error updating User logo!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error updating User logo!"}}
|
||||
}
|
||||
|
||||
var user types.User
|
||||
|
||||
if err := api.db.GetUserById(id, &user); err != nil {
|
||||
return context.String(http.StatusNotFound, "User not found!")
|
||||
return types.Error{Code: http.StatusNotFound, Messages: []string{"User not found!"}}
|
||||
}
|
||||
|
||||
if user.Username == "" {
|
||||
return context.String(http.StatusBadRequest, "You have to add a username first for this User!")
|
||||
return types.Error{Code: http.StatusBadRequest, Messages: []string{"You have to add a username first for this User!"}}
|
||||
}
|
||||
|
||||
file, err := context.FormFile("logo")
|
||||
|
||||
if err != nil {
|
||||
return types.Error{Code: http.StatusBadRequest, Messages: []string{"No image has been uploaded!"}}
|
||||
return types.Error{Code: http.StatusUnsupportedMediaType, Messages: []string{"Image must be uploaded in form data!"}}
|
||||
}
|
||||
|
||||
ext := strings.ToLower(filepath.Ext(file.Filename))
|
||||
@@ -194,14 +189,13 @@ func (api *API) UpdateUserLogoHandler(context echo.Context) error {
|
||||
}
|
||||
|
||||
if !allowedExtensions[ext] {
|
||||
return context.String(http.StatusBadRequest, "Invalid file type, only image files are allowed!")
|
||||
return types.Error{Code: http.StatusUnsupportedMediaType, Messages: []string{"Invalid file type, only image files are allowed!"}}
|
||||
}
|
||||
|
||||
src, err := file.Open()
|
||||
|
||||
if err != nil {
|
||||
log.Println(fmt.Sprintf("Error updating logo for User: %v.", err))
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error while updating logo for User!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error updating logo User logo!"}}
|
||||
}
|
||||
|
||||
defer src.Close()
|
||||
@@ -209,35 +203,31 @@ func (api *API) UpdateUserLogoHandler(context echo.Context) error {
|
||||
data, err := io.ReadAll(src)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error while updating logo for User!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error updating logo User logo!"}}
|
||||
}
|
||||
|
||||
filetype := http.DetectContentType(data)
|
||||
|
||||
if !strings.HasPrefix(filetype, "image/") {
|
||||
return context.String(http.StatusBadRequest, "Uploaded file is not a valid image!")
|
||||
return types.Error{Code: http.StatusBadRequest, Messages: []string{"Uploaded file is not a valid image!"}}
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Join(api.ConfigDirectory, user.Username), os.ModePerm); err != nil {
|
||||
log.Println(fmt.Sprintf("Error updating logo for User: %v.", err))
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error while updating logo for User!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error updating User logo!"}}
|
||||
}
|
||||
|
||||
path, err := filepath.Abs(filepath.Join(api.ConfigDirectory, user.Username, fmt.Sprintf("logo%s", ext)))
|
||||
|
||||
if err != nil {
|
||||
log.Println(fmt.Sprintf("Error updating logo for User: %v.", err))
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error while updating logo for User!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error updating User logo!"}}
|
||||
}
|
||||
|
||||
if err := os.WriteFile(path, data, 0644); err != nil {
|
||||
log.Println(fmt.Sprintf("Error updating logo for User: %v.", err))
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error while updating logo for User!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error updating User logo!"}}
|
||||
}
|
||||
|
||||
if err := api.db.UpdateUserLogo(path, &user); err != nil {
|
||||
log.Println(fmt.Sprintf("Error updating logo for User: %v.", err))
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error while updating logo for User!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error updating User logo!"}}
|
||||
}
|
||||
|
||||
return context.JSON(http.StatusOK, user)
|
||||
@@ -247,49 +237,46 @@ func (api *API) DeleteUserHandler(context echo.Context) error {
|
||||
userId, ok := context.Get("id").(string)
|
||||
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error deleting User!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error updating User logo!"}}
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(userId)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error deleting User!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error deleting User!"}}
|
||||
}
|
||||
|
||||
if err := api.db.DeleteUser(id); err != nil {
|
||||
return context.String(http.StatusNotFound, "User not found!")
|
||||
return types.Error{Code: http.StatusNotFound, Messages: []string{"User not found!"}}
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("Deleted User with ID %d.", id))
|
||||
return context.String(http.StatusOK, "User deleted successfully!")
|
||||
return context.JSON(http.StatusOK, map[string]string{"message":"User deleted successfully!"})
|
||||
}
|
||||
|
||||
func (api *API) DeleteUserLogoHandler(context echo.Context) error {
|
||||
userId, ok := context.Get("id").(string)
|
||||
|
||||
if !ok {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error deleting User logo!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error deleting User logo!"}}
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(userId)
|
||||
|
||||
if err != nil {
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error deleting User logo!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error deleting User logo!"}}
|
||||
}
|
||||
|
||||
var user types.User
|
||||
|
||||
if err := api.db.GetUserById(id, &user); err != nil {
|
||||
return context.String(http.StatusNotFound, "User not found!")
|
||||
return types.Error{Code: http.StatusNotFound, Messages: []string{"User not found!"}}
|
||||
}
|
||||
|
||||
os.Remove(user.Logo)
|
||||
|
||||
if err := api.db.DeleteUserLogo(&user); err != nil {
|
||||
log.Println(fmt.Sprintf("Error deleting logo of User: %v.", err))
|
||||
return context.String(http.StatusInternalServerError, "Unexpected error deleting logo of User!")
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error deleting User logo!"}}
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("Deleted logo of User with ID %s.", user.ID))
|
||||
return context.String(http.StatusOK, "User logo deleted successfully!")
|
||||
return context.JSON(http.StatusOK, map[string]string{"message":"User logo deleted successfully!"})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user