template functions now panic on error for quicker error handling

This commit is contained in:
amy
2026-11-12 14:54:45 -07:00
parent 630d129bb5
commit b6632b2d09
3 changed files with 50 additions and 50 deletions
+21 -24
View File
@@ -14,7 +14,6 @@ package main
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"reflect" "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. // templateExtractUserName extracts the user name from the post.
func templateExtractUserName(args jet.Arguments) reflect.Value { func templateExtractUserName(args jet.Arguments) reflect.Value {
rc := "<<ERROR>>"
post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader) post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader)
ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext) ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext)
user, err := database.AmGetUser(ctxt.Ctx(), post.CreatorUid) user, err := database.AmGetUser(ctxt.Ctx(), post.CreatorUid)
if err == nil { if err != nil {
rc = user.Username args.Panicf("templateExtractUserName failed to get user #%d: %v", post.CreatorUid, err)
} else {
log.Errorf("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. // 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) ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext)
rc, err := post.Text(ctxt.Ctx()) rc, err := post.Text(ctxt.Ctx())
if err != nil { if err != nil {
if errors.Is(err, context.Canceled) { args.Panicf("templatePostText could not get post text from post #%d: %v", post.PostId, err)
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 = ""
} }
return reflect.ValueOf(rc) 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) scr_date = p.Localizer().Strftime("%b %e, %Y %r", *post.ScribbleDate)
} }
} }
if err == nil { if err != nil {
rc = fmt.Sprintf("(Scribbled by %s on %s)", scr_user.Username, scr_date) args.Panicf("failed to get prefs for current user in templateOverrideLine - %v", err)
} else {
rc = fmt.Sprintf("<<<%v>>>", err)
} }
rc = fmt.Sprintf("(Scribbled by %s on %s)", scr_user.Username, scr_date)
} else if post.Hidden && !advanced { } else if post.Hidden && !advanced {
rc = fmt.Sprintf("(Hidden Message: %d Lines)", *post.LineCount) 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 { func templateAttachmentInfo(args jet.Arguments) reflect.Value {
post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader) post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader)
ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext) 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 { if rc == nil {
rc = &database.PostAttachInfo{} 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) post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader)
topic := args.Get(1).Convert(reflect.TypeFor[*database.Topic]()).Interface().(*database.Topic) topic := args.Get(1).Convert(reflect.TypeFor[*database.Topic]()).Interface().(*database.Topic)
ctxt := args.Get(2).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext) 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) return reflect.ValueOf(rc)
} }
@@ -452,14 +447,16 @@ func templateProfileImage(args jet.Arguments) reflect.Value {
ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext) ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext)
rc := config.GlobalConfig.Site.DefaultUserPhoto rc := config.GlobalConfig.Site.DefaultUserPhoto
user, err := post.Creator(ctxt.Ctx()) user, err := post.Creator(ctxt.Ctx())
if err == nil { if err != nil {
args.Panicf("templateProfileImage unable to get post creator for post #%d: %v", post.PostId, err)
}
ci, err := user.ContactInfo(ctxt.Ctx()) ci, err := user.ContactInfo(ctxt.Ctx())
if err == nil { if err != nil {
args.Panicf("templateProfileImage unable to get contact info for user #%d: %v", user.Uid, err)
}
if ci.PhotoURL != nil { if ci.PhotoURL != nil {
rc = *ci.PhotoURL rc = *ci.PhotoURL
} }
}
}
return reflect.ValueOf(rc) return reflect.ValueOf(rc)
} }
+8 -2
View File
@@ -170,7 +170,10 @@ func (rx *sbRender) SetVar(name string, value any) {
func templateGetTopic(args jet.Arguments) reflect.Value { func templateGetTopic(args jet.Arguments) reflect.Value {
post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader) post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader)
ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext) 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) 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) comm := args.Get(0).Convert(reflect.TypeFor[*database.Community]()).Interface().(*database.Community)
topic := args.Get(1).Convert(reflect.TypeFor[*database.Topic]()).Interface().(*database.Topic) topic := args.Get(1).Convert(reflect.TypeFor[*database.Topic]()).Interface().(*database.Topic)
ctxt := args.Get(2).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext) 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) return reflect.ValueOf(link)
} }
+14 -17
View File
@@ -14,7 +14,6 @@ package ui
import ( import (
"embed" "embed"
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@@ -86,18 +85,16 @@ func makeYearRange(a jet.Arguments) reflect.Value {
param := a.Get(0).Convert(reflect.TypeFor[string]()).String() param := a.Get(0).Convert(reflect.TypeFor[string]()).String()
yearRegex, _ := regexp.Compile(`year:(\S+)(\s+.+)?$`) yearRegex, _ := regexp.Compile(`year:(\S+)(\s+.+)?$`)
m := yearRegex.FindStringSubmatch(param) m := yearRegex.FindStringSubmatch(param)
if m != nil { if m == nil {
a.Panicf("cannot locate year: marker in param %s", param)
}
count, err := strconv.Atoi(m[1]) count, err := strconv.Atoi(m[1])
if err == nil { if err != nil {
a.Panicf("bad string in yearRange: %s - %v", param, err)
}
start_year := time.Now().Year() start_year := time.Now().Year()
rc := &countRanger{i: -1, val: int64(start_year) + 1, step: -1, to: int64(start_year + count - 1)} 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]()) 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"))
}
} }
// immediateIf returns its 2nd argument if its 1st argument is true, and its 3rd argument if its 1st argument is false. // 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]()) { } else if p0.CanConvert(reflect.TypeFor[*time.Time]()) {
ptr := p0.Convert(reflect.TypeFor[*time.Time]()).Interface().(*time.Time) ptr := p0.Convert(reflect.TypeFor[*time.Time]()).Interface().(*time.Time)
if ptr == nil { if ptr == nil {
return reflect.ValueOf("<<NIL>>") a.Panicf("null date/time value in displayDateTime")
} }
timeval = *ptr timeval = *ptr
} else { } else {
return reflect.ValueOf("<<BOGUS>>") a.Panicf("bogus date/time value in displayDateTime")
} }
ctxt := a.Get(1).Convert(reflect.TypeFor[AmContext]()).Interface().(AmContext) ctxt := a.Get(1).Convert(reflect.TypeFor[AmContext]()).Interface().(AmContext)
prefs, err := ctxt.CurrentUser().Prefs(ctxt.Ctx()) prefs, err := ctxt.CurrentUser().Prefs(ctxt.Ctx())
if err == nil { if err != nil {
a.Panicf("unable to get prefs for current user in displayDateTime - %v", err)
}
loc := prefs.Localizer() loc := prefs.Localizer()
return reflect.ValueOf(loc.Strftime("%b %e, %Y %r", timeval)) return reflect.ValueOf(loc.Strftime("%b %e, %Y %r", timeval))
}
return reflect.ValueOf(fmt.Sprintf("<<%v>>", err))
} }
// displayActivity displays an activity string formatted to the user's preferences. // 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) timeval := a.Get(0).Convert(reflect.TypeFor[*time.Time]()).Interface().(*time.Time)
ctxt := a.Get(1).Convert(reflect.TypeFor[AmContext]()).Interface().(AmContext) ctxt := a.Get(1).Convert(reflect.TypeFor[AmContext]()).Interface().(AmContext)
prefs, err := ctxt.CurrentUser().Prefs(ctxt.Ctx()) prefs, err := ctxt.CurrentUser().Prefs(ctxt.Ctx())
if err == nil { if err != nil {
return reflect.ValueOf(util.AmActivityString(timeval, prefs.Localizer())) 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. // displayMemberCount displays the current member count of a community.