fed09316b8
A previous commit added a helper function for sending JSON data back to the client. This commit makes use of it for homogenizing the current implementation. It also renames the existing helper message to send JSON errors to starts with "send" because the new helper starts with it and they helpers are clearer with their name starting with it. Change-Id: I53ee0b4ca33d677a8ccd366c9ba6d73f4f472247
38 lines
873 B
Go
38 lines
873 B
Go
// 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.
|
|
}
|