Skip to content

Commit 7989121

Browse files
authored
Merge pull request #148 from Kinto/add-batch-debug-info
Add Batch debug information.
2 parents 8fc639e + 3903b4b commit 7989121

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

CHANGELOG.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@ CHANGELOG
44
This document describes changes between each past release.
55

66

7-
8.1.0 (unreleased)
7+
9.0.0 (unreleased)
88
==================
99

10+
**Breaking changes**
11+
12+
- The client will fail a batch only when a 5XX error occurs (#148)
13+
14+
**New Features**
15+
16+
- Log all the batch responses (#148)
17+
- Log the request and the batch responses in debug (#148)
1018
- Allow reading responses from batch requests with the ``results()`` method. (#146)
1119

1220

kinto_http/batch.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
from . import utils
1+
import json
2+
import logging
23
from collections import defaultdict
34

45
from kinto_http.exceptions import KintoException
56

7+
from . import utils
8+
9+
logger = logging.getLogger(__name__)
10+
611

712
class BatchSession(object):
813

@@ -46,6 +51,7 @@ def _build_requests(self):
4651
def send(self):
4752
self._results = []
4853
requests = self._build_requests()
54+
id_request = 0
4955
for chunk in utils.chunks(requests, self.batch_max_requests):
5056
kwargs = dict(method='POST',
5157
endpoint=self.endpoints.get('batch'),
@@ -58,8 +64,25 @@ def send(self):
5864
exception = KintoException(message)
5965
exception.request = chunk[i]
6066
exception.response = response
67+
68+
level = logging.WARN if status_code < 400 else logging.ERROR
69+
message = response["body"].get("message", "")
70+
logger.log(level, "Batch #{}: {} {} - {} {}".format(
71+
id_request, chunk[i]["method"], chunk[i]["path"],
72+
status_code, message))
73+
74+
# Full log in DEBUG mode
75+
logger.debug("\nBatch #{}: \n\tRequest: {}\n\tResponse: {}\n".format(
76+
id_request, json.dumps(chunk[i]), json.dumps(response)))
77+
78+
# Raise in case of a 500
79+
if status_code >= 500:
6180
raise exception
81+
82+
id_request += 1
83+
6284
self._results.append((resp, headers))
85+
6386
return self._results
6487

6588
def results(self):

kinto_http/tests/test_batch.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ def test_prefix_is_removed_from_batch_requests(self):
9696
_, kwargs1 = calls[0]
9797
assert kwargs1['payload']['requests'][0]['path'] == '/foobar'
9898

99-
def test_batch_raises_exception_as_soon_as_subrequest_fails(self):
99+
def test_batch_raises_exception_as_soon_as_subrequest_fails_with_status_code_5xx(self):
100100
self.client.session.request.side_effect = [
101101
({"responses": [
102-
{"status": 404, "path": "/url2", "body": {}, "headers": {}}
102+
{"status": 502, "path": "/url2", "body": {"message": "Host not reachable"},
103+
"headers": {}}
103104
]}, mock.sentinel.headers),
104105
({"responses": [
105106
{"status": 200, "path": "/url1", "body": {}, "headers": {}},

kinto_http/tests/test_client.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,43 @@ def test_batch_raises_exception(self):
5151
with self.client.batch(bucket='moz', collection='test') as batch:
5252
batch.create_record(id=1234, data={'foo': 'bar'})
5353

54-
def test_batch_raises_exception_if_subrequest_failed(self):
54+
def test_batch_raises_exception_if_subrequest_failed_with_code_5xx(self):
5555
error = {
5656
"errno": 121,
5757
"message": "This user cannot access this resource.",
58-
"code": 403,
59-
"error": "Forbidden"
58+
"code": 500,
59+
"error": "Server Internal Error"
6060
}
6161
self.session.request.side_effect = [
6262
({"settings": {"batch_max_requests": 25}}, []),
6363
({"responses": [
6464
{"status": 200, "path": "/url1", "body": {}, "headers": {}},
65-
{"status": 404, "path": "/url2", "body": error, "headers": {}}
65+
{"status": 500, "path": "/url2", "body": error, "headers": {}}
6666
]}, [])]
6767

6868
with self.assertRaises(KintoException):
6969
with self.client.batch(bucket='moz', collection='test') as batch:
7070
batch.create_record(id=1234, data={'foo': 'bar'})
7171
batch.create_record(id=5678, data={'tutu': 'toto'})
7272

73+
def test_batch_dont_raise_exception_if_subrequest_failed_with_code_4xx(self):
74+
error = {
75+
"errno": 121,
76+
"message": "Forbidden",
77+
"code": 403,
78+
"error": "This user cannot access this resource."
79+
}
80+
self.session.request.side_effect = [
81+
({"settings": {"batch_max_requests": 25}}, []),
82+
({"responses": [
83+
{"status": 200, "path": "/url1", "body": {}, "headers": {}},
84+
{"status": 403, "path": "/url2", "body": error, "headers": {}}
85+
]}, [])]
86+
87+
with self.client.batch(bucket='moz', collection='test') as batch: # Do not raise
88+
batch.create_record(id=1234, data={'foo': 'bar'})
89+
batch.create_record(id=5678, data={'tutu': 'toto'})
90+
7391
def test_batch_options_are_transmitted(self):
7492
settings = {"batch_max_requests": 25}
7593
self.session.request.side_effect = [({"settings": settings}, [])]

0 commit comments

Comments
 (0)