From e9e527e76a120787f3dcfa95477996b56e479c1f Mon Sep 17 00:00:00 2001 From: Azareal Date: Sun, 28 Jul 2019 14:58:01 +1000 Subject: [PATCH] Experimenting with request state in local errors. Move the sheet and style declarations after the initial error on the profiles. Use a global URL ID error message for the profiles. Updates #51 --- common/errors.go | 20 ++++++++++++++++++-- routes/profile.go | 14 +++++++------- routes/topic.go | 36 ++++++++++++++++++------------------ 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/common/errors.go b/common/errors.go index dd496282..5ddeeb1d 100644 --- a/common/errors.go +++ b/common/errors.go @@ -212,16 +212,32 @@ func PreErrorJSQ(errmsg string, w http.ResponseWriter, r *http.Request, isJs boo // LocalError is an error shown to the end-user when something goes wrong and it's not the software's fault // TODO: Pass header in for this and similar errors instead of having to pass in both user and w? Would also allow for more stateful things, although this could be a problem -func LocalError(errmsg string, w http.ResponseWriter, r *http.Request, user User) RouteError { +/*func LocalError(errmsg string, w http.ResponseWriter, r *http.Request, user User) RouteError { w.WriteHeader(500) pi := ErrorPage{errorHeader(w, user, phrases.GetErrorPhrase("local_error_title")), errmsg} handleErrorTemplate(w, r, pi) return HandledRouteError() +}*/ + +func LocalError(errmsg string, w http.ResponseWriter, r *http.Request, user User) RouteError { + return SimpleError(errmsg, w, r, errorHeader(w, user, "")) +} + +func SimpleError(errmsg string, w http.ResponseWriter, r *http.Request, header *Header) RouteError { + if header == nil { + header = errorHeader(w, GuestUser, phrases.GetErrorPhrase("local_error_title")) + } else { + header.Title = phrases.GetErrorPhrase("local_error_title") + } + w.WriteHeader(500) + pi := ErrorPage{header, errmsg} + handleErrorTemplate(w, r, pi) + return HandledRouteError() } func LocalErrorJSQ(errmsg string, w http.ResponseWriter, r *http.Request, user User, isJs bool) RouteError { if !isJs { - return LocalError(errmsg, w, r, user) + return SimpleError(errmsg, w, r, errorHeader(w, user, "")) } return LocalErrorJS(errmsg, w, r) } diff --git a/routes/profile.go b/routes/profile.go index b9d33fa8..71a3b2f2 100644 --- a/routes/profile.go +++ b/routes/profile.go @@ -28,12 +28,6 @@ func init() { // TODO: Remove the View part of the name? func ViewProfile(w http.ResponseWriter, r *http.Request, user c.User, header *c.Header) c.RouteError { - // TODO: Preload this? - header.AddSheet(header.Theme.Name + "/profile.css") - if user.Loggedin { - header.AddScriptAsync("profile_member.js") - } - var err error var replyCreatedAt time.Time var replyContent, replyCreatedByName, replyAvatar string @@ -43,7 +37,13 @@ func ViewProfile(w http.ResponseWriter, r *http.Request, user c.User, header *c. // TODO: Do a 301 if it's the wrong username? Do a canonical too? _, pid, err := ParseSEOURL(r.URL.Path[len("/user/"):]) if err != nil { - return c.LocalError("The provided UserID is not a valid number.", w, r, user) + return c.SimpleError(phrases.GetErrorPhrase("url_id_must_be_integer"),w,r,header) + } + + // TODO: Preload this? + header.AddSheet(header.Theme.Name + "/profile.css") + if user.Loggedin { + header.AddScriptAsync("profile_member.js") } var puser *c.User diff --git a/routes/topic.go b/routes/topic.go index 7bfd2007..5b69cdbc 100644 --- a/routes/topic.go +++ b/routes/topic.go @@ -44,7 +44,7 @@ func ViewTopic(w http.ResponseWriter, r *http.Request, user c.User, header *c.He page, _ := strconv.Atoi(r.FormValue("page")) _, tid, err := ParseSEOURL(urlBit) if err != nil { - return c.PreError(phrases.GetErrorPhrase("url_id_must_be_integer"), w, r) + return c.SimpleError(phrases.GetErrorPhrase("url_id_must_be_integer"),w,r,header) } // Get the topic... @@ -369,26 +369,26 @@ func CreateTopicSubmit(w http.ResponseWriter, r *http.Request, user c.User) c.Ro if !strings.HasPrefix(key, "pollinputitem[") { continue } - halves := strings.Split(key, "[") - if len(halves) != 2 { - return c.LocalError("Malformed pollinputitem", w, r, user) - } - halves[1] = strings.TrimSuffix(halves[1], "]") + halves := strings.Split(key, "[") + if len(halves) != 2 { + return c.LocalError("Malformed pollinputitem", w, r, user) + } + halves[1] = strings.TrimSuffix(halves[1], "]") - index, err := strconv.Atoi(halves[1]) - if err != nil { - return c.LocalError("Malformed pollinputitem", w, r, user) - } + index, err := strconv.Atoi(halves[1]) + if err != nil { + return c.LocalError("Malformed pollinputitem", w, r, user) + } - // If there are duplicates, then something has gone horribly wrong, so let's ignore them, this'll likely happen during an attack - _, exists := pollInputItems[index] - // TODO: Should we use SanitiseBody instead to keep the newlines? - if !exists && len(c.SanitiseSingleLine(value)) != 0 { - pollInputItems[index] = c.SanitiseSingleLine(value) - if len(pollInputItems) >= maxPollOptions { - break - } + // If there are duplicates, then something has gone horribly wrong, so let's ignore them, this'll likely happen during an attack + _, exists := pollInputItems[index] + // TODO: Should we use SanitiseBody instead to keep the newlines? + if !exists && len(c.SanitiseSingleLine(value)) != 0 { + pollInputItems[index] = c.SanitiseSingleLine(value) + if len(pollInputItems) >= maxPollOptions { + break } + } } }