Skip to content

Commit 1334779

Browse files
authored
Feature/extra debug logging (#132)
* add test for utf-16 request body encoding * handle other charsets for json request body * add logging test for mimer errors * log invalid mime translations * changelog + version bump * add request body to mimer error logs * add some extra logging in dda.resources.resource * flake8
1 parent de3b2a0 commit 1334779

File tree

6 files changed

+36
-23
lines changed

6 files changed

+36
-23
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.31.0
2+
current_version = 0.31.1
33

44
[bumpversion:file:pyproject.toml]
55

CHANGELOG.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
# [0.31.0 = 2023-09-11]
7+
# [0.31.1 = 2023-09-11]
88
- [PR 134](https://github.com/salesforce/django-declarative-apis/pull/134) Make filter caching work for all datatypes
9-
10-
# [0.30.0] - 2023-08-23
119
- [PR 131](https://github.com/salesforce/django-declarative-apis/pull/131) Support non-utf8 request body
1210

1311
# [0.29.0] - 2023-08-16

django_declarative_apis/resources/resource.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ def anonymous(self):
148148

149149
return None
150150

151-
def authenticate(self, request, rm):
151+
def authenticate(self, request, rm): # noqa: C901
152152
actor, anonymous, error = False, True, ""
153153
# workaround for django header sillyness
154154
if "HTTP_AUTHORIZATION" in request.META:
155155
request.META["AUTHORIZATION"] = request.META["HTTP_AUTHORIZATION"]
156-
156+
logger.info("ev=dda_resource method=authenticate state=begin")
157157
# first we're going to try any authenticators that might match header hints. then, we'll try
158158
# any catch-all registered under None as a hint
159159
potential_authenticators = []
@@ -167,6 +167,7 @@ def authenticate(self, request, rm):
167167
potential_authenticators.extend(authenticators)
168168
continue
169169
except KeyError:
170+
logger.exception("ev=dda_resource method=authenticate state=KeyError")
170171
pass
171172

172173
try:
@@ -175,24 +176,29 @@ def authenticate(self, request, rm):
175176
except KeyError:
176177
pass
177178

178-
if len(potential_authenticators) <= 0:
179-
actor, anonymous = _no_authenticators_found, CHALLENGE
180-
else:
181-
for authenticator in potential_authenticators:
182-
authentication_result = authenticator.is_authenticated(request)
179+
try:
180+
if len(potential_authenticators) <= 0:
181+
actor, anonymous = _no_authenticators_found, CHALLENGE
182+
else:
183+
for authenticator in potential_authenticators:
184+
authentication_result = authenticator.is_authenticated(request)
183185

184-
if not authentication_result:
185-
error = authentication_result
186-
if self.anonymous and rm in self.anonymous.allowed_methods:
186+
if not authentication_result:
187+
error = authentication_result
188+
if self.anonymous and rm in self.anonymous.allowed_methods:
187189

188-
actor, anonymous = self.anonymous(), True
190+
actor, anonymous = self.anonymous(), True
191+
else:
192+
actor, anonymous = authenticator.challenge, CHALLENGE
189193
else:
190-
actor, anonymous = authenticator.challenge, CHALLENGE
191-
else:
192-
return self.handler, False, error
194+
return self.handler, False, error
193195

194-
# XXX: this might be a little weird as it'll contain information about the last executed authenticator
195-
return actor, anonymous, error
196+
# XXX: this might be a little weird as it'll contain information about the last executed authenticator
197+
return actor, anonymous, error
198+
except Exception:
199+
logger.exception(
200+
"ev=dda_resource method=authenticate state=authentication_exception"
201+
)
196202

197203
# TODO: make this method less complex and remove the `noqa`
198204
@vary_on_headers("Authorization") # noqa: C901
@@ -202,6 +208,9 @@ def __call__(self, request, *args, **kwargs): # noqa: C901
202208
that are different (OAuth stuff in `Authorization` header.)
203209
"""
204210
rm = request.method.upper()
211+
logger.info(
212+
f'ev=dda_resource method=__call__ content_type="{request.headers.get("content-type")}" body="{request.body}"' # noqa: E501
213+
)
205214

206215
# Django's internal mechanism doesn't pick up
207216
# PUT request, so we trick it a little here.
@@ -258,12 +267,18 @@ def __call__(self, request, *args, **kwargs): # noqa: C901
258267
_ = request.POST if request.method == "POST" else request.GET
259268
status_code, result = meth(request, *args, **kwargs)
260269
except Exception as e:
270+
logger.exception(
271+
"ev=dda_resource method=__call__ state=exception_during_endpoint_processing"
272+
)
261273
status_code = http.client.BAD_REQUEST
262274
result = self.error_handler(e, request, meth, em_format)
263275

264276
try:
265277
emitter, ct = Emitter.get(em_format)
266278
except ValueError: # pragma: nocover
279+
logger.error(
280+
"ev=dda_resource method=__call__ state=bad_emitter emitter={emitter}"
281+
)
267282
result = rc.BAD_REQUEST
268283
result.content = "Invalid output format specified '%s'." % em_format
269284
return result

django_declarative_apis/resources/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def translate(self):
207207
except (TypeError, ValueError):
208208
# This also catches if loadee is None.
209209
log_mimer_data_exception = getattr(
210-
settings, "DDA_LOG_MIMER_DATA_EXCEPTION", False
210+
settings, "DDA_LOG_MIMER_DATA_EXCEPTION", True
211211
)
212212
if log_mimer_data_exception:
213213
# using the exception logger should give a better hint of what exactly went wrong

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
# built documents.
7575

7676
# The full version, including alpha/beta/rc tags.
77-
release = '0.31.0' # set by bumpversion
77+
release = '0.31.1' # set by bumpversion
7878

7979
# The short X.Y version.
8080
version = release.rsplit('.', 1)[0]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "django-declarative-apis"
7-
version = "0.31.0" # set by bumpversion
7+
version = "0.31.1" # set by bumpversion
88
description = "Simple, readable, declarative APIs for Django"
99
readme = "README.md"
1010
dependencies = [

0 commit comments

Comments
 (0)