2019-06-06 13:06:31 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type errorResponse struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
Status int `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonError(err error, status int) []byte {
|
|
|
|
r := errorResponse{err.Error(), status}
|
|
|
|
b, _ := json.Marshal(r)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2019-07-06 16:05:27 +02:00
|
|
|
// RespondWithJSONError writes the given error to the given response writer
|
|
|
|
// while wrapping it into a JSON payload.
|
2019-06-06 13:06:31 +02:00
|
|
|
func RespondWithJSONError(w http.ResponseWriter, err error, status int) {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Write(jsonError(err, status))
|
|
|
|
}
|