X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/95e5ccacf6c1193b313fa90a6d39baafa2ba67d8..c4c57180c9ee9f79a1d272710aa7b8747d4d0c38:/sdk/go/httpserver/error.go diff --git a/sdk/go/httpserver/error.go b/sdk/go/httpserver/error.go index 398e61fcd0..75ff85336f 100644 --- a/sdk/go/httpserver/error.go +++ b/sdk/go/httpserver/error.go @@ -6,16 +6,38 @@ 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) { + Errors(w, []string{error}, code) +} + +func Errors(w http.ResponseWriter, errors []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}}) + json.NewEncoder(w).Encode(ErrorResponse{Errors: errors}) }