# Buggy code
def wait_on_exit(container):
    exit_code = container.wait()
    return "%s exited with code %s\n" % (container.name, exit_code)


# Traceback (APIError)
Exception in thread Thread-9:
Traceback (most recent call last):
File "threading.py", line 810, in __bootstrap_inner
File "threading.py", line 763, in run
File "compose/cli/log_printer.py", line 149, in tail_container_logs
File "compose/cli/log_printer.py", line 179, in wait_on_exit
File "compose/container.py", line 239, in wait
File "site-packages/docker/utils/decorators.py", line 21, in wrapped
File "site-packages/docker/api/container.py", line 441, in wait
File "site-packages/docker/client.py", line 174, in _raise_for_status
APIError: 502 Server Error: Bad Gateway ("Bad response from Docker engine")

Exception in thread Thread-6:
Traceback (most recent call last):
File "threading.py", line 810, in __bootstrap_inner
File "threading.py", line 763, in run
File "compose/cli/log_printer.py", line 149, in tail_container_logs
File "compose/cli/log_printer.py", line 179, in wait_on_exit
File "compose/container.py", line 239, in wait
File "site-packages/docker/utils/decorators.py", line 21, in wrapped
File "site-packages/docker/api/container.py", line 441, in wait
File "site-packages/docker/client.py", line 174, in _raise_for_status
APIError: 502 Server Error: Bad Gateway ("Bad response from Docker engine")

Exception in thread Thread-10:
Traceback (most recent call last):
File "threading.py", line 810, in __bootstrap_inner
File "threading.py", line 763, in run
File "compose/cli/log_printer.py", line 149, in tail_container_logs
File "compose/cli/log_printer.py", line 179, in wait_on_exit
File "compose/container.py", line 239, in wait
File "site-packages/docker/utils/decorators.py", line 21, in wrapped
File "site-packages/docker/api/container.py", line 441, in wait
File "site-packages/docker/client.py", line 174, in _raise_for_status
APIError: 502 Server Error: Bad Gateway ("Bad response from Docker engine")

Exception in thread Thread-8:
Traceback (most recent call last):
File "threading.py", line 810, in __bootstrap_inner
File "threading.py", line 763, in run
File "compose/cli/log_printer.py", line 149, in tail_container_logs
File "compose/cli/log_printer.py", line 179, in wait_on_exit
File "compose/container.py", line 239, in wait
File "site-packages/docker/utils/decorators.py", line 21, in wrapped
File "site-packages/docker/api/container.py", line 441, in wait
File "site-packages/docker/client.py", line 174, in _raise_for_status
APIError: 502 Server Error: Bad Gateway ("Bad response from Docker engine")

Exception in thread Thread-11:
Traceback (most recent call last):
File "threading.py", line 810, in __bootstrap_inner
File "threading.py", line 763, in run
File "compose/cli/log_printer.py", line 190, in watch_events
File "compose/project.py", line 356, in events
File "compose/container.py", line 42, in from_id
File "site-packages/docker/utils/decorators.py", line 21, in wrapped
File "site-packages/docker/api/container.py", line 182, in inspect_container
File "site-packages/docker/utils/decorators.py", line 47, in inner
File "site-packages/docker/client.py", line 140, in _get
File "site-packages/requests/sessions.py", line 477, in get
File "site-packages/requests/sessions.py", line 465, in request
File "site-packages/requests/sessions.py", line 573, in send
File "site-packages/requests/adapters.py", line 415, in send
ConnectionError: ('Connection aborted.', BadStatusLine("''",))

# Fixed code
def wait_on_exit(container):
    try:
        exit_code = container.wait()
        return "%s exited with code %s\n" % (container.name, exit_code)
    except APIError as e:
        return "Unexpected API error for %s (HTTP code %s)\nResponse body:\n%s\n" % (
            container.name,
            e.response.status_code,
            e.response.text or "[empty]",
        )