package httperrors import ( "fmt" "net/http" ) type content struct { status int title string statusString string header string subHeader string } var ( content401 = content{ http.StatusUnauthorized, "Unauthorized (401)", "401", "You don't have permission to access the resource.", `

The resource that you are attempting to access is protected and you don't have the necessary permissions to view it.

`, } content404 = content{ http.StatusNotFound, "The page you're looking for could not be found (404)", "404", "The page you're looking for could not be found.", `

The resource that you are attempting to access does not exist or you don't have the necessary permissions to view it.

Make sure the address is correct and that the page hasn't moved.

Please contact your GitLab administrator if you think this is a mistake.

`, } content500 = content{ http.StatusInternalServerError, "Something went wrong (500)", "500", "Whoops, something went wrong on our end.", `

Try refreshing the page, or going back and attempting the action again.

Please contact your GitLab administrator if this problem persists.

`, } content502 = content{ http.StatusBadGateway, "Something went wrong (502)", "502", "Whoops, something went wrong on our end.", `

Try refreshing the page, or going back and attempting the action again.

Please contact your GitLab administrator if this problem persists.

`, } content503 = content{ http.StatusServiceUnavailable, "Service Unavailable (503)", "503", "Whoops, something went wrong on our end.", `

Try refreshing the page, or going back and attempting the action again.

Please contact your GitLab administrator if this problem persists.

`, } ) const predefinedErrorPage = ` %v GitLab Logo

%v

%v


%v Go back
` func generateErrorHTML(c content) string { return fmt.Sprintf(predefinedErrorPage, c.title, c.statusString, c.header, c.subHeader) } func serveErrorPage(w http.ResponseWriter, c content) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("X-Content-Type-Options", "nosniff") w.WriteHeader(c.status) fmt.Fprintln(w, generateErrorHTML(c)) } // Serve401 returns a 401 error response / HTML page to the http.ResponseWriter func Serve401(w http.ResponseWriter) { serveErrorPage(w, content401) } // Serve404 returns a 404 error response / HTML page to the http.ResponseWriter func Serve404(w http.ResponseWriter) { serveErrorPage(w, content404) } // Serve500 returns a 500 error response / HTML page to the http.ResponseWriter func Serve500(w http.ResponseWriter) { serveErrorPage(w, content500) } // Serve502 returns a 502 error response / HTML page to the http.ResponseWriter func Serve502(w http.ResponseWriter) { serveErrorPage(w, content502) } // Serve503 returns a 503 error response / HTML page to the http.ResponseWriter func Serve503(w http.ResponseWriter) { serveErrorPage(w, content503) }