-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
50 lines (40 loc) · 1.07 KB
/
error.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
// Copyright (c) Bluebeam Inc. All rights reserved.
//
// Licensed under the MIT License. See LICENSE in the project root for license information.
package main
import (
"errors"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"net/url"
)
func errorPage(w http.ResponseWriter, r *http.Request) {
html, err := Asset("assets/error.html")
if err != nil {
redirectToError(w, r, err)
return
}
t, _ := template.New("error").Parse(string(html))
errorData := struct {
Description string
}{}
desc, _ := url.QueryUnescape(r.URL.Query().Get("description"))
errorData.Description = desc
t.Execute(w, errorData)
}
func redirectToError(w http.ResponseWriter, r *http.Request, err error) {
fmt.Println(err)
http.Redirect(w, r, "/error?description="+url.QueryEscape(err.Error()), http.StatusFound)
}
func checkHTTPResponse(resp *http.Response) (bool, error) {
if resp.StatusCode >= http.StatusBadRequest {
errBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}
return false, errors.New(resp.Status + " " + string(errBytes))
}
return true, nil
}