From 044c87785e853b4468e8e45a5e5b86022b2dbfae Mon Sep 17 00:00:00 2001 From: Mark Janssen <20283+praseodym@users.noreply.github.com> Date: Tue, 4 Apr 2023 16:47:37 +0200 Subject: [PATCH] Handle errors from Octopoes connector Handle errors from the Octopoes connector on the crisis room and findings pages by displaying a message to the user and logging the error to the Django logger. --- octopoes/octopoes/connector/__init__.py | 4 ++++ octopoes/octopoes/connector/octopoes.py | 6 +++--- rocky/crisis_room/views.py | 14 ++++++++++++-- rocky/rocky/views/finding_list.py | 15 ++++++++++++++- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/octopoes/octopoes/connector/__init__.py b/octopoes/octopoes/connector/__init__.py index 3227aa9e223..a5ca0237d37 100644 --- a/octopoes/octopoes/connector/__init__.py +++ b/octopoes/octopoes/connector/__init__.py @@ -12,3 +12,7 @@ def __str__(self): class RemoteException(ConnectorException): pass + + +class DecodeException(ConnectorException): + pass diff --git a/octopoes/octopoes/connector/octopoes.py b/octopoes/octopoes/connector/octopoes.py index 8a78a904cbb..2d61ae8d729 100644 --- a/octopoes/octopoes/connector/octopoes.py +++ b/octopoes/octopoes/connector/octopoes.py @@ -7,7 +7,7 @@ from requests import Response, HTTPError from octopoes.api.models import Observation, Declaration, ServiceHealth -from octopoes.connector import RemoteException +from octopoes.connector import RemoteException, DecodeException from octopoes.models import ( Reference, OOI, @@ -42,8 +42,8 @@ def _verify_response(response: Response) -> None: data = response.json() raise RemoteException(value=data["value"]) raise error - except json.decoder.JSONDecodeError: - pass + except json.decoder.JSONDecodeError as error: + raise DecodeException("JSON decode error") from error def request( self, diff --git a/rocky/crisis_room/views.py b/rocky/crisis_room/views.py index d5c4e259e00..2ad315c1810 100644 --- a/rocky/crisis_room/views.py +++ b/rocky/crisis_room/views.py @@ -1,14 +1,18 @@ +import logging + from datetime import timezone, datetime from typing import List, Union +from django.contrib import messages from django.conf import settings from django.urls.base import reverse +from django.utils.translation import gettext_lazy as _ from django.views.generic import TemplateView from django_otp.decorators import otp_required from two_factor.views.utils import class_view_decorator from account.models import KATUser -from octopoes.connector import RemoteException +from octopoes.connector import ConnectorException from octopoes.connector.octopoes import OctopoesAPIConnector from octopoes.models.ooi.findings import Finding from rocky.views.ooi_report import build_findings_list_from_store @@ -17,6 +21,8 @@ from tools.models import Organization from tools.view_helpers import BreadcrumbsMixin, convert_date_to_datetime +logger = logging.getLogger(__name__) + class CrisisRoomBreadcrumbsMixin(BreadcrumbsMixin): breadcrumbs = [ @@ -46,7 +52,11 @@ def get_list_for_org(self, organization: Organization) -> Union[List, None]: api_connector = OctopoesAPIConnector(settings.OCTOPOES_API, organization.code) return api_connector.list(self.ooi_types, valid_time=self.get_observed_at()).items - except RemoteException: + except ConnectorException: + messages.add_message( + self.request, messages.ERROR, _("Failed to get list of findings, check server logs for more details.") + ) + logger.exception("Failed to get list of findings for organization %s", organization.code) return [] def get_observed_at(self) -> datetime: diff --git a/rocky/rocky/views/finding_list.py b/rocky/rocky/views/finding_list.py index f230491a48b..cb012cd5211 100644 --- a/rocky/rocky/views/finding_list.py +++ b/rocky/rocky/views/finding_list.py @@ -1,10 +1,14 @@ +import logging + from typing import Any, Dict, List, Optional from django.contrib import messages from django.urls.base import reverse_lazy from django.utils.translation import gettext_lazy as _ + from octopoes.models import DEFAULT_SCAN_LEVEL_FILTER, DEFAULT_SCAN_PROFILE_TYPE_FILTER from octopoes.models.ooi.findings import Finding, MutedFinding +from octopoes.connector import ConnectorException from rocky.views.mixins import OOIList from rocky.views.ooi_view import BaseOOIListView @@ -12,6 +16,8 @@ from tools.ooi_helpers import get_finding_type_from_finding, get_knowledge_base_data_for_ooi, RiskLevelSeverity from account.mixins import OrganizationView +logger = logging.getLogger(__name__) + def sort_by_severity_desc(findings) -> List[Dict[str, Any]]: # Sorting is stable (when multiple records have the same key, their original @@ -71,7 +77,14 @@ def get_queryset(self): except ValueError as e: messages.error(self.request, _(str(e))) - return generate_findings_metadata(findings, muted_findings, severity_filter) + try: + return generate_findings_metadata(findings, muted_findings, severity_filter) + except ConnectorException: + messages.add_message( + self.request, messages.ERROR, _("Failed to get list of findings, check server logs for more details.") + ) + logger.exception("Failed get list of findings") + return [] def build_breadcrumbs(self): return [