Skip to content

Commit 97759b4

Browse files
authored
Catch ConnectionError when Docker isn't running (#66)
1 parent d1d209f commit 97759b4

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

python/rpdk/python/codegen.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import docker
99
from docker.errors import APIError, ContainerError, ImageLoadError
10+
from requests.exceptions import ConnectionError as RequestsConnectionError
1011
from rpdk.core.data_loaders import resource_stream
1112
from rpdk.core.exceptions import DownstreamError, SysExitRecommendedError
1213
from rpdk.core.init import input_with_validation
@@ -249,6 +250,15 @@ def _docker_build(cls, external_path):
249250
volumes=volumes,
250251
stream=True,
251252
)
253+
except RequestsConnectionError as e:
254+
# it seems quite hard to reliably extract the cause from
255+
# ConnectionError. we replace it with a friendlier error message
256+
# and preserve the cause for debug traceback
257+
cause = RequestsConnectionError(
258+
"Could not connect to docker - is it running?"
259+
)
260+
cause.__cause__ = e
261+
raise DownstreamError("Error running docker build") from cause
252262
except (ContainerError, ImageLoadError, APIError) as e:
253263
raise DownstreamError("Error running docker build") from e
254264
LOG.debug("Build running. Output:")

tests/plugin/codegen_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pytest
1010

1111
from docker.errors import APIError, ContainerError, ImageLoadError
12+
from requests.exceptions import ConnectionError as RequestsConnectionError
1213
from rpdk.core.exceptions import DownstreamError
1314
from rpdk.core.project import Project
1415
from rpdk.python.codegen import (
@@ -252,6 +253,9 @@ def test__docker_build_good_path(plugin, tmp_path):
252253
lambda: ContainerError("abcde", 255, "/bin/false", "image", ""),
253254
ImageLoadError,
254255
lambda: APIError("500"),
256+
lambda: RequestsConnectionError(
257+
"Connection aborted.", ConnectionRefusedError(61, "Connection refused")
258+
),
255259
],
256260
)
257261
def test__docker_build_bad_path(plugin, tmp_path, exception):

0 commit comments

Comments
 (0)