-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathwrite.go
77 lines (68 loc) · 1.93 KB
/
write.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package util
import (
"bytes"
"fmt"
"html/template"
"net/http"
"v.io/x/ref/internal/logger"
)
// HTTPBadRequest sends an HTTP 400 error on 'w' and renders a pretty page.
// If err is not nil, it also renders the string representation of err in the response page.
func HTTPBadRequest(w http.ResponseWriter, req *http.Request, err error) {
w.WriteHeader(http.StatusBadRequest)
if e := tmplBadRequest.Execute(w, badRequestData{Request: requestString(req), Error: err}); e != nil {
logger.Global().Errorf("Failed to execute Bad Request Template: %v", e)
}
}
// ServerError sends an HTTP 500 error on 'w' and renders a pretty page that
// also has the string representation of err.
func HTTPServerError(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusInternalServerError)
logger.Global().Errorf("HTTPServerError: %v", err)
if e := tmplServerError.Execute(w, fmt.Errorf("unexpected server error")); e != nil {
logger.Global().Errorf("Failed to execute Server Error template: %v", e)
}
}
var (
tmplBadRequest = template.Must(template.New("Bad Request").Parse(`<!doctype html>
<html>
<head>
<meta charset="UTF8">
<title>Bad Request</title>
</head>
<body>
<h1>Bad Request</h1>
{{with $data := .}}
{{if $data.Error}}Error: {{$data.Error}}{{end}}
<pre>
{{$data.Request}}
</pre>
{{end}}
</body>
</html>`))
tmplServerError = template.Must(template.New("Server Error").Parse(`<!doctype html>
<html>
<head>
<meta charset="UTF8">
<title>Server Error</title>
</head>
<body>
<h1>Oops! Error at the server</h1>
Error: {{.}}
<br/>
Ask the server administrator to check the server logs
</body>
</html>`))
)
func requestString(r *http.Request) string {
var buf bytes.Buffer
r.Write(&buf) //nolint:errcheck
return buf.String()
}
type badRequestData struct {
Request string
Error error
}