bouncer/internal/admin/error.go
William Petit d4c28b80d7
Some checks are pending
Cadoles/bouncer/pipeline/pr-develop Build started...
feat: global error handler with template rendering
2024-09-27 15:02:49 +02:00

36 lines
808 B
Go

package admin
import (
"context"
"fmt"
"net/http"
"forge.cadoles.com/cadoles/bouncer/internal/schema"
"gitlab.com/wpetit/goweb/api"
"gitlab.com/wpetit/goweb/logger"
)
const ErrCodeAlreadyExist api.ErrorCode = "already-exist"
func invalidDataErrorResponse(w http.ResponseWriter, r *http.Request, err *schema.InvalidDataError) {
keyErrors := err.KeyErrors()
message := ""
for idx, err := range keyErrors {
if idx != 0 {
message += ", "
}
message += fmt.Sprintf("Path [%s]: %s", err.PropertyPath, err.Message)
}
api.ErrorResponse(w, http.StatusBadRequest, api.ErrCodeInvalidRequest, &struct {
Message string `json:"message"`
}{
Message: message,
})
}
func logAndCaptureError(ctx context.Context, message string, err error) {
logger.Error(ctx, message, logger.CapturedE(err))
}