Skip to content

Commit a690e94

Browse files
authored
Merge pull request #32 from rcfox/better-exceptions
When reraising exceptions, don't hide the original exception message.
2 parents 5931d36 + 67523f0 commit a690e94

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

suitcase/structure.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ def write(self, stream):
4545
except Exception:
4646
# keep the original traceback information, see
4747
# http://stackoverflow.com/questions/3847503/wrapping-exceptions-in-python
48-
exc_value = SuitcasePackException("Unexpected exception during pack of %r" % name)
49-
six.reraise(type(exc_value), exc_value, sys.exc_info()[2])
48+
exc_type = SuitcasePackException
49+
_, exc_value, exc_traceback = sys.exc_info()
50+
exc_value = exc_type("Unexpected exception during pack of %r: %s" % (name, str(exc_value)))
51+
six.reraise(exc_type, exc_value, exc_traceback)
5052

5153
# if there is a crc value, seek back to the field and
5254
# pack it with the right value
@@ -120,8 +122,10 @@ def unpack_stream(self, stream):
120122
except SuitcaseException:
121123
raise # just re-raise these
122124
except Exception:
123-
exc_value = SuitcaseParseError("Unexpected exception while unpacking field %r" % name)
124-
six.reraise(type(exc_value), exc_value, sys.exc_info()[2])
125+
exc_type = SuitcaseParseError
126+
_, exc_value, exc_traceback = sys.exc_info()
127+
exc_value = exc_type("Unexpected exception while unpacking field %r: %s" % (name, str(exc_value)))
128+
six.reraise(exc_type, exc_value, exc_traceback)
125129

126130
if greedy_field is not None:
127131
remaining_data = stream.read()
@@ -146,8 +150,10 @@ def unpack_stream(self, stream):
146150
except SuitcaseException:
147151
raise # just re-raise these
148152
except Exception:
149-
exc_value = SuitcaseParseError("Unexpected exception while unpacking field %r" % name)
150-
six.reraise(type(exc_value), exc_value, sys.exc_info()[2])
153+
exc_type = SuitcaseParseError
154+
_, exc_value, exc_traceback = sys.exc_info()
155+
exc_value = exc_type("Unexpected exception while unpacking field %r: %s" % (name, str(exc_value)))
156+
six.reraise(exc_type, exc_value, exc_traceback)
151157

152158
greedy_data_chunk = inverted_stream.read()[::-1]
153159
greedy_field.unpack(greedy_data_chunk)

0 commit comments

Comments
 (0)