18896: review feedback, un-pyramid the logrus.Fields update.
[arvados.git] / sdk / go / httpserver / error.go
index 1ccf8c04782fbf57aedfe6cb20f75c50ef53cb9d..75ff85336fada402d3fab4dedece6f634f8aaed9 100644 (file)
@@ -6,18 +6,33 @@ package httpserver
 
 import (
        "encoding/json"
+       "fmt"
        "net/http"
 )
 
+func Errorf(status int, tmpl string, args ...interface{}) error {
+       return errorWithStatus{fmt.Errorf(tmpl, args...), status}
+}
+
+func ErrorWithStatus(err error, status int) error {
+       return errorWithStatus{err, status}
+}
+
+type errorWithStatus struct {
+       error
+       Status int
+}
+
+func (ews errorWithStatus) HTTPStatus() int {
+       return ews.Status
+}
+
 type ErrorResponse struct {
        Errors []string `json:"errors"`
 }
 
 func Error(w http.ResponseWriter, error string, code int) {
-       w.Header().Set("Content-Type", "application/json")
-       w.Header().Set("X-Content-Type-Options", "nosniff")
-       w.WriteHeader(code)
-       json.NewEncoder(w).Encode(ErrorResponse{Errors: []string{error}})
+       Errors(w, []string{error}, code)
 }
 
 func Errors(w http.ResponseWriter, errors []string, code int) {