Skip to content

Commit

Permalink
Fixes #6263 - Tribler should open FeedbackDialog if error happend in …
Browse files Browse the repository at this point in the history
…Core during startup
  • Loading branch information
kozlovsky committed Nov 5, 2021
1 parent 8b51589 commit b63ee20
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time
from asyncio import CancelledError
from dataclasses import asdict
from typing import List, Optional

from aiohttp import web

Expand Down Expand Up @@ -71,9 +72,10 @@ class EventsEndpoint(RESTEndpoint, TaskManager):
def __init__(self):
RESTEndpoint.__init__(self)
TaskManager.__init__(self)
self.events_responses = []
self.events_responses: List[RESTStreamResponse] = []
self.app.on_shutdown.append(self.on_shutdown)
self.notifier = None
self.undelivered_error: Optional[dict] = None

# We need to know that Tribler completed its startup sequence
self.tribler_started = False
Expand Down Expand Up @@ -109,30 +111,51 @@ def on_tribler_started(self, _):
def setup_routes(self):
self.app.add_routes([web.get('', self.get_events)])

@task
async def write_data(self, message):
"""
Write data over the event socket if it's open.
"""
if not self.events_responses:
return
def initial_message(self) -> dict:
return {
"type": NTFY.EVENTS_START.value,
"event": {"tribler_started": self.tribler_started, "version": version_id}
}

def error_message(self, reported_error: ReportedError) -> dict:
return {
"type": NTFY.TRIBLER_EXCEPTION.value,
"event": asdict(reported_error),
}

def encode_message(self, message: dict) -> bytes:
try:
message = json.dumps(message)
except UnicodeDecodeError:
# The message contains invalid characters; fix them
self._logger.error("Event contains non-unicode characters, fixing")
message = json.dumps(fix_unicode_dict(message))
message_bytes = b'data: ' + message.encode('utf-8') + b'\n\n'
for request in self.events_responses:
await request.write(message_bytes)
return b'data: ' + message.encode('utf-8') + b'\n\n'

def has_connection_to_gui(self):
return bool(self.events_responses)

@task
async def write_data(self, message):
"""
Write data over the event socket if it's open.
"""
if not self.has_connection_to_gui():
return

message_bytes = self.encode_message(message)
for response in self.events_responses:
await response.write(message_bytes)

# An exception has occurred in Tribler. The event includes a readable
# string of the error and a Sentry event.
def on_tribler_exception(self, reported_error: ReportedError):
self.write_data({
"type": NTFY.TRIBLER_EXCEPTION.value,
"error": asdict(reported_error),
})
message = self.error_message(reported_error)
if self.has_connection_to_gui():
self.write_data(message)
elif not self.undelivered_error:
# If there are several undelivered errors, we store the first error as more important and skip other
self.undelivered_error = message

@docs(
tags=["General"],
Expand Down Expand Up @@ -164,11 +187,15 @@ async def get_events(self, request):
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'})
await response.prepare(request)
# FIXME: Proper start check!
await response.write(b'data: ' + json.dumps({"type": NTFY.EVENTS_START.value,
"event": {"tribler_started": self.tribler_started,
"version": version_id}}).encode('utf-8') + b'\n\n')
await response.write(self.encode_message(self.initial_message()))

if self.undelivered_error:
error = self.undelivered_error
self.undelivered_error = None
await response.write(self.encode_message(error))

self.events_responses.append(response)

try:
while True:
await self.register_anonymous_task('event_sleep', lambda: None, delay=3600)
Expand Down
10 changes: 2 additions & 8 deletions src/tribler-gui/tribler_gui/event_request_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(self, api_port, api_key, error_handler):
NTFY.EVENTS_START.value: self.events_start_received,
NTFY.TRIBLER_STARTED.value: self.tribler_started_event,
NTFY.REPORT_CONFIG_ERROR.value: self.config_error_signal.emit,
NTFY.TRIBLER_EXCEPTION.value: lambda data: self.error_handler.core_error(ReportedError(**data))
}

def events_start_received(self, event_dict):
Expand Down Expand Up @@ -115,14 +116,7 @@ def on_read_data(self):
event_type, event = json_dict.get("type"), json_dict.get("event")
reaction = self.reactions_dict.get(event_type)
if reaction:
if event:
reaction(event)
else:
reaction()
elif event_type == NTFY.TRIBLER_EXCEPTION.value:
self.error_handler.core_error(
reported_error=ReportedError(**json_dict["error"]),
)
reaction(event) if event else reaction()
self.current_event_string = ""

def on_finished(self):
Expand Down

0 comments on commit b63ee20

Please sign in to comment.