commit eb4f7cc467af45941abf5fa4a0a86c1ce8b4f929
parent 8cf6dbaec7f7d0f2dbeaf0f14cd66c35cbf1588e
Author: lash <dev@holbrook.no>
Date: Sun, 23 Apr 2023 09:32:24 +0100
Add error render tests
Diffstat:
2 files changed, 33 insertions(+), 6 deletions(-)
diff --git a/render/page.go b/render/page.go
@@ -56,8 +56,8 @@ func(pg *Page) WithError(err error) *Page {
return pg
}
-// WithOpaqueError sets an error which will be used for display regardless of which error was set using WithError
-func(pg *Page) WithOpaqueError(err error) *Page {
+// WithFixedError sets an error which will be used for display regardless of which error was set using WithError
+func(pg *Page) WithFixedError(err error) *Page {
pg.errConst = err
return pg
}
@@ -65,7 +65,7 @@ func(pg *Page) WithOpaqueError(err error) *Page {
// Error implements error interface.
//
// This error is used internally by the renderer, and is not intended for direct use.
-func(pg *Page) ErrorString() string {
+func(pg *Page) Error() string {
if pg.err != nil {
if pg.errConst != nil {
return pg.errConst.Error()
@@ -162,11 +162,12 @@ func(pg *Page) RenderTemplate(sym string, values map[string]string, idx uint16,
return "", err
}
if pg.err != nil {
- Logg.DebugCtxf(ctx, "Prepending error: %s", pg.err)
+ derr := pg.Error()
+ Logg.DebugCtxf(ctx, "prepending error", "err", pg.err, "display", derr)
if len(tpl) == 0 {
- tpl = pg.err.Error()
+ tpl = derr
} else {
- tpl = fmt.Sprintf("%s\n%s", pg.err, tpl)
+ tpl = fmt.Sprintf("%s\n%s", derr, tpl)
}
}
if pg.sizer != nil {
diff --git a/render/page_test.go b/render/page_test.go
@@ -1,6 +1,7 @@
package render
import (
+ "context"
"fmt"
"testing"
@@ -119,6 +120,7 @@ func TestStateMapSink(t *testing.T) {
func TestWithError(t *testing.T) {
ca := cache.NewCache()
rs := resource.NewMemResource()
+ rs.AddTemplate("foo", "bar")
pg := NewPage(ca, rs)
ca.Push()
@@ -130,4 +132,28 @@ func TestWithError(t *testing.T) {
err = fmt.Errorf("my humps")
pg = pg.WithMenu(mn).WithError(err)
+ ctx := context.TODO()
+ r, err := pg.Render("foo", 0, ctx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ expect := `my humps
+bar
+0:aiee`
+ if r != expect {
+ t.Fatalf("expected:\n\t%s\ngot:\n\t%s", expect, r)
+ }
+
+ err = fmt.Errorf("my lumps")
+ pg = pg.WithFixedError(err)
+ r, err = pg.Render("foo", 0, ctx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ expect = `my lumps
+bar
+0:aiee`
+ if r != expect {
+ t.Fatalf("expected:\n\t%s\ngot:\n\t%s", expect, r)
+ }
}