Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ye11ow committed Mar 2, 2020
1 parent 3c4a2a7 commit b2477bd
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions noti.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ def get_config(self, vcs):
return {**self._shared_config, **self.user_config.get(vcs)}

class NotiError(Exception):
def __init__(self, vcs, message):
def __init__(self, vcs, message, help_link=None):
self.vcs = vcs
self.message = message
self.help_link = help_link

class Gitlab(VCS):
def __init__(self, config):
Expand All @@ -174,7 +175,7 @@ def __init__(self, config):
try:
import gitlab
except:
raise NotiError('gitlab', 'Missing dependencies: You need to install python-gitlab')
raise NotiError('gitlab', 'Missing dependencies: You need to install python-gitlab', 'https://python-gitlab.readthedocs.io/en/stable/install.html')

self._gl = gitlab.Gitlab(self.host, private_token=self.token)

Expand Down Expand Up @@ -267,7 +268,7 @@ def __init__(self, config):
try:
import github
except:
raise NotiError('github', 'Missing dependencies: You need to install PyGithub')
raise NotiError('github', 'Missing dependencies: You need to install PyGithub', 'https://pygithub.readthedocs.io/en/latest/introduction.html#download-and-install')

self._gh = github.Github(self.token, base_url=self.host)

Expand Down Expand Up @@ -386,12 +387,13 @@ def print(self):
def add_error(self, title):
self._configs.insert(0, f"{title}| color=red")

def fatal(self, title, extra):
def fatal(self, message, help_link=None):
self.clear()

self.title(title)
if extra:
self.add(extra)
self.title('Noti Error | color=red')
if help_link is not None:
message += ' | color=red href=' + help_link
self.add(message)

self.print()
exit(1)
Expand Down Expand Up @@ -502,11 +504,11 @@ def time_diff(self, before):
from dateutil.tz import tzlocal
from dateutil.tz import tzutc
except:
bp.fatal('Missing dependencies', 'You need to install python-dateutil | href=https://dateutil.readthedocs.io/en/stable/#installation')
bp.fatal('Missing dependencies: You need to install python-dateutil', 'https://dateutil.readthedocs.io/en/stable/#installation')

def main(vcs, bp):
if len(vcs) == 0:
bp.fatal('Wrong configuration', 'You have to configure either gitlab or github')
bp.fatal('You have to configure either gitlab or github')

mrs = {}

Expand All @@ -531,9 +533,12 @@ def main(vcs, bp):
if __name__ == "__main__":
conf = NotiConfig()
vcs = []
if 'gitlab' in conf.user_config:
vcs.append(Gitlab(conf.get_config('gitlab')))
if 'github' in conf.user_config:
vcs.append(Github(conf.get_config('github')))
try:
if 'gitlab' in conf.user_config:
vcs.append(Gitlab(conf.get_config('gitlab')))
if 'github' in conf.user_config:
vcs.append(Github(conf.get_config('github')))
except NotiError as e:
bp.fatal(f"[{e.vcs}] {e.message}", e.help_link)

main(vcs, bp)

0 comments on commit b2477bd

Please sign in to comment.