diff --git a/api/server/router/container/container_routes.go b/api/server/router/container/container_routes.go index 6670b9ec68..345bf2cab1 100644 --- a/api/server/router/container/container_routes.go +++ b/api/server/router/container/container_routes.go @@ -44,7 +44,7 @@ func (s *containerRouter) postCommit(ctx context.Context, w http.ResponseWriter, } config, _, _, err := s.decoder.DecodeConfig(r.Body) - if err != nil && err != io.EOF { // Do not fail if body is empty. + if err != nil && !errors.Is(err, io.EOF) { // Do not fail if body is empty. return err } @@ -484,6 +484,9 @@ func (s *containerRouter) postContainersCreate(ctx context.Context, w http.Respo config, hostConfig, networkingConfig, err := s.decoder.DecodeConfig(r.Body) if err != nil { + if errors.Is(err, io.EOF) { + return errdefs.InvalidParameter(errors.New("invalid JSON: got EOF while reading request body")) + } return err } version := httputils.VersionFromContext(ctx) diff --git a/runconfig/config.go b/runconfig/config.go index b25d1a8aa3..3ba1609e91 100644 --- a/runconfig/config.go +++ b/runconfig/config.go @@ -77,10 +77,7 @@ func decodeContainerConfig(src io.Reader, si *sysinfo.SysInfo) (*container.Confi func loadJSON(src io.Reader, out interface{}) error { dec := json.NewDecoder(src) if err := dec.Decode(&out); err != nil { - if err == io.EOF { - return validationError("invalid JSON: got EOF while reading request body") - } - return validationError("invalid JSON: " + err.Error()) + return invalidJSONError{Err: err} } if dec.More() { return validationError("unexpected content after JSON") diff --git a/runconfig/errors.go b/runconfig/errors.go index 038fe39660..9522a2e0f7 100644 --- a/runconfig/errors.go +++ b/runconfig/errors.go @@ -40,3 +40,17 @@ func (e validationError) Error() string { } func (e validationError) InvalidParameter() {} + +type invalidJSONError struct { + Err error +} + +func (e invalidJSONError) Error() string { + return "invalid JSON: " + e.Err.Error() +} + +func (e invalidJSONError) Unwrap() error { + return e.Err +} + +func (e invalidJSONError) InvalidParameter() {}