Skip to content

404 API Error is not handled properly #3151

@mndeveci

Description

@mndeveci

Hi there,

We recently upgraded dockerpy version to 6.2, and we found out an issue when we try to make a call to a non-existent API.

Before this upgrade, we used to get docker.errors.NotFound error with following details;

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    main()
  File "test.py", line 5, in main
    registry_data = docker_client.images.get_registry_data("docker/hello-world")
  File "/private/tmp/docker-test/.venv420/lib/python3.8/site-packages/docker/models/images.py", line 337, in get_registry_data
    attrs=self.client.api.inspect_distribution(name, auth_config),
  File "/private/tmp/docker-test/.venv420/lib/python3.8/site-packages/docker/utils/decorators.py", line 34, in wrapper
    return f(self, *args, **kwargs)
  File "/private/tmp/docker-test/.venv420/lib/python3.8/site-packages/docker/utils/decorators.py", line 19, in wrapped
    return f(self, resource_id, *args, **kwargs)
  File "/private/tmp/docker-test/.venv420/lib/python3.8/site-packages/docker/api/image.py", line 281, in inspect_distribution
    return self._result(
  File "/private/tmp/docker-test/.venv420/lib/python3.8/site-packages/docker/api/client.py", line 267, in _result
    self._raise_for_status(response)
  File "/private/tmp/docker-test/.venv420/lib/python3.8/site-packages/docker/api/client.py", line 263, in _raise_for_status
    raise create_api_error_from_http_exception(e)
  File "/private/tmp/docker-test/.venv420/lib/python3.8/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
    raise cls(e, response=response, explanation=explanation)
docker.errors.NotFound: 404 Client Error: Not Found ("b'Not Found'")

After upgrade, we are now getting this error Error: a bytes-like object is required, not 'str', which is raised during error handling/generation inside dockerpy library;

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    main()
  File "test.py", line 5, in main
    registry_data = docker_client.images.get_registry_data("docker/hello-world")
   File "/private/tmp/docker-test/.venv610/lib/python3.8/site-packages/docker/models/images.py", line 356, in get_registry_data
    attrs=self.client.api.inspect_distribution(name, auth_config),
  File "/private/tmp/docker-test/.venv610/lib/python3.8/site-packages/docker/utils/decorators.py", line 34, in wrapper
    return f(self, *args, **kwargs)
  File "/private/tmp/docker-test/.venv610/lib/python3.8/site-packages/docker/utils/decorators.py", line 19, in wrapped
    return f(self, resource_id, *args, **kwargs)
  File "/private/tmp/docker-test/.venv610/lib/python3.8/site-packages/docker/api/image.py", line 287, in inspect_distribution
    return self._result(
  File "/private/tmp/docker-test/.venv610/lib/python3.8/site-packages/docker/api/client.py", line 274, in _result
    self._raise_for_status(response)
  File "/private/tmp/docker-test/.venv610/lib/python3.8/site-packages/docker/api/client.py", line 270, in _raise_for_status
    raise create_api_error_from_http_exception(e) from e
  File "/private/tmp/docker-test/.venv610/lib/python3.8/site-packages/docker/errors.py", line 34, in create_api_error_from_http_exception
    if any(fragment in explanation_msg
  File "/private/tmp/docker-test/.venv610/lib/python3.8/site-packages/docker/errors.py", line 34, in <genexpr>
    if any(fragment in explanation_msg
Error: a bytes-like object is required, not 'str'

I think error originates from here https://github.com/docker/docker-py/blob/main/docker/errors.py#L30, since response.content was a string type before, now it is bytes. In order to fix the issue, you might need to change that line to response.text so that it will return a string.

Happy to raise a PR if you are willing to review it.

Thanks!

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions