From 7e9c7ce60a90f3628888f178c27642561643abaa Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Fri, 1 Apr 2016 11:48:19 -0400 Subject: Visiting invalid URLs and bad APIs causes redirect to error page --- api/api.go | 2 ++ api/context.go | 36 +++++++++++++++++------------------- 2 files changed, 19 insertions(+), 19 deletions(-) (limited to 'api') diff --git a/api/api.go b/api/api.go index 20f77e558..476047877 100644 --- a/api/api.go +++ b/api/api.go @@ -27,6 +27,8 @@ func InitApi() { InitWebhook(r) InitPreference(r) InitLicense(r) + // 404 on any api route before web.go has a chance to serve it + Srv.Router.Handle("/api/{anything:.*}", http.HandlerFunc(Handle404)) utils.InitHTML() } diff --git a/api/context.go b/api/context.go index eed035daf..0f7ba0fff 100644 --- a/api/context.go +++ b/api/context.go @@ -476,25 +476,23 @@ func IsPrivateIpAddress(ipAddress string) bool { } func RenderWebError(err *model.AppError, w http.ResponseWriter, r *http.Request) { - T, locale := utils.GetTranslationsAndLocale(w, r) - page := utils.NewHTMLTemplate("error", locale) - page.Props["Message"] = err.Message - page.Props["Details"] = err.DetailedError - - pathParts := strings.Split(r.URL.Path, "/") - if len(pathParts) > 1 { - page.Props["SiteURL"] = GetProtocol(r) + "://" + r.Host + "/" + pathParts[1] - } else { - page.Props["SiteURL"] = GetProtocol(r) + "://" + r.Host - } - - page.Props["Title"] = T("api.templates.error.title", map[string]interface{}{"SiteName": utils.ClientCfg["SiteName"]}) - page.Props["Link"] = T("api.templates.error.link") - - w.WriteHeader(err.StatusCode) - if rErr := page.RenderToWriter(w); rErr != nil { - l4g.Error("Failed to create error page: " + rErr.Error() + ", Original error: " + err.Error()) - } + T, _ := utils.GetTranslationsAndLocale(w, r) + + title := T("api.templates.error.title", map[string]interface{}{"SiteName": utils.ClientCfg["SiteName"]}) + message := err.Message + details := err.DetailedError + link := "/" + linkMessage := T("api.templates.error.link") + + http.Redirect( + w, + r, + "/error?title="+url.QueryEscape(title)+ + "&message="+url.QueryEscape(message)+ + "&details="+url.QueryEscape(details)+ + "&link="+url.QueryEscape(link)+ + "&linkmessage="+url.QueryEscape(linkMessage), + http.StatusTemporaryRedirect) } func Handle404(w http.ResponseWriter, r *http.Request) { -- cgit v1.2.3-1-g7c22