@@ -26,14 +26,31 @@ func HTML(renderable RequestRenderable) http.Handler {
2626 return HTTPHandlerFunc (func (r * http.Request ) (AsRenderable , http.Handler , error ) {
2727 v , next , err := renderable .RequestRenderable (r )
2828 if err != nil {
29- return nil , next , err
29+ return nil , nil , err
30+ } else if v == nil {
31+ return nil , next , nil
3032 }
3133
3234 return html {Body : v }, next , nil
3335 })
3436}
3537
38+ var errorViewTpl = MustParseTemplate ("errorView" , `Error: {{ . }}` )
39+
40+ type errorView struct {
41+ Error error
42+ }
43+
44+ func (v errorView ) Renderable (_ context.Context ) (Renderable , error ) {
45+ return View {Tpl : errorViewTpl , Data : v .Error }, nil
46+ }
47+
48+ func newErrorView (_ context.Context , err error ) (AsRenderable , error ) {
49+ return errorView {Error : err }, nil
50+ }
51+
3652func TestRequestRequestHandler (t * testing.T ) {
53+
3754 var statusCode = func (code int ) http.Handler {
3855 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
3956 w .WriteHeader (code )
@@ -51,19 +68,22 @@ func TestRequestRequestHandler(t *testing.T) {
5168 }
5269 })
5370
54- mux := http .NewServeMux ()
55-
56- mux .Handle ("/empty" , HTTPHandler (empty ))
57- mux .Handle ("/html/empty" , HTML (empty ))
58-
59- mux .Handle ("/person" , HTTPHandlerFunc (func (r * http.Request ) (AsRenderable , http.Handler , error ) {
71+ var person = RequestRenderableFunc (func (r * http.Request ) (AsRenderable , http.Handler , error ) {
6072 name := r .URL .Query ().Get ("name" )
6173 if name == "" {
6274 return nil , nil , fmt .Errorf ("missing name" )
6375 }
6476
6577 return PersonView (Person {Name : name }), nil , nil
66- }))
78+ })
79+
80+ mux := http .NewServeMux ()
81+
82+ mux .Handle ("/empty" , HTTPHandler (empty ))
83+ mux .Handle ("/html/empty" , HTML (empty ))
84+
85+ mux .Handle ("/person" , HTTPHandler (person , WithErrorHandlerFunc (newErrorView )))
86+ mux .Handle ("/html/person" , HTML (person ))
6787
6888 server := httptest .NewServer (mux )
6989 defer server .Close ()
@@ -116,6 +136,12 @@ func TestRequestRequestHandler(t *testing.T) {
116136 assert .Equal (t , 200 , code )
117137 })
118138
139+ t .Run ("person (name=)" , func (t * testing.T ) {
140+ body , code , _ := sendRequest (t , "/person?name=" )
141+ assert .Equal (t , 500 , code )
142+ assert .Equal (t , "Error: missing name" , body )
143+ })
144+
119145 t .Run ("person renders (name=someone)" , func (t * testing.T ) {
120146 body , code , _ := sendRequest (t , "/person?name=someone" )
121147 assert .Equal (t , "<div>Hi, someone.</div>" , body )
@@ -124,7 +150,19 @@ func TestRequestRequestHandler(t *testing.T) {
124150
125151 t .Run ("/html/empty" , func (t * testing.T ) {
126152 body , code , _ := sendRequest (t , "/html/empty" )
127- assert .Equal (t , "<html><body></body></html> " , body )
153+ assert .Equal (t , "" , body )
128154 assert .Equal (t , 200 , code )
129155 })
156+
157+ t .Run ("/html/person (name=Stan)" , func (t * testing.T ) {
158+ body , code , _ := sendRequest (t , "/html/person?name=Stan" )
159+ assert .Equal (t , "<html><body><div>Hi, Stan.</div></body></html>" , body )
160+ assert .Equal (t , 200 , code )
161+ })
162+
163+ t .Run ("/html/person (name=)" , func (t * testing.T ) {
164+ body , code , _ := sendRequest (t , "/html/person?name=" )
165+ assert .Equal (t , "Internal Server Error\n " , body )
166+ assert .Equal (t , 500 , code )
167+ })
130168}
0 commit comments