Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: milo/appengine/settings/themes.go

Issue 2525493002: Milo: Add themed page for errors (Closed)
Patch Set: Review Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « milo/appengine/frontend/templates/buildbot/pages/error.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package settings 5 package settings
6 6
7 import ( 7 import (
8 "fmt" 8 "fmt"
9 "io" 9 "io"
10 "net/http" 10 "net/http"
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 // passed to all templates. 76 // passed to all templates.
77 func GetTemplateBundles() []NamedBundle { 77 func GetTemplateBundles() []NamedBundle {
78 result := []NamedBundle{} 78 result := []NamedBundle{}
79 for name, t := range Themes { 79 for name, t := range Themes {
80 if t.IsTemplate { 80 if t.IsTemplate {
81 templateBundle := &templates.Bundle{ 81 templateBundle := &templates.Bundle{
82 Loader: templates.FileSystemLoader(path .Join("templates", name)), 82 Loader: templates.FileSystemLoader(path .Join("templates", name)),
83 DefaultTemplate: name, 83 DefaultTemplate: name,
84 DebugMode: info.IsDevAppServer, 84 DebugMode: info.IsDevAppServer,
85 DefaultArgs: func(c context.Context) (templates. Args, error) { 85 DefaultArgs: func(c context.Context) (templates. Args, error) {
86 » » » » » loginURL, err := auth.LoginURL(c, "/") 86 » » » » » r := getRequest(c)
87 » » » » » path := r.URL.Path
88 » » » » » loginURL, err := auth.LoginURL(c, path)
87 if err != nil { 89 if err != nil {
88 return nil, err 90 return nil, err
89 } 91 }
90 » » » » » logoutURL, err := auth.LogoutURL(c, "/") 92 » » » » » logoutURL, err := auth.LogoutURL(c, path )
91 if err != nil { 93 if err != nil {
92 return nil, err 94 return nil, err
93 } 95 }
94 if err != nil { 96 if err != nil {
95 return nil, err 97 return nil, err
96 } 98 }
97 return templates.Args{ 99 return templates.Args{
98 "AppVersion": strings.Split(inf o.VersionID(c), ".")[0], 100 "AppVersion": strings.Split(inf o.VersionID(c), ".")[0],
99 "IsAnonymous": auth.CurrentIdent ity(c) == "anonymous:anonymous", 101 "IsAnonymous": auth.CurrentIdent ity(c) == "anonymous:anonymous",
100 "User": auth.CurrentUser( c), 102 "User": auth.CurrentUser( c),
101 "LoginURL": loginURL, 103 "LoginURL": loginURL,
102 "LogoutURL": logoutURL, 104 "LogoutURL": logoutURL,
103 "CurrentTime": clock.Now(c), 105 "CurrentTime": clock.Now(c),
104 "Analytics": analytics.Snippet (c), 106 "Analytics": analytics.Snippet (c),
107 "RequestID": info.RequestID(c) ,
105 }, nil 108 }, nil
106 }, 109 },
107 FuncMap: funcMap, 110 FuncMap: funcMap,
108 } 111 }
109 result = append(result, NamedBundle{name, templateBundle , &t}) 112 result = append(result, NamedBundle{name, templateBundle , &t})
110 } 113 }
111 } 114 }
112 return result 115 return result
113 } 116 }
114 117
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 // Wrap adapts a ThemedHandler into a router.Handler. Of note, the 168 // Wrap adapts a ThemedHandler into a router.Handler. Of note, the
166 // Render functions' interface into rendering is purely through a single 169 // Render functions' interface into rendering is purely through a single
167 // templates.Args value which gets rendered here, while the http.ResponseWriter 170 // templates.Args value which gets rendered here, while the http.ResponseWriter
168 // is stripped out. 171 // is stripped out.
169 func Wrap(h ThemedHandler) router.Handler { 172 func Wrap(h ThemedHandler) router.Handler {
170 return func(c *router.Context) { 173 return func(c *router.Context) {
171 // Figure out if we need to do the things. 174 // Figure out if we need to do the things.
172 theme := GetTheme(c.Context, c.Request) 175 theme := GetTheme(c.Context, c.Request)
173 template := h.GetTemplateName(theme) 176 template := h.GetTemplateName(theme)
174 177
178 // Inject the request into the context.
179 c.Context = WithRequest(c.Context, c.Request)
180
175 // Do the things. 181 // Do the things.
176 args, err := h.Render(c.Context, c.Request, c.Params) 182 args, err := h.Render(c.Context, c.Request, c.Params)
177 183
178 // Throw errors. 184 // Throw errors.
179 // TODO(hinoka): Add themes and templates for errors so they loo k better. 185 // TODO(hinoka): Add themes and templates for errors so they loo k better.
180 if err != nil { 186 if err != nil {
181 » » » if merr, ok := err.(*miloerror.Error); ok { 187 » » » msg := err.Error()
182 » » » » http.Error(c.Writer, merr.Message, merr.Code) 188 » » » code := http.StatusInternalServerError
183 » » » } else { 189 » » » if merr, ok := err.(miloerror.Error); ok {
184 » » » » http.Error(c.Writer, err.Error(), http.StatusInt ernalServerError) 190 » » » » msg = merr.Message
191 » » » » code = merr.Code
185 } 192 }
193 c.Writer.WriteHeader(code)
194 name := "pages/error.html"
195 themedMustRender(c.Context, c.Writer, theme.Name, name, templates.Args{
196 "Code": code,
197 "Message": msg,
198 })
186 return 199 return
187 } 200 }
188 201
189 // Render the stuff. 202 // Render the stuff.
190 name := fmt.Sprintf("pages/%s", template) 203 name := fmt.Sprintf("pages/%s", template)
191 themedMustRender(c.Context, c.Writer, theme.Name, name, *args) 204 themedMustRender(c.Context, c.Writer, theme.Name, name, *args)
192 } 205 }
193 } 206 }
207
208 // The context key
209 var requestKey = "http.request"
210
211 func WithRequest(c context.Context, r *http.Request) context.Context {
212 return context.WithValue(c, &requestKey, r)
213 }
214
215 func getRequest(c context.Context) *http.Request {
216 if req, ok := c.Value(&requestKey).(*http.Request); ok {
217 return req
218 }
219 panic("No http.request found in context")
220 }
OLDNEW
« no previous file with comments | « milo/appengine/frontend/templates/buildbot/pages/error.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698