diff options
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/shortcuts/error.go | 35 | ||||
| -rw-r--r-- | utils/shortcuts/redirect.go | 21 |
2 files changed, 55 insertions, 1 deletions
diff --git a/utils/shortcuts/error.go b/utils/shortcuts/error.go index c22e6b2..ae6cec3 100644 --- a/utils/shortcuts/error.go +++ b/utils/shortcuts/error.go @@ -38,42 +38,77 @@ func ServiceError(kind ErrorKind, message string) *Error { } } +func isHtmxRequest(context *fiber.Ctx) bool { + return context.Get("HX-Request") == "true" && context.Get("HX-Boosted") != "true" +} + +func renderAlert(context *fiber.Ctx, message string, statusCode int) error { + context.Status(statusCode) + return context.Render("partials/alert", fiber.Map{ + "ErrorMessage": message, + }) +} + func HandleError(context *fiber.Ctx, serviceError *Error) error { statusCode, exists := statusMap[serviceError.Kind] if !exists { statusCode = fiber.StatusInternalServerError } + if isHtmxRequest(context) { + return renderAlert(context, serviceError.Message, statusCode) + } + return RenderWithStatus(context, "error", fiber.Map{ "ErrorMessage": serviceError.Message, }, statusCode) } func BadRequestError(context *fiber.Ctx, err error) error { + if isHtmxRequest(context) { + return renderAlert(context, err.Error(), fiber.StatusBadRequest) + } + return RenderWithStatus(context, "error", fiber.Map{ "ErrorMessage": err.Error(), }, fiber.StatusBadRequest) } func ForbiddenError(context *fiber.Ctx, err error) error { + if isHtmxRequest(context) { + return renderAlert(context, err.Error(), fiber.StatusForbidden) + } + return RenderWithStatus(context, "error", fiber.Map{ "ErrorMessage": err.Error(), }, fiber.StatusForbidden) } func InternalServerError(context *fiber.Ctx, err error) error { + if isHtmxRequest(context) { + return renderAlert(context, err.Error(), fiber.StatusInternalServerError) + } + return RenderWithStatus(context, "error", fiber.Map{ "ErrorMessage": err.Error(), }, fiber.StatusInternalServerError) } func NotFoundError(context *fiber.Ctx, err error) error { + if isHtmxRequest(context) { + return renderAlert(context, err.Error(), fiber.StatusNotFound) + } + return RenderWithStatus(context, "error", fiber.Map{ "ErrorMessage": err.Error(), }, fiber.StatusNotFound) } func UnauthorizedError(context *fiber.Ctx, err error) error { + if isHtmxRequest(context) { + return renderAlert(context, err.Error(), fiber.StatusUnauthorized) + } + return RenderWithStatus(context, "error", fiber.Map{ "ErrorMessage": err.Error(), }, fiber.StatusUnauthorized) diff --git a/utils/shortcuts/redirect.go b/utils/shortcuts/redirect.go index 627538d..8b2bb1d 100644 --- a/utils/shortcuts/redirect.go +++ b/utils/shortcuts/redirect.go @@ -12,14 +12,33 @@ func Redirect(context *fiber.Ctx, routeName string) error { return fiber.ErrNotFound } + if isHtmxRequest(context) { + context.Set("HX-Redirect", fullPath) + return context.SendStatus(fiber.StatusNoContent) + } + return context.Redirect(fullPath) } +func RedirectToPath(context *fiber.Ctx, path string) error { + if isHtmxRequest(context) { + context.Set("HX-Redirect", path) + return context.SendStatus(fiber.StatusNoContent) + } + + return context.Redirect(path) +} + func RedirectWithStatus(context *fiber.Ctx, routeName string, statusCode int) error { fullPath, exists := urls.GetFullPath(routeName) if !exists { return fiber.ErrNotFound } + if isHtmxRequest(context) { + context.Set("HX-Redirect", fullPath) + return context.SendStatus(fiber.StatusNoContent) + } + return context.Redirect(fullPath, statusCode) -}
\ No newline at end of file +} |
