From b6632b2d09af2b2b234f6f8b4cbf0ae64257aef1 Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Thu, 12 Nov 2026 14:54:45 -0700 Subject: [PATCH] template functions now panic on error for quicker error handling --- conference.go | 51 +++++++++++++++++++++++-------------------------- top.go | 10 ++++++++-- ui/templates.go | 39 +++++++++++++++++-------------------- 3 files changed, 50 insertions(+), 50 deletions(-) diff --git a/conference.go b/conference.go index 33b9ed5..9d2502a 100644 --- a/conference.go +++ b/conference.go @@ -14,7 +14,6 @@ package main import ( "context" - "errors" "fmt" "net/http" "reflect" @@ -358,16 +357,13 @@ func breakRange(topic *database.Topic, into []int32, param string, sep string) e // templateExtractUserName extracts the user name from the post. func templateExtractUserName(args jet.Arguments) reflect.Value { - rc := "<>" post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader) ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext) user, err := database.AmGetUser(ctxt.Ctx(), post.CreatorUid) - if err == nil { - rc = user.Username - } else { - log.Errorf("templateExtractUserName failed to get user #%d: %v", post.CreatorUid, err) + if err != nil { + args.Panicf("templateExtractUserName failed to get user #%d: %v", post.CreatorUid, err) } - return reflect.ValueOf(rc) + return reflect.ValueOf(user.Username) } // templatePostText gets the text of a post. @@ -376,13 +372,7 @@ func templatePostText(args jet.Arguments) reflect.Value { ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext) rc, err := post.Text(ctxt.Ctx()) if err != nil { - if errors.Is(err, context.Canceled) { - log.Errorf("templatePostText could not get post text from post #%d: context %s canceled: %v", post.PostId, - ctxt.Ctx().Value(CONTEXTVAL_NAME), ctxt.Ctx().Err()) - } else { - log.Errorf("templatePostText could not get post text from post #%d: %v", post.PostId, err) - } - rc = "" + args.Panicf("templatePostText could not get post text from post #%d: %v", post.PostId, err) } return reflect.ValueOf(rc) } @@ -403,11 +393,10 @@ func templateOverrideLine(args jet.Arguments) reflect.Value { scr_date = p.Localizer().Strftime("%b %e, %Y %r", *post.ScribbleDate) } } - if err == nil { - rc = fmt.Sprintf("(Scribbled by %s on %s)", scr_user.Username, scr_date) - } else { - rc = fmt.Sprintf("<<<%v>>>", err) + if err != nil { + args.Panicf("failed to get prefs for current user in templateOverrideLine - %v", err) } + rc = fmt.Sprintf("(Scribbled by %s on %s)", scr_user.Username, scr_date) } else if post.Hidden && !advanced { rc = fmt.Sprintf("(Hidden Message: %d Lines)", *post.LineCount) } @@ -430,7 +419,10 @@ func templateOverrideLink(args jet.Arguments) reflect.Value { func templateAttachmentInfo(args jet.Arguments) reflect.Value { post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader) ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext) - rc, _ := post.AttachmentInfo(ctxt.Ctx()) + rc, err := post.AttachmentInfo(ctxt.Ctx()) + if err != nil { + args.Panicf("templateAttachmentInfo unable to get attachment info for post #%d: %v", post.PostId, err) + } if rc == nil { rc = &database.PostAttachInfo{} } @@ -442,7 +434,10 @@ func templateBozo(args jet.Arguments) reflect.Value { post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader) topic := args.Get(1).Convert(reflect.TypeFor[*database.Topic]()).Interface().(*database.Topic) ctxt := args.Get(2).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext) - rc, _ := topic.IsBozo(ctxt.Ctx(), ctxt.CurrentUser(), post.CreatorUid) + rc, err := topic.IsBozo(ctxt.Ctx(), ctxt.CurrentUser(), post.CreatorUid) + if err != nil { + args.Panicf("templateBozo unable to get status for user #%d in topic #%d: %v", post.CreatorUid, topic.TopicId, err) + } return reflect.ValueOf(rc) } @@ -452,13 +447,15 @@ func templateProfileImage(args jet.Arguments) reflect.Value { ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext) rc := config.GlobalConfig.Site.DefaultUserPhoto user, err := post.Creator(ctxt.Ctx()) - if err == nil { - ci, err := user.ContactInfo(ctxt.Ctx()) - if err == nil { - if ci.PhotoURL != nil { - rc = *ci.PhotoURL - } - } + if err != nil { + args.Panicf("templateProfileImage unable to get post creator for post #%d: %v", post.PostId, err) + } + ci, err := user.ContactInfo(ctxt.Ctx()) + if err != nil { + args.Panicf("templateProfileImage unable to get contact info for user #%d: %v", user.Uid, err) + } + if ci.PhotoURL != nil { + rc = *ci.PhotoURL } return reflect.ValueOf(rc) } diff --git a/top.go b/top.go index c3ff2c1..91f2643 100644 --- a/top.go +++ b/top.go @@ -170,7 +170,10 @@ func (rx *sbRender) SetVar(name string, value any) { func templateGetTopic(args jet.Arguments) reflect.Value { post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader) ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext) - topic, _ := database.AmGetTopic(ctxt.Ctx(), post.TopicId) + topic, err := database.AmGetTopic(ctxt.Ctx(), post.TopicId) + if err != nil { + args.Panicf("templateGetTopic unable to get topic for post #%d: %v", post.PostId, err) + } return reflect.ValueOf(topic) } @@ -179,7 +182,10 @@ func templateTopicLink(args jet.Arguments) reflect.Value { comm := args.Get(0).Convert(reflect.TypeFor[*database.Community]()).Interface().(*database.Community) topic := args.Get(1).Convert(reflect.TypeFor[*database.Topic]()).Interface().(*database.Topic) ctxt := args.Get(2).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext) - link, _ := topic.Link(ctxt.Ctx(), comm.Id, "global") + link, err := topic.Link(ctxt.Ctx(), comm.Id, "global") + if err != nil { + args.Panicf("templateTopicLink unable to get link for topic %d: %v", topic.TopicId, err) + } return reflect.ValueOf(link) } diff --git a/ui/templates.go b/ui/templates.go index 8c9f349..7417dd2 100644 --- a/ui/templates.go +++ b/ui/templates.go @@ -14,7 +14,6 @@ package ui import ( "embed" - "errors" "fmt" "io" "os" @@ -86,18 +85,16 @@ func makeYearRange(a jet.Arguments) reflect.Value { param := a.Get(0).Convert(reflect.TypeFor[string]()).String() yearRegex, _ := regexp.Compile(`year:(\S+)(\s+.+)?$`) m := yearRegex.FindStringSubmatch(param) - if m != nil { - count, err := strconv.Atoi(m[1]) - if err == nil { - start_year := time.Now().Year() - rc := &countRanger{i: -1, val: int64(start_year) + 1, step: -1, to: int64(start_year + count - 1)} - return reflect.ValueOf(rc).Convert(reflect.TypeFor[jet.Ranger]()) - } else { - return reflect.ValueOf(err) - } - } else { - return reflect.ValueOf(errors.New("cannot locate year: marker in param")) + if m == nil { + a.Panicf("cannot locate year: marker in param %s", param) } + count, err := strconv.Atoi(m[1]) + if err != nil { + a.Panicf("bad string in yearRange: %s - %v", param, err) + } + start_year := time.Now().Year() + rc := &countRanger{i: -1, val: int64(start_year) + 1, step: -1, to: int64(start_year + count - 1)} + return reflect.ValueOf(rc).Convert(reflect.TypeFor[jet.Ranger]()) } // immediateIf returns its 2nd argument if its 1st argument is true, and its 3rd argument if its 1st argument is false. @@ -156,19 +153,19 @@ func displayDateTime(a jet.Arguments) reflect.Value { } else if p0.CanConvert(reflect.TypeFor[*time.Time]()) { ptr := p0.Convert(reflect.TypeFor[*time.Time]()).Interface().(*time.Time) if ptr == nil { - return reflect.ValueOf("<>") + a.Panicf("null date/time value in displayDateTime") } timeval = *ptr } else { - return reflect.ValueOf("<>") + a.Panicf("bogus date/time value in displayDateTime") } ctxt := a.Get(1).Convert(reflect.TypeFor[AmContext]()).Interface().(AmContext) prefs, err := ctxt.CurrentUser().Prefs(ctxt.Ctx()) - if err == nil { - loc := prefs.Localizer() - return reflect.ValueOf(loc.Strftime("%b %e, %Y %r", timeval)) + if err != nil { + a.Panicf("unable to get prefs for current user in displayDateTime - %v", err) } - return reflect.ValueOf(fmt.Sprintf("<<%v>>", err)) + loc := prefs.Localizer() + return reflect.ValueOf(loc.Strftime("%b %e, %Y %r", timeval)) } // displayActivity displays an activity string formatted to the user's preferences. @@ -176,10 +173,10 @@ func displayActivity(a jet.Arguments) reflect.Value { timeval := a.Get(0).Convert(reflect.TypeFor[*time.Time]()).Interface().(*time.Time) ctxt := a.Get(1).Convert(reflect.TypeFor[AmContext]()).Interface().(AmContext) prefs, err := ctxt.CurrentUser().Prefs(ctxt.Ctx()) - if err == nil { - return reflect.ValueOf(util.AmActivityString(timeval, prefs.Localizer())) + if err != nil { + a.Panicf("unable to get prefs for current user in displayActivity - %v", err) } - return reflect.ValueOf(fmt.Sprintf("<<%v>>", err)) + return reflect.ValueOf(util.AmActivityString(timeval, prefs.Localizer())) } // displayMemberCount displays the current member count of a community.