From 0dd66c33bffe5e247e86fe86984c5878abede9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 19 Jul 2018 15:05:43 +0200 Subject: [PATCH] Make error handling more robust --- lib/tfw/components/snapshot_provider.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/tfw/components/snapshot_provider.py b/lib/tfw/components/snapshot_provider.py index 0b3b008..69aa33e 100644 --- a/lib/tfw/components/snapshot_provider.py +++ b/lib/tfw/components/snapshot_provider.py @@ -2,7 +2,7 @@ # All Rights Reserved. See LICENSE file for details. import re -from subprocess import run +from subprocess import run, CalledProcessError from getpass import getuser from os.path import isdir from datetime import datetime @@ -11,7 +11,8 @@ from uuid import uuid4 class SnapshotProvider: def __init__(self, directory, git_dir): - author = f'{getuser()} via TFW {self.__class__.__name__}' + self._classname = self.__class__.__name__ + author = f'{getuser()} via TFW {self._classname}' self.gitenv = { 'GIT_DIR': git_dir, 'GIT_WORK_TREE': directory, @@ -32,13 +33,16 @@ class SnapshotProvider: self._run(('git', 'init')) if self._number_of_commits == 0: - self._snapshot() + try: + self._snapshot() + except CalledProcessError: + raise EnvironmentError(f'{self._classname} cannot init on empty directories!') self._check_head_not_detached() def _check_environment(self): if not isdir(self.gitenv['GIT_DIR']) or not isdir(self.gitenv['GIT_WORK_TREE']): - raise EnvironmentError('Directories "directory" and "git_dir" must exist!') + raise EnvironmentError(f'{self._classname}: "directory" and "git_dir" must exist!') @property def _repo_is_initialized(self): @@ -69,7 +73,7 @@ class SnapshotProvider: def _check_head_not_detached(self): if self._head_detached: - raise EnvironmentError(f'{self.__class__.__name__} cannot init from detached HEAD state!') + raise EnvironmentError(f'{self._classname} cannot init from detached HEAD state!') @property def _head_detached(self):