-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
46 lines (38 loc) · 1.02 KB
/
http.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
package routeinjector
import (
"fmt"
"net/http"
"strconv"
"time"
)
// MyError is an error implementation that includes a time and message.
type MyError struct {
When time.Time
What string
}
func (e MyError) Error() string {
return fmt.Sprintf("%v: %v", e.When, e.What)
}
//Oops is an example error handler
func Oops() MyError {
return MyError{
time.Date(1989, 3, 15, 22, 30, 0, 0, time.UTC),
"the file system has gone away",
}
}
// WriteError sends a Error as a Response
func WriteError(w http.ResponseWriter, status int, message string) {
// do the same as a regular response, but manually format as JSON
WriteJSON(w, status, fmt.Sprintf("{\"error\":\"%s\"}", message))
}
// WriteJSON sends a JSON String as a Response
func WriteJSON(w http.ResponseWriter, status int, message string) {
// write headers
header := w.Header()
header.Add("Content-Length", strconv.Itoa(len(message)))
header.Add("Content-Type", "application/json")
// write status code
w.WriteHeader(status)
// write data
w.Write([]byte(message))
}