-
Notifications
You must be signed in to change notification settings - Fork 402
/
common.go
37 lines (30 loc) · 873 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
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package admin
import (
"encoding/json"
"net/http"
"github.com/zeebo/errs"
)
// Error is default error class for admin package.
var Error = errs.Class("admin")
func sendJSONError(w http.ResponseWriter, errMsg, detail string, statusCode int) {
errStr := struct {
Error string `json:"error"`
Detail string `json:"detail"`
}{
Error: errMsg,
Detail: detail,
}
body, err := json.Marshal(errStr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
sendJSONData(w, statusCode, body)
}
func sendJSONData(w http.ResponseWriter, statusCode int, data []byte) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
_, _ = w.Write(data) // any error here entitles a client side disconnect or similar, which we do not care about.
}