-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhandling_yaml.go
47 lines (40 loc) · 1.39 KB
/
handling_yaml.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
package gimlet
import (
"fmt"
"net/http"
"github.com/mongodb/grip"
yaml "gopkg.in/yaml.v2"
)
// WriteYAMLResponse writes a YAML document to the body of an HTTP
// request, setting the return status of to 500 if the YAML
// seralization process encounters an error, otherwise return
func WriteYAMLResponse(w http.ResponseWriter, code int, data interface{}) {
defer func() {
if msg := recover(); msg != nil {
m := fmt.Sprintf("parsing YAML message: %v", msg)
grip.Debug(m)
http.Error(w, m, http.StatusInternalServerError)
}
}()
// ignoring the error because the yaml library always panics
out, _ := yaml.Marshal(data)
writeResponse(YAML, w, code, out)
}
// WriteYAML is a helper method to write YAML data to the body of an
// HTTP request and return 200 (successful.)
func WriteYAML(w http.ResponseWriter, data interface{}) {
// 200
WriteYAMLResponse(w, http.StatusOK, data)
}
// WriteYAMLError is a helper method to write YAML data to the body of
// an HTTP request and return 400 (user error.)
func WriteYAMLError(w http.ResponseWriter, data interface{}) {
// 400
WriteYAMLResponse(w, http.StatusBadRequest, data)
}
// WriteYAMLInternalError is a helper method to write YAML data to the
// body of an HTTP request and return 500 (internal error.)
func WriteYAMLInternalError(w http.ResponseWriter, data interface{}) {
// 500
WriteYAMLResponse(w, http.StatusInternalServerError, data)
}