diff --git a/docker/api/image.py b/docker/api/image.py index 85109473b..484a40736 100644 --- a/docker/api/image.py +++ b/docker/api/image.py @@ -1,5 +1,6 @@ import logging import os +from json import JSONDecodeError from .. import auth, errors, utils from ..constants import DEFAULT_DATA_CHUNK_SIZE @@ -495,7 +496,14 @@ def push(self, repository, tag=None, stream=False, auth_config=None, self._raise_for_status(response) if stream: - return self._stream_helper(response, decode=decode) + try: + for line in self._stream_helper(response, decode=decode): + if isinstance(line, dict) and "errorDetail" in line: + message = line["errorDetail"]["message"] + raise errors.APIError(message=message) + yield line + except JSONDecodeError as e: + raise JSONDecodeError("Error decoding response stream") from e return self._result(response) diff --git a/tests/unit/api_image_test.py b/tests/unit/api_image_test.py index 148109d37..4f2e2bdad 100644 --- a/tests/unit/api_image_test.py +++ b/tests/unit/api_image_test.py @@ -4,6 +4,7 @@ import docker from docker import auth +import docker.errors from . import fake_api from .api_test import ( @@ -288,6 +289,12 @@ def test_push_image_stream(self): timeout=DEFAULT_TIMEOUT_SECONDS ) + def test_push_image_with_exception(self): + with pytest.raises(Exception): + self.client.push("docker", stream=True, tag='tag', decode=True) + self.fail('An exception should have been raised') + + def test_tag_image(self): self.client.tag(fake_api.FAKE_IMAGE_ID, fake_api.FAKE_REPO_NAME)