mirror of
https://github.com/get-drexa/drive.git
synced 2026-02-02 13:21:17 +00:00
feat: improve err logging
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
|
||||
"github.com/get-drexa/drexa/internal/auth"
|
||||
"github.com/get-drexa/drexa/internal/httperr"
|
||||
"github.com/get-drexa/drexa/internal/user"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
@@ -68,7 +69,7 @@ func (h *HTTPHandler) accountMiddleware(c *fiber.Ctx) error {
|
||||
if errors.Is(err, ErrAccountNotFound) {
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
c.Locals(currentAccountKey, account)
|
||||
@@ -92,7 +93,7 @@ func (h *HTTPHandler) registerAccount(c *fiber.Ctx) error {
|
||||
|
||||
tx, err := h.db.BeginTx(c.Context(), nil)
|
||||
if err != nil {
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
@@ -109,17 +110,17 @@ func (h *HTTPHandler) registerAccount(c *fiber.Ctx) error {
|
||||
if errors.Is(err, ErrAccountAlreadyExists) {
|
||||
return c.SendStatus(fiber.StatusConflict)
|
||||
}
|
||||
return c.SendStatus(fiber.StatusBadRequest)
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
result, err := h.authService.GenerateTokenForUser(c.Context(), tx, u)
|
||||
if err != nil {
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
return c.JSON(registerAccountResponse{
|
||||
|
||||
@@ -3,6 +3,7 @@ package auth
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/get-drexa/drexa/internal/httperr"
|
||||
"github.com/get-drexa/drexa/internal/user"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/uptrace/bun"
|
||||
@@ -41,7 +42,7 @@ func (h *HTTPHandler) Login(c *fiber.Ctx) error {
|
||||
|
||||
tx, err := h.db.BeginTx(c.Context(), nil)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal server error"})
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
@@ -50,11 +51,11 @@ func (h *HTTPHandler) Login(c *fiber.Ctx) error {
|
||||
if errors.Is(err, ErrInvalidCredentials) {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Invalid credentials"})
|
||||
}
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal server error"})
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal server error"})
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
return c.JSON(loginResponse{
|
||||
|
||||
@@ -2,8 +2,10 @@ package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"github.com/get-drexa/drexa/internal/httperr"
|
||||
"github.com/get-drexa/drexa/internal/user"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/uptrace/bun"
|
||||
@@ -17,11 +19,13 @@ func NewBearerAuthMiddleware(s *Service, db *bun.DB) fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
authHeader := c.Get("Authorization")
|
||||
if authHeader == "" {
|
||||
slog.Info("no auth header")
|
||||
return c.SendStatus(fiber.StatusUnauthorized)
|
||||
}
|
||||
|
||||
parts := strings.Split(authHeader, " ")
|
||||
if len(parts) != 2 || parts[0] != "Bearer" {
|
||||
slog.Info("invalid auth header")
|
||||
return c.SendStatus(fiber.StatusUnauthorized)
|
||||
}
|
||||
|
||||
@@ -30,15 +34,17 @@ func NewBearerAuthMiddleware(s *Service, db *bun.DB) fiber.Handler {
|
||||
if err != nil {
|
||||
var e *InvalidAccessTokenError
|
||||
if errors.As(err, &e) {
|
||||
slog.Info("invalid access token")
|
||||
return c.SendStatus(fiber.StatusUnauthorized)
|
||||
}
|
||||
|
||||
var nf *user.NotFoundError
|
||||
if errors.As(err, &nf) {
|
||||
slog.Info("user not found")
|
||||
return c.SendStatus(fiber.StatusUnauthorized)
|
||||
}
|
||||
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
c.Locals(authenticatedUserKey, u)
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"log/slog"
|
||||
|
||||
"github.com/get-drexa/drexa/internal/password"
|
||||
"github.com/get-drexa/drexa/internal/user"
|
||||
@@ -94,11 +95,13 @@ func (s *Service) AuthenticateWithEmailAndPassword(ctx context.Context, db bun.I
|
||||
func (s *Service) AuthenticateWithAccessToken(ctx context.Context, db bun.IDB, token string) (*user.User, error) {
|
||||
claims, err := ParseAccessToken(token, &s.tokenConfig)
|
||||
if err != nil {
|
||||
slog.Info("failed to parse access token", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(claims.Subject)
|
||||
if err != nil {
|
||||
slog.Info("failed to parse access token subject", "error", err)
|
||||
return nil, newInvalidAccessTokenError(err)
|
||||
}
|
||||
|
||||
|
||||
@@ -8,19 +8,24 @@ import (
|
||||
"github.com/get-drexa/drexa/internal/auth"
|
||||
"github.com/get-drexa/drexa/internal/blob"
|
||||
"github.com/get-drexa/drexa/internal/database"
|
||||
"github.com/get-drexa/drexa/internal/httperr"
|
||||
"github.com/get-drexa/drexa/internal/upload"
|
||||
"github.com/get-drexa/drexa/internal/user"
|
||||
"github.com/get-drexa/drexa/internal/virtualfs"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||
"github.com/uptrace/bun/extra/bundebug"
|
||||
)
|
||||
|
||||
func NewServer(c Config) (*fiber.App, error) {
|
||||
app := fiber.New()
|
||||
db := database.NewFromPostgres(c.Database.PostgresURL)
|
||||
|
||||
app := fiber.New(fiber.Config{
|
||||
ErrorHandler: httperr.ErrorHandler,
|
||||
})
|
||||
app.Use(logger.New())
|
||||
|
||||
db := database.NewFromPostgres(c.Database.PostgresURL)
|
||||
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
|
||||
|
||||
// Initialize blob store based on config
|
||||
var blobStore blob.Store
|
||||
switch c.Storage.Backend {
|
||||
|
||||
42
apps/backend/internal/httperr/error.go
Normal file
42
apps/backend/internal/httperr/error.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package httperr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// HTTPError represents an HTTP error with a status code and underlying error.
|
||||
type HTTPError struct {
|
||||
Code int
|
||||
Message string
|
||||
Err error
|
||||
}
|
||||
|
||||
// Error implements the error interface.
|
||||
func (e *HTTPError) Error() string {
|
||||
if e.Err != nil {
|
||||
return fmt.Sprintf("HTTP %d: %s: %v", e.Code, e.Message, e.Err)
|
||||
}
|
||||
return fmt.Sprintf("HTTP %d: %s", e.Code, e.Message)
|
||||
}
|
||||
|
||||
// Unwrap returns the underlying error.
|
||||
func (e *HTTPError) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
// NewHTTPError creates a new HTTPError with the given status code, message, and underlying error.
|
||||
func NewHTTPError(code int, message string, err error) *HTTPError {
|
||||
return &HTTPError{
|
||||
Code: code,
|
||||
Message: message,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
||||
// Internal creates a new HTTPError with status 500.
|
||||
func Internal(err error) *HTTPError {
|
||||
return NewHTTPError(fiber.StatusInternalServerError, "Internal", err)
|
||||
}
|
||||
|
||||
64
apps/backend/internal/httperr/handler.go
Normal file
64
apps/backend/internal/httperr/handler.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package httperr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log/slog"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// ErrorHandler is a global error handler for Fiber that logs errors and returns appropriate responses.
|
||||
func ErrorHandler(c *fiber.Ctx, err error) error {
|
||||
// Default status code
|
||||
code := fiber.StatusInternalServerError
|
||||
message := "Internal"
|
||||
|
||||
// Check if it's our custom HTTPError
|
||||
var httpErr *HTTPError
|
||||
if errors.As(err, &httpErr) {
|
||||
code = httpErr.Code
|
||||
message = httpErr.Message
|
||||
|
||||
// Log the error with underlying error details
|
||||
if httpErr.Err != nil {
|
||||
slog.Error("HTTP error",
|
||||
"status", code,
|
||||
"message", message,
|
||||
"error", httpErr.Err.Error(),
|
||||
"path", c.Path(),
|
||||
"method", c.Method(),
|
||||
)
|
||||
} else {
|
||||
slog.Warn("HTTP error",
|
||||
"status", code,
|
||||
"message", message,
|
||||
"path", c.Path(),
|
||||
"method", c.Method(),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// Check if it's a Fiber error
|
||||
var fiberErr *fiber.Error
|
||||
if errors.As(err, &fiberErr) {
|
||||
code = fiberErr.Code
|
||||
message = fiberErr.Message
|
||||
} else {
|
||||
// Generic error - log it
|
||||
slog.Error("Unhandled error",
|
||||
"status", code,
|
||||
"error", err.Error(),
|
||||
"path", c.Path(),
|
||||
"method", c.Method(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Set Content-Type header
|
||||
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSONCharsetUTF8)
|
||||
|
||||
// Return JSON response
|
||||
return c.Status(code).JSON(fiber.Map{
|
||||
"error": message,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
|
||||
"github.com/get-drexa/drexa/internal/account"
|
||||
"github.com/get-drexa/drexa/internal/httperr"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
@@ -50,7 +51,10 @@ func (h *HTTPHandler) Create(c *fiber.Ctx) error {
|
||||
Name: req.Name,
|
||||
})
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal server error"})
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
return c.JSON(upload)
|
||||
@@ -67,7 +71,7 @@ func (h *HTTPHandler) ReceiveContent(c *fiber.Ctx) error {
|
||||
err := h.service.ReceiveUpload(c.Context(), h.db, account.ID, uploadID, c.Request().BodyStream())
|
||||
defer c.Request().CloseBodyStream()
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal server error"})
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
return c.SendStatus(fiber.StatusNoContent)
|
||||
@@ -90,7 +94,7 @@ func (h *HTTPHandler) Update(c *fiber.Ctx) error {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal server error"})
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
return c.JSON(upload)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user