Skip to content

Commit 7feab6e

Browse files
authored
Merge pull request #43 from buildkite/ming/pie-3522
PIE-3522: print error message when API return non 2xx 3xx result
2 parents 491af4d + 424a0b9 commit 7feab6e

File tree

2 files changed

+24
-5
lines changed
  • src/buildkite_test_collector/collector
  • tests/buildkite_test_collector/collector

2 files changed

+24
-5
lines changed

src/buildkite_test_collector/collector/api.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from sys import stderr
66
import traceback
77
from requests import post, Response
8-
from requests.exceptions import InvalidHeader
8+
from requests.exceptions import InvalidHeader, HTTPError
99
from .payload import Payload
1010

1111

@@ -29,13 +29,15 @@ def submit(payload: Payload, batch_size=100) -> Optional[Response]:
2929
"Authorization": f"Token token=\"{token}\""
3030
},
3131
timeout=60)
32-
if response.status_code >= 300:
33-
return response
32+
response.raise_for_status()
33+
return response
3434
except InvalidHeader as error:
3535
print("Warning: Invalid `BUILDKITE_ANALYTICS_TOKEN` environment variable", file=stderr)
3636
print(error, file=stderr)
37+
except HTTPError as err:
38+
print("Warning: Failed to uploads test results to buildkite", file=stderr)
39+
print(err, file=stderr)
3740
except Exception: # pylint: disable=broad-except
3841
error_message = traceback.format_exc()
3942
print(error_message, file=stderr)
40-
41-
return response
43+
return None

tests/buildkite_test_collector/collector/test_api.py

+17
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ def test_submit_with_payload_returns_an_api_response(successful_test):
9393
assert len(json["errors"]) == 0
9494
assert json['queued'] == 1
9595

96+
@responses.activate
97+
def test_submit_with_bad_response(successful_test):
98+
responses.add(
99+
responses.POST,
100+
"https://analytics-api.buildkite.com/v1/uploads",
101+
json={'error': str(uuid4())},
102+
status=401)
103+
104+
with mock.patch.dict(os.environ, {"CI": "true", "BUILDKITE_ANALYTICS_TOKEN": str(uuid4())}):
105+
payload = Payload.init(detect_env())
106+
payload = Payload.started(payload)
107+
108+
payload = payload.push_test_data(successful_test)
109+
110+
result = submit(payload)
111+
112+
assert result is None
96113

97114
@responses.activate
98115
def test_submit_with_large_payload_batches_requests(successful_test, failed_test):

0 commit comments

Comments
 (0)