Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Thurion committed Jun 28, 2020
1 parent 850b035 commit 41d3d5e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion BackgroundTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def execute(self):
self.rseData.frame.event_generate(RseData.EVENT_RSE_UPDATE_AVAILABLE, when="tail")
break
except Exception as e:
print("{plugin_name}: Failed to retrieve information about available updates. Error: {e}".format(plugin_name=RseData.PLUGIN_NAME, e=e))
RseData.printError("Failed to retrieve information about available updates. Error: {e}".format(e=e))


class TimedTask(BackgroundTask):
Expand Down
9 changes: 8 additions & 1 deletion Backgroundworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

from threading import Thread, Timer
from BackgroundTask import TimedTask
import traceback

from RseData import RseData


class BackgroundWorker(Thread):
Expand Down Expand Up @@ -46,7 +49,11 @@ def run(self):
if not task:
break
else:
task.execute()
try:
task.execute()
except Exception as e:
RseData.printError("Exception occurred in background task {bg}: {e}".format(bg=task.__class__.__name__, e=e))
traceback.print_exc()

self.queue.task_done()

Expand Down
20 changes: 15 additions & 5 deletions RseData.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
import plug
import sys
import os
import time
import math
Expand Down Expand Up @@ -194,8 +193,7 @@ def openLocalDatabase(self):
self.localDbCursor = self.localDbConnection.cursor()
except Exception as e:
error_msg = "{plugin_name}: Local cache database could not be opened".format(plugin_name=RseData.PLUGIN_NAME)
print(error_msg)
plug.show_error(error_msg)
RseData.printError(error_msg, showError=True)

def closeLocalDatabase(self):
if not self.isLocalDatabaseAccessible():
Expand Down Expand Up @@ -384,7 +382,6 @@ def initialize(self):
self.closeLocalDatabase()

# initialize dictionaries
# self.openRemoteDatabase()
if len(self.projectsDict) == 0:
url = urlopen("https://cyberlord.de/rse/projects.py", timeout=10)
response = url.read()
Expand All @@ -394,4 +391,17 @@ def initialize(self):

def printDebug(self, msg):
if self.debug:
print("{plugin_name} (Debug): {msg}".format(plugin_name=RseData.PLUGIN_NAME, msg=msg))
print("{plugin_name}-{version} (Debug): {msg}".format(plugin_name=RseData.PLUGIN_NAME, version=RseData.VERSION, msg=msg))

@staticmethod
def printError(msg, showError=False):
"""
Prints/logs an error and can show it on the main window if necessary.
:param msg: The error message
:param showError: Whether to show it on the EDMC main window or not
:return:
"""
errorMessage = "{plugin_name}-{version}: {msg}".format(plugin_name=RseData.PLUGIN_NAME, version=RseData.VERSION, msg=msg)
print(errorMessage)
if showError:
plug.show_error(errorMessage)

0 comments on commit 41d3d5e

Please sign in to comment.