Skip to content

Commit 5bad20c

Browse files
committed
custom exceptions
1 parent 8daa129 commit 5bad20c

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

src/snippet/exceptions.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class SnippetError(Exception):
2+
pass
3+
4+
5+
class DuplicateName(SnippetError):
6+
pass
7+
8+
9+
class ValidationFailure(SnippetError):
10+
pass
11+
12+
13+
class TagMismatch(SnippetError):
14+
pass
15+
16+
17+
class StartEndMismatch(TagMismatch):
18+
pass
19+
20+
21+
class CloakMismatch(TagMismatch):
22+
pass

src/snippet/snippet.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from snippet.config import Config
2+
from snippet import exceptions
23

34

45
def extract_snippets(config: Config, lines, path):
@@ -19,32 +20,32 @@ def extract_snippets(config: Config, lines, path):
1920
examples[current_key] = current_block
2021
current_strip = len(line) - len(line.lstrip())
2122
if capture:
22-
raise Exception('Start/end example mismatch - already capturing at %s' % (current_key,))
23+
raise exceptions.StartEndMismatch(f'Already capturing at {current_key}')
2324
capture = True
2425
continue
2526

2627
if config.end_flag in line:
2728
# stop capturing, and discard empty blocks
2829
if not capture:
29-
raise Exception('Start/end example mismatch - not yet capturing at %s' % (current_key,))
30+
raise exceptions.StartEndMismatch(f'Not yet capturing at {current_key}')
3031
capture = False
3132
if not current_block:
3233
examples.pop(current_key)
3334

3435
if capture:
3536
# whilst capturing, append code lines to the current block
3637
if config.fail_on_dedent and any(line[:current_strip].split(' ')):
37-
raise Exception('Unexpected dedent whilst capturing %s' % (current_key,))
38+
raise exceptions.ValidationFailure(f'Unexpected dedent whilst capturing {current_key}')
3839
clean_line = line[current_strip:].rstrip()
3940
for r_before, r_after in config.replacements.items():
4041
clean_line = clean_line.replace(r_before, r_after)
4142
for trigger in config.fail_on_contains:
4243
if trigger in clean_line:
43-
raise Exception('Unexpected phrase %r at %s' % (trigger, current_key))
44+
raise exceptions.ValidationFailure(f'Unexpected phrase {repr(trigger)} at {current_key}')
4445
# add this line of code to the example block
4546
current_block.append(clean_line)
4647

4748
if capture:
48-
raise Exception('EOF reached whilst still capturing %s' % (current_key,))
49+
raise exceptions.StartEndMismatch(f'EOF reached whilst still capturing {current_key}')
4950

5051
return examples

src/snippet/workflow.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from snippet.config import Config
66
from snippet.snippet import extract_snippets
77
from snippet.wrapper import wrap
8+
from snippet import exceptions
89

910

1011
def run(config: Config):
@@ -30,7 +31,7 @@ def run(config: Config):
3031
for (path, line_num, example_name), code_lines in examples.items():
3132
existing = unique_example_names.get(example_name)
3233
if existing:
33-
raise Exception('Example with duplicate name %s %s matches %s' % (path, line_num, existing))
34+
raise exceptions.DuplicateName('Example with duplicate name %s %s matches %s' % (path, line_num, existing))
3435
else:
3536
unique_example_names[example_name] = (path, line_num, example_name)
3637

tests/test_workflow.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import os
33
import unittest
44

5-
from snippet.workflow import run
5+
from snippet import exceptions
6+
from snippet import workflow
67
from snippet.config import Config
78

89
from tests import test_parser as P
@@ -31,7 +32,7 @@ def test_read(self):
3132
config.input_glob = self.tmp_fp
3233
config.output_dir = self.tmpdir.name
3334

34-
examples, paths, failures = run(config)
35+
examples, paths, failures = workflow.run(config)
3536

3637
with self.subTest(part='found the file'):
3738
self.assertEqual([self.tmp_fp], paths)
@@ -55,5 +56,5 @@ class TestDuplicateNames(Test):
5556
text = text + '\n' + text
5657

5758
def test_read(self):
58-
with self.assertRaises(Exception):
59+
with self.assertRaises(exceptions.DuplicateName):
5960
super().test_read()

0 commit comments

Comments
 (0)