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
This commit is contained in:
Azareal 2019-07-28 14:58:01 +10:00
parent a465850adb
commit e9e527e76a
3 changed files with 43 additions and 27 deletions

View File

@ -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)
}

View File

@ -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

View File

@ -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
}
}
}
}