Skip to content

Commit

Permalink
Experimenting with request state in local errors.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Azareal committed Jul 28, 2019
1 parent a465850 commit e9e527e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
20 changes: 18 additions & 2 deletions common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
14 changes: 7 additions & 7 deletions routes/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
36 changes: 18 additions & 18 deletions routes/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down Expand Up @@ -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
}
}
}
}

Expand Down

0 comments on commit e9e527e

Please sign in to comment.