Skip to content

Commit e01d53b

Browse files
catch exception at entry point
1 parent 9790f59 commit e01d53b

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

index_generator/__main__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import jinja2
77
import argparse
88

9+
from jinja2.exceptions import TemplateNotFound
910
from index_generator.models.entries import Entry
11+
from index_generator.models.exceptions import IndexGeneratorTemplateNotFound
1012
from . import *
1113

1214
indexIgnore = ('index.html', 'images', 'favicon.ico')
@@ -29,9 +31,14 @@ def main():
2931
parser.add_argument('--human', action='store_true', default=False, help='Make size human readable.')
3032
parser.add_argument('path', type=str, default='', help='Path', nargs='?')
3133
arguments = parser.parse_args()
32-
app(arguments)
33-
except Exception as e:
34-
print(e)
34+
try:
35+
app(arguments)
36+
except TemplateNotFound as e:
37+
raise IndexGeneratorTemplateNotFound(str(e))
38+
except BaseException as e:
39+
print('[Exception] ' + e.__class__.__name__ + ': ' + str(e))
40+
if hasattr(e, 'hint'):
41+
print(e.hint)
3542

3643

3744
def app(args):

index_generator/models/exceptions.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
class IndexGeneratorBaseException(BaseException):
2-
pass
2+
def __init__(self, message, hint='Something went wrong.'):
3+
super().__init__(message)
4+
self.hint = hint
5+
6+
7+
class IndexGeneratorTemplateNotFound(IndexGeneratorBaseException):
8+
def __init__(self, message):
9+
return super().__init__(message, hint='Template folder does not exists or template file is not found.')

0 commit comments

Comments
 (0)