Skip to content

Commit ed7f20a

Browse files
author
Thomas Grainger
committed
expand exception hierarchy
1 parent 4d232c9 commit ed7f20a

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

whatthepatch/apply.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _apply_diff_with_subprocess(diff, lines):
2929
# call out to patch program
3030
patchexec = which('patch')
3131
if not patchexec:
32-
raise exceptions.ApplyException('patch program does not exist')
32+
raise exceptions.SubprocessException('patch program does not exist')
3333

3434
filepath = '/tmp/wtp-' + str(hash(diff.header))
3535
oldfilepath = filepath + '.old'
@@ -67,7 +67,7 @@ def _apply_diff_with_subprocess(diff, lines):
6767

6868
# do this last to ensure files get cleaned up
6969
if ret != 0:
70-
raise exceptions.ApplyException('patch program failed')
70+
raise exceptions.SubprocessException('patch program failed', code=ret)
7171

7272
return lines, rejlines
7373

@@ -87,15 +87,15 @@ def apply_diff(diff, text, use_patch=False):
8787
# might have to check for line is None here for ed scripts
8888
if old is not None and line is not None:
8989
if old > n_lines:
90-
raise exceptions.ApplyException(
90+
raise exceptions.HunkApplyException(
9191
'context line {n}, "{l}" does not exist in source'.format(
9292
n=old,
9393
l=line,
9494
),
9595
hunk=hunk,
9696
)
9797
if lines[old-1] != line:
98-
raise exceptions.ApplyException(
98+
raise exceptions.HunkApplyException(
9999
'context line {n}, "{l}" does not match "{sl}"'.format(
100100
n=old,
101101
l=line,

whatthepatch/exceptions.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@ def __init__(self, msg, hunk=None):
1414
super(HunkException, self).__init__(msg)
1515

1616

17-
class ApplyException(HunkException):
17+
class ApplyException(WhatThePatchException):
18+
pass
19+
20+
21+
class SubprocessException(ApplyException):
22+
def __init__(self, msg, code):
23+
super(SubprocessException, self).__init__(msg)
24+
self.code = code
25+
26+
27+
class HunkApplyException(HunkException, ApplyException, ValueError):
1828
pass
1929

2030

0 commit comments

Comments
 (0)