Skip to content

Commit

Permalink
Adding a single exception handler to handle unhandled exceptions (pym…
Browse files Browse the repository at this point in the history
…edusa#1345)

* Adding a single exception handler to handle unhandled exceptions

* Fixing flake8 issues
  • Loading branch information
ratoaq2 authored and fernandog committed Oct 28, 2016
1 parent 7129f02 commit 2182f1e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
25 changes: 25 additions & 0 deletions medusa/ehandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# coding=utf-8
"""Exception Handler."""
from __future__ import unicode_literals

import logging

logger = logging.getLogger(__name__)


def handle(thread_name, err):
"""Single entry point to handle unhandled exceptions.
:param thread_name:
:type thread_name: string
:param err:
:type err: Exception
"""
if isinstance(err, OSError):
if err.errno == 28:
logger.warning('Out of disk space: {error_msg}', error_msg=err.message)
else:
logger.warning('OS error: {error_msg}', error_msg=err.message)
else:
logger.exception("Exception generated in thread {thread_name}: {error_msg}",
thread_name=thread_name, error_msg=err.message)
7 changes: 2 additions & 5 deletions medusa/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import datetime
import threading
import time
import traceback

from . import logger
from .helper.exceptions import ex
from . import ehandler, logger


class Scheduler(threading.Thread):
Expand Down Expand Up @@ -111,5 +109,4 @@ def run(self):
# exiting thread
self.stop.clear()
except Exception as e:
logger.log(u"Exception generated in thread " + self.name + ": " + ex(e), logger.ERROR)
logger.log(repr(traceback.format_exc()), logger.DEBUG)
ehandler.handle(self.name, e)
7 changes: 3 additions & 4 deletions medusa/server/web/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from tornado.web import HTTPError, RequestHandler, authenticated
from ...api.v1.core import function_mapper
from .... import (
classes, db, helpers, logger, network_timezones, ui
classes, db, ehandler, helpers, logger, network_timezones, ui
)
from ....media.ShowBanner import ShowBanner
from ....media.ShowFanArt import ShowFanArt
Expand Down Expand Up @@ -272,9 +272,8 @@ def async_call(self, function):

result = function(**kwargs)
return result
except Exception:
logger.log(u'Failed doing web ui callback: {error}'.format(error=traceback.format_exc()), logger.ERROR)
raise
except Exception as e:
ehandler.handle('HTTP REQUEST', e)

# post uses get method
post = get
Expand Down
10 changes: 8 additions & 2 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
pass

import medusa as app
from medusa import db, failed_history, logger, name_cache, network_timezones
from medusa import db, ehandler, failed_history, logger, name_cache, network_timezones
from medusa.event_queue import Events
from medusa.server.core import AppWebServer
from medusa.tv import TVShow
Expand Down Expand Up @@ -164,10 +164,16 @@ def help_message():

return help_msg

def start(self, args): # pylint: disable=too-many-branches,too-many-statements
def start(self, args):
"""
Start Application
"""
try:
self._do_start(args)
except Exception as e:
ehandler.handle('MAIN', e)

def _do_start(self, args):
# do some preliminary stuff
app.MY_FULLNAME = os.path.normpath(os.path.abspath(__file__))
app.MY_NAME = os.path.basename(app.MY_FULLNAME)
Expand Down

0 comments on commit 2182f1e

Please sign in to comment.