style: apply gofmt across Go sources
Repo-wide gofmt sweep: const-block alignment, doc-comment spacing, statement-after-brace fix in media.go, trailing blank lines. No semantic changes — whitespace-insensitive diff is empty for every file in this commit.
This commit is contained in:
1 parent
dd2ab4ea96
commit
8733e83f1f
29 files changed
+91
-1725
No files matched your search
@@ -1,94 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
)
|
||||
|
||||
// ErrorResponse represents a standardized error response
|
||||
type ErrorResponse struct {
|
||||
Error string `json:"error"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Code string `json:"code,omitempty"`
|
||||
}
|
||||
|
||||
// HTTPError represents an HTTP error with additional context
|
||||
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("%s: %v", e.Message, e.Err)
|
||||
}
|
||||
return e.Message
|
||||
}
|
||||
|
||||
// Common error types
|
||||
var (
|
||||
ErrBadRequest = &HTTPError{Code: http.StatusBadRequest, Message: "Bad request"}
|
||||
ErrUnauthorized = &HTTPError{Code: http.StatusUnauthorized, Message: "Unauthorized"}
|
||||
ErrForbidden = &HTTPError{Code: http.StatusForbidden, Message: "Forbidden"}
|
||||
ErrNotFound = &HTTPError{Code: http.StatusNotFound, Message: "Resource not found"}
|
||||
ErrConflict = &HTTPError{Code: http.StatusConflict, Message: "Resource conflict"}
|
||||
ErrTooManyRequest = &HTTPError{Code: http.StatusTooManyRequests, Message: "Too many requests"}
|
||||
ErrInternal = &HTTPError{Code: http.StatusInternalServerError, Message: "Internal server error"}
|
||||
)
|
||||
|
||||
// NewHTTPError creates a new HTTP error
|
||||
func NewHTTPError(code int, message string, err error) *HTTPError {
|
||||
return &HTTPError{
|
||||
Code: code,
|
||||
Message: message,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
||||
// RespondWithError sends a standardized error response
|
||||
func RespondWithError(c *echo.Context, code int, message string, err error) error {
|
||||
response := ErrorResponse{
|
||||
Error: message,
|
||||
Message: "",
|
||||
Code: "",
|
||||
}
|
||||
|
||||
// Include original error message in development mode if available
|
||||
if err != nil {
|
||||
response.Message = err.Error()
|
||||
}
|
||||
|
||||
return c.JSON(code, response)
|
||||
}
|
||||
|
||||
// RespondWithHTTPError sends an HTTPError as JSON
|
||||
func RespondWithHTTPError(c *echo.Context, httpErr *HTTPError) error {
|
||||
response := ErrorResponse{
|
||||
Error: httpErr.Message,
|
||||
}
|
||||
|
||||
if httpErr.Err != nil {
|
||||
response.Message = httpErr.Err.Error()
|
||||
}
|
||||
|
||||
return c.JSON(httpErr.Code, response)
|
||||
}
|
||||
|
||||
// WrapHandler wraps an echo.HandlerFunc to return standardized errors
|
||||
func WrapHandler(fn func(*echo.Context) error) echo.HandlerFunc {
|
||||
return func(c *echo.Context) error {
|
||||
err := fn(c)
|
||||
if err != nil {
|
||||
if httpErr, ok := errors.AsType[*HTTPError](err); ok {
|
||||
return RespondWithHTTPError(c, httpErr)
|
||||
}
|
||||
return RespondWithError(c, http.StatusInternalServerError, "Internal server error", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
File renamed without changes.
@@ -1,53 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// TransactionManager handles database transactions
|
||||
type TransactionManager struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
// NewTransactionManager creates a new transaction manager
|
||||
func NewTransactionManager(pool *pgxpool.Pool) *TransactionManager {
|
||||
return &TransactionManager{pool: pool}
|
||||
}
|
||||
|
||||
// WithTx runs a function within a database transaction
|
||||
// If the function returns an error, the transaction is rolled back
|
||||
// If the function succeeds, the transaction is committed
|
||||
func (tm *TransactionManager) WithTx(ctx context.Context, fn func(pgx.Tx) error) error {
|
||||
tx, err := tm.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
|
||||
// Ensure the transaction is handled properly
|
||||
defer func() {
|
||||
if p := recover(); p != nil {
|
||||
// A panic occurred, rollback and re-panic
|
||||
_ = tx.Rollback(ctx)
|
||||
panic(p)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := fn(tx); err != nil {
|
||||
// Function returned error, rollback
|
||||
if rbErr := tx.Rollback(ctx); rbErr != nil {
|
||||
return fmt.Errorf("function error: %v, rollback error: %w", err, rbErr)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Success, commit
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("failed to commit transaction: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user