parsing strategy error handling for bytes as not an encoded string
This commit is contained in:
parent
26573eeda3
commit
ea2d3223fb
@ -175,7 +175,10 @@ class ParsingStrategy(abc.ABC):
|
||||
class DynamicParsingStrategy(ParsingStrategy):
|
||||
@staticmethod
|
||||
def attempt_parse_json(data):
|
||||
return json.loads(data.decode())
|
||||
data = json.loads(data)
|
||||
if "data" in data:
|
||||
data["data"] = string_to_bytes(data["data"])
|
||||
return data
|
||||
|
||||
@staticmethod
|
||||
def fallback(data):
|
||||
@ -183,10 +186,12 @@ class DynamicParsingStrategy(ParsingStrategy):
|
||||
|
||||
def parse(self, data: bytes) -> Union[bytes, dict]:
|
||||
try:
|
||||
data = self.attempt_parse_json(data)
|
||||
if "data" in data:
|
||||
data["data"] = string_to_bytes(data["data"])
|
||||
return data
|
||||
data = data.decode()
|
||||
except UnicodeDecodeError:
|
||||
return self.fallback(data)
|
||||
|
||||
try:
|
||||
return self.attempt_parse_json(data)
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user