-
Notifications
You must be signed in to change notification settings - Fork 84
/
common.go
62 lines (52 loc) · 1000 Bytes
/
common.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
package common
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
type closer interface {
Close()
}
type urler interface {
URL() string
}
type FakeServer interface {
closer
urler
}
func writeError(w http.ResponseWriter, status int, err error) {
w.WriteHeader(status)
_, werr := w.Write([]byte(fmt.Sprintf(`{"description": %q`, err.Error())))
if werr != nil {
panic(werr)
}
}
func writeGQLError(w http.ResponseWriter, errMsg string) {
errJson := struct {
Errors []struct {
Message string
}
}{
Errors: make([]struct{ Message string }, 1),
}
errJson.Errors[0] = struct{ Message string }{
Message: errMsg,
}
if err := json.NewEncoder(w).Encode(errJson); err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
}
func getFileContent(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
bytes, err := ioutil.ReadAll(file)
if err != nil {
return "", err
}
return string(bytes), nil
}