Skip to content

Commit 58a87ad

Browse files
authored
Throw ValueError when discriminator cannot be found (#88)
* Throw ValueError when discriminator cannot be found This provides the receiver with more details about the Commercetools response * Fix formatting Co-authored-by: Tim Leguijt <t.leguijt@labdigital.nl>
1 parent 0237387 commit 58a87ad

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/commercetools/helpers.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(
6666
only=None,
6767
discriminator_field=None,
6868
discriminator_schemas=None,
69-
**kwargs
69+
**kwargs,
7070
):
7171
# Raise error if only or exclude is passed as string, not list of strings
7272
if only is not None and not is_collection(only):
@@ -149,7 +149,13 @@ def _load(self, value, data, **kwargs):
149149
return None
150150

151151
discriminator_value = value[self.discriminator_field[0]]
152-
schema_name = self.discriminator_schemas[discriminator_value]
152+
try:
153+
schema_name = self.discriminator_schemas[discriminator_value]
154+
except KeyError:
155+
raise ValueError(
156+
f"Could not find discriminator schema {discriminator_value} for field '{self.name}' ({value})"
157+
)
158+
153159
schema = self.get_schema(schema_name)
154160

155161
try:

0 commit comments

Comments
 (0)