Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle errors from Octopoes connector #647

Merged
merged 2 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions octopoes/octopoes/connector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ def __str__(self):

class RemoteException(ConnectorException):
pass


class DecodeException(ConnectorException):
pass
6 changes: 3 additions & 3 deletions octopoes/octopoes/connector/octopoes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 12 additions & 2 deletions rocky/crisis_room/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = [
Expand Down Expand Up @@ -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:
Expand Down
15 changes: 14 additions & 1 deletion rocky/rocky/views/finding_list.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
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
from tools.view_helpers import BreadcrumbsMixin
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
Expand Down Expand Up @@ -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 [
Expand Down