Fixed the bug where errors get sent in plaintext rather than as formatted HTML.

This commit is contained in:
Azareal 2016-12-03 11:06:47 +00:00
parent 8ff76d1bc8
commit 82b351e1d7
2 changed files with 68 additions and 32 deletions

View File

@ -1,4 +1,5 @@
package main package main
import "fmt"
import "log" import "log"
import "bytes" import "bytes"
import "net/http" import "net/http"
@ -9,7 +10,8 @@ func InternalError(err error, w http.ResponseWriter, r *http.Request, user User)
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
} }
func InternalErrorJSQ(err error, w http.ResponseWriter, r *http.Request, user User, is_js string) { func InternalErrorJSQ(err error, w http.ResponseWriter, r *http.Request, user User, is_js string) {
@ -20,7 +22,8 @@ func InternalErrorJSQ(err error, w http.ResponseWriter, r *http.Request, user Us
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
} else { } else {
http.Error(w,"{'errmsg': '" + errmsg + "'}",500) http.Error(w,"{'errmsg': '" + errmsg + "'}",500)
} }
@ -31,7 +34,8 @@ func LocalError(errmsg string, w http.ResponseWriter, r *http.Request, user User
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
} }
func LoginRequired(w http.ResponseWriter, r *http.Request, user User) { func LoginRequired(w http.ResponseWriter, r *http.Request, user User) {
@ -41,7 +45,8 @@ func LoginRequired(w http.ResponseWriter, r *http.Request, user User) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,401) w.WriteHeader(401)
fmt.Fprintln(w,errpage)
} }
func LocalErrorJSQ(errmsg string, w http.ResponseWriter, r *http.Request, user User, is_js string) { func LocalErrorJSQ(errmsg string, w http.ResponseWriter, r *http.Request, user User, is_js string) {
@ -50,7 +55,8 @@ func LocalErrorJSQ(errmsg string, w http.ResponseWriter, r *http.Request, user U
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
} else { } else {
http.Error(w,"{'errmsg': '" + errmsg + "'}",500) http.Error(w,"{'errmsg': '" + errmsg + "'}",500)
} }
@ -62,7 +68,8 @@ func NoPermissions(w http.ResponseWriter, r *http.Request, user User) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,403) w.WriteHeader(403)
fmt.Fprintln(w,errpage)
} }
func NoPermissionsJSQ(w http.ResponseWriter, r *http.Request, user User, is_js string) { func NoPermissionsJSQ(w http.ResponseWriter, r *http.Request, user User, is_js string) {
@ -72,7 +79,8 @@ func NoPermissionsJSQ(w http.ResponseWriter, r *http.Request, user User, is_js s
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,403) w.WriteHeader(403)
fmt.Fprintln(w,errpage)
} else { } else {
http.Error(w,"{'errmsg': '" + errmsg + "'}",403) http.Error(w,"{'errmsg': '" + errmsg + "'}",403)
} }
@ -85,7 +93,8 @@ func LoginRequiredJSQ(w http.ResponseWriter, r *http.Request, user User, is_js s
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,401) w.WriteHeader(401)
fmt.Fprintln(w,errpage)
} else { } else {
http.Error(w,"{'errmsg': '" + errmsg + "'}",401) http.Error(w,"{'errmsg': '" + errmsg + "'}",401)
} }
@ -97,7 +106,8 @@ func CustomErrorJSQ(errmsg string, errcode int, errtitle string, w http.Response
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
} else { } else {
http.Error(w,"{'errmsg': '" + errmsg + "'}",500) http.Error(w,"{'errmsg': '" + errmsg + "'}",500)
} }

View File

