Skip to content

Handle errorDetails and raise exception on failure instead of Str Stream #3321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion docker/api/image.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
from json import JSONDecodeError

from .. import auth, errors, utils
from ..constants import DEFAULT_DATA_CHUNK_SIZE
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions tests/unit/api_image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import docker
from docker import auth
import docker.errors

from . import fake_api
from .api_test import (
Expand Down Expand Up @@ -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)

Expand Down
Loading