Skip to content

Commit fa50b07

Browse files
committed
Throw some (more) proper exceptions on error.
Also store some additional debug information into the exceptions. This will later be useful in creating parse debug tools!
1 parent ace2863 commit fa50b07

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

sc2reader/exceptions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class MutipleMatchingFilesError(SC2ReaderError):
88
pass
99

1010
class ReadError(SC2ReaderError):
11-
pass
11+
def __init__(self, msg, replay=None, game_events=[], buffer=None, location=None):
12+
self.__dict__.update(locals())
13+
super(ReadError, self).__init__(msg)
1214

1315
class ProcessError(SC2ReaderError):
1416
pass

sc2reader/readers.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .parsers import *
22
from .objects import *
33
from .utils import AttributeDict, LITTLE_ENDIAN
4-
4+
import exceptions
55

66
class InitDataReader(object):
77
def __call__(self,buffer, replay):
@@ -231,14 +231,18 @@ def __call__(self, buffer, replay):
231231
# If the type is not a key in the PARSERS lookup table we
232232
# probably incorrectly parsed the previous event
233233
# TODO: Raise an error instead an store the previous event
234-
msg = "Unknown event: %X - %X at %X"
235-
raise TypeError(msg % (type, code, start))
234+
msg = "Unknown event type: {0:X} - {1:X} at {2:X}".format(type, code, start)
235+
raise exceptions.ReadError(msg, replay, game_events, buffer, start)
236236

237237
except TypeError:
238238
# For some reason, the type handler that we delegated to didn't
239239
# recognize the event code that we extracated.
240240
# TODO: Do something meaningful here
241-
raise
241+
msg = "Unknown event code: {0:X} - {1:X} at {2:X}".format(type, code, start)
242+
raise exceptions.ReadError(msg, replay, game_events, buffer, start)
243+
244+
except exceptions.ReadError as e:
245+
raise exceptions.ReadError(e.msg, replay, game_events, buffer, start)
242246

243247
# Because events are parsed in a bitwise fashion, they sometimes
244248
# leave the buffer in a bitshifted state. Each new event always
@@ -257,7 +261,7 @@ def get_setup_parser(self, code):
257261
elif code in (0x05,): return self.parse_start_event
258262
else:
259263
# TODO: Raise a better error
260-
raise TypeError()
264+
raise exceptions.ReadError("Unknown Setup Parser Code {0}".format(code))
261265

262266
def get_action_parser(self, code):
263267
# The action events are always associated with a particular player and
@@ -269,7 +273,7 @@ def get_action_parser(self, code):
269273
elif code & 0x0F == 0xF: return self.parse_transfer_event
270274
else:
271275
# TODO: Raise a better error
272-
raise TypeError()
276+
raise exceptions.ReadError("Unknown Action Parser Code {0}".format(code))
273277

274278
def get_unknown2_parser(self, code):
275279
# While its unclear what these events represent, they are MUCH more
@@ -280,7 +284,7 @@ def get_unknown2_parser(self, code):
280284
elif code == 0x0E: return self.parse_020E_event
281285
else:
282286
# TODO: Raise a better error
283-
raise TypeError()
287+
raise exceptions.ReadError("Unknown Unknown2 Parser Code {0}".format(code))
284288

285289
def get_camera_parser(self, code):
286290
# Each player's camera control events are recorded, separately from the
@@ -293,7 +297,7 @@ def get_camera_parser(self, code):
293297
elif code & 0x0F == 1: return self.parse_cameraX1_event
294298
else:
295299
# TODO: Raise a better error
296-
raise TypeError()
300+
raise exceptions.ReadError("Unknown Camera Parser Code {0}".format(code))
297301

298302
def get_unknown4_parser(self, code):
299303
# I don't know anything about these events. Any parse information for
@@ -309,7 +313,7 @@ def get_unknown4_parser(self, code):
309313
elif code & 0x0F == 0x0C: return self.parse_04XC_event
310314
else:
311315
# TODO: Raise a better error
312-
raise TypeError()
316+
raise exceptions.ReadError("Unknown Unknown4 Parser Code {0}".format(code))
313317

314318
# The storage format for many of the game events has changed, sometimes
315319
# dramatically, over time. To handle this inconsistency sc2reader uses mixins

sc2reader/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import os
44
import struct
55

6+
import exceptions
7+
68
from itertools import groupby
79

810
LITTLE_ENDIAN,BIG_ENDIAN = '<','>'
@@ -521,7 +523,7 @@ def read_header(file):
521523
#Sanity check that the input is in fact an MPQ file
522524
if buffer.empty or buffer.read_hex(4).upper() != "4D50511B":
523525
print "Header Hex was: %s" % buffer.read_hex(4).upper()
524-
raise ValueError("File '%s' is not an MPQ file" % file.name)
526+
raise exceptions.FileError("File '%s' is not an MPQ file" % file.name)
525527

526528
#Extract replay header data, we are unlikely to ever use most of this
527529
max_data_size = buffer.read_int(LITTLE_ENDIAN)

0 commit comments

Comments
 (0)