Skip to content

Commit

Permalink
feat: adding rendering error in boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Aug 1, 2024
1 parent 81a7d31 commit ad4021c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
16 changes: 11 additions & 5 deletions comps.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/go-playground/validator/v10"
"github.com/yuin/goldmark"
"github.com/zeiss/pkg/errorx"
)

// Node is a node in the HTML tree.
Expand Down Expand Up @@ -293,30 +294,35 @@ func (c errorBoundary) Render(w io.Writer) error {

type fallback struct {
n Node
f Node
f FallbackFunc
}

// FallbackFunc is a function that returns a node.
type FallbackFunc func(error) Node

// Fallback is a node that renders a fallback node if a condition is false.
func Fallback(n Node, f Node) Node {
func Fallback(n Node, f FallbackFunc) Node {
return fallback{n: n, f: f}
}

// Render is a node that renders a fallback node.
func (c fallback) Render(w io.Writer) (err error) {
if c.n == nil {
return c.f.Render(w)
n := c.f(nil)
return n.Render(w)
}

defer func() {
if r := recover(); r != nil {
err = c.f.Render(w)
n := c.f(errorx.RecoverError(r))
err = n.Render(w)
}
}()

var b bytes.Buffer

if err := c.n.Render(&b); err != nil {
return c.f.Render(w)
return c.f(err).Render(w)
}

_, err = io.Copy(w, &b)
Expand Down
10 changes: 7 additions & 3 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,18 @@ func (c *exampleController) Get() error {
),
),
),
htmx.Fallback(htmx.Markdown([]byte("## Hello, Markdown!")), htmx.Text("Fallback")),
htmx.Fallback(htmx.Markdown([]byte("## Hello, Markdown!")), func(err error) htmx.Node {
return htmx.Text("Fallback")
}),
htmx.Fallback(
htmx.ErrorBoundary(
func() htmx.Node {
return utils.Panic(errors.New("panic"))
return utils.Panic(errors.New("this is a new panic"))
},
),
htmx.Text("Fallback"),
func(err error) htmx.Node {
return htmx.Text(err.Error())
},
),
htmx.Raw(`<multi-select>Click me</multi-select>`),
dropdowns.Dropdown(
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/zeiss/fiber-htmx

go 1.22
go 1.22.0

toolchain go1.22.3
toolchain go1.22.5

require (
github.com/ettle/strcase v0.2.0
Expand Down Expand Up @@ -211,6 +211,7 @@ require (
github.com/yagipy/maintidx v1.0.0 // indirect
github.com/yeya24/promlinter v0.3.0 // indirect
github.com/ykadowak/zerologlint v0.1.5 // indirect
github.com/zeiss/pkg v0.1.4 // indirect
gitlab.com/bosi/decorder v0.4.2 // indirect
go-simpler.org/musttag v0.12.2 // indirect
go-simpler.org/sloglint v0.7.1 // indirect
Expand All @@ -219,7 +220,7 @@ require (
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp/typeparams v0.0.0-20240314144324-c7f7c6466f7f // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ github.com/zeiss/fiber-authz v1.0.31 h1:tcD9zkKHZNSVN6BYgKJ2cYwpbfT5p1+FyKqX1fx+
github.com/zeiss/fiber-authz v1.0.31/go.mod h1:kpmrBtJ23crBOwT7u0LwwnNywmDYhV4ez6L6oDCWkPA=
github.com/zeiss/fiber-goth v1.2.8 h1:8QoksnNLWD/3BbeCus5LS4eT1+YkDjz85bqYEL8A5iE=
github.com/zeiss/fiber-goth v1.2.8/go.mod h1:tYxywhiORkOUB2gRktCAgwz9i0Y+CTgBEwnQ3QZsAxU=
github.com/zeiss/pkg v0.1.3 h1:84xD8qdvvkgcvOsSP2GlwTwetGH+4pod6q1C/+WKQeY=
github.com/zeiss/pkg v0.1.3/go.mod h1:soMcxMxuhfHDni3KGF/diy90dqWhRI2uLNlKHV3IxPk=
github.com/zeiss/pkg v0.1.4 h1:maPLvFrffuRyiVtr+RSWSozXsWmlG9Bbz6JK3SoZGeY=
github.com/zeiss/pkg v0.1.4/go.mod h1:soMcxMxuhfHDni3KGF/diy90dqWhRI2uLNlKHV3IxPk=
gitlab.com/bosi/decorder v0.4.2 h1:qbQaV3zgwnBZ4zPMhGLW4KZe7A7NwxEhJx39R3shffo=
gitlab.com/bosi/decorder v0.4.2/go.mod h1:muuhHoaJkA9QLcYHq4Mj8FJUwDZ+EirSHRiaTcTf6T8=
go-simpler.org/assert v0.9.0 h1:PfpmcSvL7yAnWyChSjOz6Sp6m9j5lyK8Ok9pEL31YkQ=
Expand Down Expand Up @@ -532,6 +536,8 @@ golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down

0 comments on commit ad4021c

Please sign in to comment.