refactor: Flatten directory structure
Move project files from uzdb/ subdirectory to root directory for cleaner project structure.
Changes:
- Move frontend/ to root
- Move internal/ to root
- Move build/ to root
- Move all config files (go.mod, wails.json, etc.) to root
- Remove redundant uzdb/ subdirectory nesting
Project structure is now:
├── frontend/ # React application
├── internal/ # Go backend
├── build/ # Wails build assets
├── doc/ # Design documentation
├── main.go # Entry point
└── ...
🤖 Generated with Qoder
This commit is contained in:
102
internal/utils/response.go
Normal file
102
internal/utils/response.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"uzdb/internal/config"
|
||||
"uzdb/internal/models"
|
||||
)
|
||||
|
||||
// ErrorResponse sends an error response
|
||||
func ErrorResponse(c *gin.Context, statusCode int, err error, message string) {
|
||||
logger := config.GetLogger()
|
||||
|
||||
response := models.ErrorResponse{
|
||||
Error: getErrorCode(err),
|
||||
Message: message,
|
||||
Timestamp: time.Now(),
|
||||
Path: c.Request.URL.Path,
|
||||
}
|
||||
|
||||
if message == "" {
|
||||
response.Message = err.Error()
|
||||
}
|
||||
|
||||
// Log the error
|
||||
logger.Error("error response",
|
||||
zap.Int("status_code", statusCode),
|
||||
zap.String("error", response.Error),
|
||||
zap.String("message", response.Message),
|
||||
zap.String("path", response.Path),
|
||||
)
|
||||
|
||||
c.JSON(statusCode, response)
|
||||
}
|
||||
|
||||
// SuccessResponse sends a success response
|
||||
func SuccessResponse(c *gin.Context, data interface{}) {
|
||||
c.JSON(http.StatusOK, models.NewAPIResponse(data))
|
||||
}
|
||||
|
||||
// CreatedResponse sends a created response
|
||||
func CreatedResponse(c *gin.Context, data interface{}) {
|
||||
c.JSON(http.StatusCreated, models.NewAPIResponse(data))
|
||||
}
|
||||
|
||||
// getErrorCode maps errors to error codes
|
||||
func getErrorCode(err error) string {
|
||||
if err == nil {
|
||||
return string(models.CodeSuccess)
|
||||
}
|
||||
|
||||
switch {
|
||||
case errors.Is(err, models.ErrNotFound):
|
||||
return string(models.CodeNotFound)
|
||||
case errors.Is(err, models.ErrValidationFailed):
|
||||
return string(models.CodeValidation)
|
||||
case errors.Is(err, models.ErrUnauthorized):
|
||||
return string(models.CodeUnauthorized)
|
||||
case errors.Is(err, models.ErrForbidden):
|
||||
return string(models.CodeForbidden)
|
||||
case errors.Is(err, models.ErrConnectionFailed):
|
||||
return string(models.CodeConnection)
|
||||
case errors.Is(err, models.ErrQueryFailed):
|
||||
return string(models.CodeQuery)
|
||||
case errors.Is(err, models.ErrEncryptionFailed):
|
||||
return string(models.CodeEncryption)
|
||||
default:
|
||||
return string(models.CodeInternal)
|
||||
}
|
||||
}
|
||||
|
||||
// WrapError wraps an error with context
|
||||
func WrapError(err error, message string) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return &wrappedError{
|
||||
err: err,
|
||||
message: message,
|
||||
}
|
||||
}
|
||||
|
||||
type wrappedError struct {
|
||||
err error
|
||||
message string
|
||||
}
|
||||
|
||||
func (w *wrappedError) Error() string {
|
||||
if w.message != "" {
|
||||
return w.message + ": " + w.err.Error()
|
||||
}
|
||||
return w.err.Error()
|
||||
}
|
||||
|
||||
func (w *wrappedError) Unwrap() error {
|
||||
return w.err
|
||||
}
|
||||
Reference in New Issue
Block a user