Added exception handling when trying to get or set request annotations
authorThierry Florac <thierry.florac@onf.fr>
Fri, 26 Jan 2018 15:33:55 +0100
changeset 129 d0f4ef05c378
parent 128 5b80ba4fba8e
child 130 9dd00059a845
Added exception handling when trying to get or set request annotations
src/pyams_utils/request.py
--- a/src/pyams_utils/request.py	Fri Jan 26 15:30:56 2018 +0100
+++ b/src/pyams_utils/request.py	Fri Jan 26 15:33:55 2018 +0100
@@ -152,8 +152,12 @@
     :param object default: the default value when data is missing
     :return: the requested value, or *default*
     """
-    annotations = request.annotations
-    return annotations.get(key, default)
+    try:
+        annotations = request.annotations
+    except AttributeError:
+        return default
+    else:
+        return annotations.get(key, default)
 
 
 def set_request_data(request, key, value):
@@ -163,5 +167,9 @@
     :param str key: request data annotation key
     :param object value: the value to be set in request annotation
     """
-    annotations = request.annotations
-    annotations[key] = value
+    try:
+        annotations = request.annotations
+    except AttributeError:
+        pass
+    else:
+        annotations[key] = value