Skip to content

Commit

Permalink
feat: add error handler for toasts
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Sep 12, 2024
1 parent f4a2f34 commit b9c5b3e
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions components/toasts/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package toasts

import (
"errors"

"github.com/gofiber/fiber/v2"
htmx "github.com/zeiss/fiber-htmx"
)

// DefaultErrorHandler is the default error handler for toasts.
var DefaultErrorHandler = func(c *fiber.Ctx, err error) error {
code := fiber.StatusInternalServerError

var te Toast
if !errors.As(err, &te) {
te = Error("there has been an unexpected error")
}

if te.Level != SUCCESS {
htmx.ReSwap(c, "none")
}

te.SetHXTriggerHeader(c)

var e *fiber.Error
if errors.As(err, &e) {
code = e.Code
}

c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)

return c.Status(code).SendString(err.Error())
}

0 comments on commit b9c5b3e

Please sign in to comment.