Skip to content

Commit 23bd210

Browse files
author
Lukáš Klíma
committed
add context to RestValidationError
1 parent 654a2e4 commit 23bd210

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

docs/exceptions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,4 @@ Pyston forms exception classes are defined in ``pyston.forms``.
122122
.. exception:: RestValidationError
123123

124124
``RestValidationError`` is similar to Django ``ValidationError`` but it can contain only one error message with one
125-
code.
125+
code and context.

pyston/forms/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,15 @@ def __str__(self):
136136

137137
class RestValidationError(RestError):
138138

139-
def __init__(self, message, code=None):
139+
def __init__(self, message, code=None, context=None):
140140
if isinstance(message, (RestValidationError, ValidationError)):
141141
self.message = message.message
142142
self.code = message.code
143+
self.context = getattr(message, 'context', {}) | (context or {})
143144
else:
144145
self.message = message
145146
self.code = code or DEFAULT_CODE
147+
self.context = context or {}
146148

147149
def __str__(self):
148150
return self.message
@@ -613,7 +615,9 @@ def _parse_rest_errors(self, errors):
613615
elif isinstance(errors, dict):
614616
return RestDictError({k: self._parse_rest_errors(v) for k, v in errors.items()})
615617
else:
616-
return RestValidationError(list(errors.as_data()[0])[0], errors.as_data()[0].code)
618+
error = errors.as_data()[0]
619+
context = error.params if isinstance(error, ValidationError) else None
620+
return RestValidationError(list(error)[0], error.code, context=context)
617621

618622
def is_invalid(self):
619623
"""

0 commit comments

Comments
 (0)