# Buggy code
def log_api_error(e, client_version):
    if b"client is newer than server" not in e.explanation:
        log.error(e.explanation)
        return

    version = API_VERSION_TO_ENGINE_VERSION.get(client_version)
    if not version:
        # They've set a custom API version
        log.error(e.explanation)
        return

    log.error(
        "The Docker Engine version is less than the minimum required by "
        "Compose. Your current project requires a Docker Engine of "
        "version {version} or greater.".format(version=version)
    )


# Traceback (requests.exceptions.HTTPError)
$ docker-compose -f prod.yaml exec proxy sh
Traceback (most recent call last):
File "/home/yajo/.local/lib/python3.5/site-packages/docker/api/client.py", line 214, in _raise_for_status
response.raise_for_status()
File "/home/yajo/.local/lib/python3.5/site-packages/requests/models.py", line 862, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 409 Client Error: Conflict for url: http+docker://localunixsocket/v1.24/containers/6e4476fbc03e711b187ef37046d89125449f69a2fa7f741db06a0742ddfc9f51/exec

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/yajo/.local/lib/python3.5/site-packages/compose/cli/errors.py", line 44, in handle_connection_errors
yield
File "/home/yajo/.local/lib/python3.5/site-packages/compose/cli/main.py", line 116, in perform_command
handler(command, command_options)
File "/home/yajo/.local/lib/python3.5/site-packages/compose/cli/main.py", line 448, in exec_command
exec_id = container.create_exec(command, **create_exec_options)
File "/home/yajo/.local/lib/python3.5/site-packages/compose/container.py", line 220, in create_exec
return self.client.exec_create(self.id, command, **options)
File "/home/yajo/.local/lib/python3.5/site-packages/docker/utils/decorators.py", line 35, in wrapper
return f(self, *args, **kwargs)
File "/home/yajo/.local/lib/python3.5/site-packages/docker/utils/decorators.py", line 21, in wrapped
return f(self, resource_id, *args, **kwargs)
File "/home/yajo/.local/lib/python3.5/site-packages/docker/api/exec_api.py", line 58, in exec_create
return self._result(res, True)
File "/home/yajo/.local/lib/python3.5/site-packages/docker/api/client.py", line 220, in _result
self._raise_for_status(response)
File "/home/yajo/.local/lib/python3.5/site-packages/docker/api/client.py", line 216, in _raise_for_status
raise create_api_error_from_http_exception(e)
File "/home/yajo/.local/lib/python3.5/site-packages/docker/errors.py", line 30, in create_api_error_from_http_exception
raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 409 Client Error: Conflict for url: http+docker://localunixsocket/v1.24/containers/6e4476fbc03e711b187ef37046d89125449f69a2fa7f741db06a0742ddfc9f51/exec ("Container 6e4476fbc03e711b187ef37046d89125449f69a2fa7f741db06a0742ddfc9f51 is restarting, wait until the container is running")

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/yajo/.local/bin/docker-compose", line 11, in <module>
sys.exit(main())
File "/home/yajo/.local/lib/python3.5/site-packages/compose/cli/main.py", line 64, in main
command()
File "/home/yajo/.local/lib/python3.5/site-packages/compose/cli/main.py", line 116, in perform_command
handler(command, command_options)
File "/usr/lib64/python3.5/contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "/home/yajo/.local/lib/python3.5/site-packages/compose/cli/errors.py", line 54, in handle_connection_errors
log_api_error(e, client.api_version)
File "/home/yajo/.local/lib/python3.5/site-packages/compose/cli/errors.py", line 71, in log_api_error
if b'client is newer than server' not in e.explanation:
TypeError: 'in <string>' requires string as left operand, not bytes

# Fixed code
def log_api_error(e, client_version):
    explanation = e.explanation
    if isinstance(explanation, six.binary_type):
        explanation = explanation.decode("utf-8")

    if "client is newer than server" not in explanation:
        log.error(explanation)
        return

    version = API_VERSION_TO_ENGINE_VERSION.get(client_version)
    if not version:
        # They've set a custom API version
        log.error(explanation)
        return

    log.error(
        "The Docker Engine version is less than the minimum required by "
        "Compose. Your current project requires a Docker Engine of "
        "version {version} or greater.".format(version=version)
    )