Skip to content

Commit

Permalink
Catch RecursionError in dependency tree view #1145
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Jul 29, 2024
1 parent 9e46a41 commit 8f3473d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions scanpipe/templates/scanpipe/project_dependency_tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
</div>

<div class="container is-max-widescreen mb-3">
{% if recursion_error %}
<article class="message is-danger">
<div class="message-body">
The dependency tree cannot be rendered as it contains circular references.
{{ message|linebreaksbr }}
</div>
</article>
{% endif %}
<div id="tree"></div>
</div>
{% endblock %}
Expand Down
6 changes: 6 additions & 0 deletions scanpipe/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,3 +1174,9 @@ def test_scanpipe_views_project_dependency_tree(self):
self.assertContains(
response, '<script id="row_count" type="application/json">5</script>'
)

# Adding a circular reference such as: Project -> A -> B -> C -> B -> C -> ...
make_dependency(project, for_package=c, resolved_to_package=b)
response = self.client.get(url)
self.assertTrue(response.context["recursion_error"])
self.assertContains(response, "The dependency tree cannot be rendered")
8 changes: 7 additions & 1 deletion scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2289,7 +2289,13 @@ class ProjectDependencyTreeView(ConditionalLoginRequired, generic.DetailView):

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
dependency_tree = self.get_dependency_tree(project=self.object)

try:
dependency_tree = self.get_dependency_tree(project=self.object)
except RecursionError:
context["recursion_error"] = True
return context

context["dependency_tree"] = dependency_tree
context["max_depth"] = self.max_depth(dependency_tree)
context["row_count"] = self.count_rows(dependency_tree)
Expand Down

0 comments on commit 8f3473d

Please sign in to comment.