From 07608fc3fb94981785bd36d5f490293740482bdb Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 5 May 2023 13:53:19 -0500 Subject: [PATCH] chore: fix proxy 404 page (#7421) * chore: fix proxy 404 page --------- Co-authored-by: Kyle Carberry --- enterprise/wsproxy/wsproxy.go | 8 +++++--- site/site.go | 4 +++- site/site_test.go | 31 +++++++++++++++++++++++++++++++ site/static/error.html | 10 ++++++++-- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/enterprise/wsproxy/wsproxy.go b/enterprise/wsproxy/wsproxy.go index 508167550d..f617f00c05 100644 --- a/enterprise/wsproxy/wsproxy.go +++ b/enterprise/wsproxy/wsproxy.go @@ -235,9 +235,11 @@ func New(ctx context.Context, opts *Options) (*Server, error) { r.Get("/healthz-report", s.healthReport) r.NotFound(func(rw http.ResponseWriter, r *http.Request) { site.RenderStaticErrorPage(rw, r, site.ErrorPageData{ - Status: 404, - Title: "Route Not Found", - Description: "The route you requested does not exist on this workspace proxy. Maybe you intended to make this request to the primary dashboard? Click below to be redirected to the primary site.", + Title: "Head to the Dashboard", + Status: http.StatusBadRequest, + HideStatus: true, + Description: "Workspace Proxies route traffic in terminals and apps directly to your workspace. " + + "This page must be loaded from the dashboard. Click to be redirected!", RetryEnabled: false, DashboardURL: opts.DashboardURL.String(), }) diff --git a/site/site.go b/site/site.go index 28f0880c3c..e047ab21cb 100644 --- a/site/site.go +++ b/site/site.go @@ -606,7 +606,9 @@ func extractBin(dest string, r io.Reader) (numExtracted int, err error) { // ErrorPageData contains the variables that are found in // site/static/error.html. type ErrorPageData struct { - Status int + Status int + // HideStatus will remove the status code from the page. + HideStatus bool Title string Description string RetryEnabled bool diff --git a/site/site_test.go b/site/site_test.go index c7b301982f..f06ce1f991 100644 --- a/site/site_test.go +++ b/site/site_test.go @@ -517,3 +517,34 @@ func TestRenderStaticErrorPage(t *testing.T) { require.Contains(t, bodyStr, "Retry") require.Contains(t, bodyStr, d.DashboardURL) } + +func TestRenderStaticErrorPageNoStatus(t *testing.T) { + t.Parallel() + + d := site.ErrorPageData{ + HideStatus: true, + Status: http.StatusBadGateway, + Title: "Bad Gateway 1234", + Description: "shout out colin", + RetryEnabled: true, + DashboardURL: "https://example.com", + } + + rw := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/", nil) + site.RenderStaticErrorPage(rw, r, d) + + resp := rw.Result() + defer resp.Body.Close() + require.Equal(t, d.Status, resp.StatusCode) + require.Contains(t, resp.Header.Get("Content-Type"), "text/html") + + body, err := io.ReadAll(resp.Body) + require.NoError(t, err) + bodyStr := string(body) + require.NotContains(t, bodyStr, strconv.Itoa(d.Status)) + require.Contains(t, bodyStr, d.Title) + require.Contains(t, bodyStr, d.Description) + require.Contains(t, bodyStr, "Retry") + require.Contains(t, bodyStr, d.DashboardURL) +} diff --git a/site/static/error.html b/site/static/error.html index f587a63f46..93b348d455 100644 --- a/site/static/error.html +++ b/site/static/error.html @@ -7,7 +7,10 @@ running). */}} - {{ .Error.Status }} - {{ .Error.Title }} + + {{- if not .Error.HideStatus }}{{ .Error.Status }} - {{end}}{{ + .Error.Title }} +