Skip to content

Commit 979c82a

Browse files
feat: better than nothing HTMX error handling
1 parent 123e763 commit 979c82a

File tree

8 files changed

+29
-9
lines changed

8 files changed

+29
-9
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ make dev-web
227227
- Reload on config change
228228
- Add [Apprise](https://github.com/caronc/apprise) endpoint
229229
- HTTP and SMTP auth
230-
- Better HTTP error handling with [HTMX](https://htmx.org/)
231230
- CRUD endpoints and rules
232231
- SQLite full text search
233232
- Read mailbox files

web/controllers/controllers.go

+4
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,7 @@ func Trim(c *fiber.Ctx, cc *core.Context) error {
114114
c.Set("HX-Trigger", "trimmed")
115115
return c.SendStatus(http.StatusNoContent)
116116
}
117+
118+
func SomethingWentWrong(c *fiber.Ctx) error {
119+
return c.Render("something-went-wrong", nil)
120+
}

web/controllers/rules.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func Rules(c *fiber.Ctx, cc *core.Context) error {
2020
})
2121
}
2222

23-
func RuleEnable(c *fiber.Ctx, cc *core.Context, id int64) error {
23+
func RulePEnableForm(c *fiber.Ctx, cc *core.Context, id int64) error {
2424
// Request
2525
enable := c.FormValue("enable") == "on"
2626

web/controllers/utils.go

-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,4 @@ func checkbox(c *fiber.Ctx, key string) bool {
99
}
1010

1111
return true
12-
13-
}
14-
15-
func partial(c *fiber.Ctx, name string, bind interface{}) error {
16-
return c.Render(name, bind, "")
1712
}

web/helpers/helpers.go

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/ItsNotGoodName/smtpbridge/internal/build"
99
"github.com/ItsNotGoodName/smtpbridge/web"
1010
"github.com/dustin/go-humanize"
11+
"github.com/gofiber/fiber/v2"
1112
)
1213

1314
var Map template.FuncMap = template.FuncMap{
@@ -41,3 +42,8 @@ var Map template.FuncMap = template.FuncMap{
4142
return template.URL(Query(queries, vals...))
4243
},
4344
}
45+
46+
func IsHTMXRequest(c *fiber.Ctx) bool {
47+
_, isHTMXRequest := c.GetReqHeaders()["Hx-Request"]
48+
return isHTMXRequest
49+
}

web/http/http.go

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ func New(app core.App, shutdown context.CancelFunc, address string, bodyLimit in
2929
Views: views,
3030
ViewsLayout: "layouts/index",
3131
BodyLimit: bodyLimit,
32+
ErrorHandler: func(c *fiber.Ctx, err error) error {
33+
if helpers.IsHTMXRequest(c) {
34+
c.Set("HX-Redirect", "/something-went-wrong")
35+
}
36+
37+
return fiber.DefaultErrorHandler(c, err)
38+
},
3239
})
3340

3441
// Middleware

web/http/routes.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ func routes(http *fiber.App, app core.App, retentionPolicy models.RetentionPolic
4141
http.Route("/rules", func(http fiber.Router) {
4242
http.Get("/", middleware.App(app, controllers.Rules))
4343
http.Route("/:id", func(http fiber.Router) {
44-
http.Post("/enable", middleware.AppID(app, controllers.RuleEnable))
44+
http.Post("/enable", middleware.AppID(app, controllers.RulePEnableForm))
4545
})
4646
})
4747

4848
http.Post("/send", middleware.App(app, controllers.Send))
4949
http.Post("/vacuum", middleware.App(app, controllers.Vacuum))
5050
http.Post("/trim", middleware.App(app, controllers.Trim))
5151
http.Group("/files", controllers.Files(app))
52-
52+
http.Get("/something-went-wrong", controllers.SomethingWentWrong)
5353
}

web/views/something-went-wrong.html

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{{template "partials/header" .}}
2+
3+
<main class="container">
4+
<section>
5+
Something went wrong.
6+
</section>
7+
</main>
8+
9+
{{template "partials/footer" .}}

0 commit comments

Comments
 (0)