@ -21,6 +21,9 @@ var tList map[int]interface{}
// GET functions // GET functions
func route_overview(w http.ResponseWriter, r *http.Request){ func route_overview(w http.ResponseWriter, r *http.Request){
user := SessionCheck(w,r) user := SessionCheck(w,r)
NoPermissions(w, r, user)
return
pi := Page{"Overview","overview",user,tList,0} pi := Page{"Overview","overview",user,tList,0}
err := templates.ExecuteTemplate(w,"overview.html", pi) err := templates.ExecuteTemplate(w,"overview.html", pi)
if err != nil { if err != nil {
@ -43,7 +46,8 @@ func route_custom_page(w http.ResponseWriter, r *http.Request){
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,404) w.WriteHeader(404)
fmt.Fprintln(w,errpage)
} }
} }
@ -143,7 +147,8 @@ func route_topic_id(w http.ResponseWriter, r *http.Request){
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,404) w.WriteHeader(404)
fmt.Fprintln(w,errpage)
return return
} else if err != nil { } else if err != nil {
InternalError(err,w,r,user) InternalError(err,w,r,user)
@ -236,7 +241,8 @@ func route_create_topic(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
} else { } else {
http.Redirect(w, r, "/topic/" + strconv.FormatInt(lastId, 10), http.StatusSeeOther) http.Redirect(w, r, "/topic/" + strconv.FormatInt(lastId, 10), http.StatusSeeOther)
} }
@ -268,7 +274,8 @@ func route_create_reply(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }
@ -285,7 +292,8 @@ func route_create_reply(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
} else { } else {
http.Redirect(w, r, "/topic/" + strconv.Itoa(tid), http.StatusSeeOther) http.Redirect(w, r, "/topic/" + strconv.Itoa(tid), http.StatusSeeOther)
} }
@ -519,7 +527,8 @@ func route_account_own_edit_critical(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }
@ -536,7 +545,8 @@ func route_account_own_edit_critical_submit(w http.ResponseWriter, r *http.Reque
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }
@ -559,7 +569,8 @@ func route_account_own_edit_critical_submit(w http.ResponseWriter, r *http.Reque
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} else if err != nil { } else if err != nil {
InternalError(err,w,r,user) InternalError(err,w,r,user)
@ -574,7 +585,8 @@ func route_account_own_edit_critical_submit(w http.ResponseWriter, r *http.Reque
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} else if err != nil { } else if err != nil {
InternalError(err,w,r,user) InternalError(err,w,r,user)
@ -586,7 +598,8 @@ func route_account_own_edit_critical_submit(w http.ResponseWriter, r *http.Reque
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }
SetPassword(user.ID, new_password) SetPassword(user.ID, new_password)
@ -611,7 +624,8 @@ func route_account_own_edit_avatar(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }
@ -634,7 +648,8 @@ func route_account_own_edit_avatar_submit(w http.ResponseWriter, r *http.Request
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }
@ -719,7 +734,8 @@ func route_account_own_edit_username(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }
@ -736,7 +752,8 @@ func route_account_own_edit_username_submit(w http.ResponseWriter, r *http.Reque
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }
@ -767,7 +784,8 @@ func route_logout(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }
@ -788,7 +806,8 @@ func route_login(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }
@ -805,7 +824,8 @@ func route_login_submit(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }
@ -832,7 +852,8 @@ func route_login_submit(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} else if err != nil { } else if err != nil {
InternalError(err,w,r,user) InternalError(err,w,r,user)
@ -848,7 +869,8 @@ func route_login_submit(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }
@ -873,7 +895,8 @@ func route_login_submit(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} else if err != nil { } else if err != nil {
InternalError(err,w,r,user) InternalError(err,w,r,user)
@ -911,7 +934,8 @@ func route_register(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }
@ -940,7 +964,8 @@ func route_register_submit(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }
@ -956,7 +981,8 @@ func route_register_submit(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer var b bytes.Buffer
templates.ExecuteTemplate(&b,"error.html", pi) templates.ExecuteTemplate(&b,"error.html", pi)
errpage := b.String() errpage := b.String()
http.Error(w,errpage,500) w.WriteHeader(500)
fmt.Fprintln(w,errpage)
return return
} }