forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
errorpage.go
112 lines (91 loc) · 3.24 KB
/
errorpage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package errorpage
import (
"bytes"
"fmt"
"html/template"
"net/http"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"github.com/golang/glog"
"github.com/openshift/origin/pkg/util/httprequest"
)
// ErrorPage implements auth and grant error handling by rendering an error page for browser-like clients
type ErrorPage struct {
render ErrorPageRenderer
}
// NewErrorPageHandler returns an auth and grant error handler using the given renderer
func NewErrorPageHandler(renderer ErrorPageRenderer) *ErrorPage {
return &ErrorPage{render: renderer}
}
func (p *ErrorPage) AuthenticationError(err error, w http.ResponseWriter, req *http.Request) (bool, error) {
glog.Errorf("AuthenticationError: %v", err)
// Only render html error pages for browser-like things
if !httprequest.PrefersHTML(req) {
return false, err
}
errorData := ErrorData{}
errorData.ErrorCode = AuthenticationErrorCode(err)
errorData.Error = AuthenticationErrorMessage(errorData.ErrorCode)
p.render.Render(errorData, w, req)
return true, nil
}
func (p *ErrorPage) GrantError(err error, w http.ResponseWriter, req *http.Request) (bool, error) {
glog.Errorf("GrantError: %v", err)
// Only render html error pages for browser-like things
if !httprequest.PrefersHTML(req) {
return false, err
}
errorData := ErrorData{}
errorData.ErrorCode = GrantErrorCode(err)
errorData.Error = GrantErrorMessage(errorData.ErrorCode)
p.render.Render(errorData, w, req)
return true, nil
}
// ErrorData holds fields for the error page renderer
type ErrorData struct {
Error string
ErrorCode string
}
// ErrorPageRenderer handles rendering a given error code/message
type ErrorPageRenderer interface {
Render(data ErrorData, w http.ResponseWriter, req *http.Request)
}
// errorPageTemplateRenderer renders a golang template for requests which indicate they can accept HTML
type errorPageTemplateRenderer struct {
errorPageTemplate *template.Template
}
// NewErrorPageRenderer creates an error page renderer that takes in an optional custom template to
// allow branding of the page. Uses the default if templateFile is not set.
func NewErrorPageTemplateRenderer(templateFile string) (ErrorPageRenderer, error) {
r := &errorPageTemplateRenderer{}
if len(templateFile) > 0 {
customTemplate, err := template.ParseFiles(templateFile)
if err != nil {
return nil, err
}
r.errorPageTemplate = customTemplate
} else {
r.errorPageTemplate = defaultErrorPageTemplate
}
return r, nil
}
func (r *errorPageTemplateRenderer) Render(data ErrorData, w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", "text/html; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := r.errorPageTemplate.Execute(w, data); err != nil {
utilruntime.HandleError(fmt.Errorf("unable to render error page template: %v", err))
}
}
// ValidateErrorPageTemplate ensures the given template does not error when rendered with an ErrorData object as input
func ValidateErrorPageTemplate(templateContent []byte) []error {
var allErrs []error
template, err := template.New("errorPageTemplateTest").Parse(string(templateContent))
if err != nil {
return append(allErrs, err)
}
var buffer bytes.Buffer
err = template.Execute(&buffer, ErrorData{})
if err != nil {
return append(allErrs, err)
}
return allErrs
}