Handle empty URL when using "+" traverser
authorThierry Florac <thierry.florac@onf.fr>
Fri, 22 Feb 2019 12:30:37 +0100
changeset 329 1482a4b86075
parent 328 5f8deef8e5d2
child 330 b20fc80aa6be
Handle empty URL when using "+" traverser
src/pyams_utils/traversing.py
--- a/src/pyams_utils/traversing.py	Mon Feb 18 17:12:39 2019 +0100
+++ b/src/pyams_utils/traversing.py	Fri Feb 22 12:30:37 2019 +0100
@@ -115,17 +115,23 @@
                     traverser = registry.queryMultiAdapter((ob, request), ITraversable, '+')
                     if traverser is None:
                         raise NotFound()
-                    ob = traverser.traverse(vpath_tuple[vroot_idx + i + 2], vpath_tuple[vroot_idx + i + 3:])
-                    i += 1
-                    return {
-                        'context': ob,
-                        'view_name': ''.join(vpath_tuple[vroot_idx + i + 2:]),
-                        'subpath': vpath_tuple[i + 2:],
-                        'traversed': vpath_tuple[:vroot_idx + i + 2],
-                        'virtual_root': vroot,
-                        'virtual_root_path': vroot_tuple,
-                        'root': root
-                    }
+                    try:
+                        ob = traverser.traverse(vpath_tuple[vroot_idx + i + 2], vpath_tuple[vroot_idx + i + 3:])
+                    except IndexError:
+                        # the "+" namespace traverser is waiting for additional elements from input URL
+                        # so a "+" URL not followed by something else is just an error!
+                        raise NotFound()
+                    else:
+                        i += 1
+                        return {
+                            'context': ob,
+                            'view_name': ''.join(vpath_tuple[vroot_idx + i + 2:]),
+                            'subpath': vpath_tuple[i + 2:],
+                            'traversed': vpath_tuple[:vroot_idx + i + 2],
+                            'virtual_root': vroot,
+                            'virtual_root_path': vroot_tuple,
+                            'root': root
+                        }
 
                 elif segment[:2] == ns_selector:
                     # check for namespace prefixed by '++'