2020-02-07 16:36:28 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package admin
|
|
|
|
|
2020-08-05 14:13:11 +01:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
)
|
2020-02-07 16:36:28 +00:00
|
|
|
|
|
|
|
// Error is default error class for admin package.
|
|
|
|
var Error = errs.Class("admin")
|
2020-08-05 14:13:11 +01:00
|
|
|
|
|
|
|
func httpJSONError(w http.ResponseWriter, error, detail string, statusCode int) {
|
|
|
|
errStr := struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
Detail string `json:"detail"`
|
|
|
|
}{
|
|
|
|
Error: error,
|
|
|
|
Detail: detail,
|
|
|
|
}
|
|
|
|
byt, err := json.Marshal(errStr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
_, _ = w.Write(byt) // any error here entitles a client side disconnect or similar, which we do not care about.
|
|
|
|
}
|