diff options
Diffstat (limited to 'jsonschema')
-rw-r--r-- | jsonschema/exceptions.py | 87 |
1 files changed, 36 insertions, 51 deletions
diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index 14f9a88..97a60ed 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -21,6 +21,10 @@ _unset = _utils.Unset() class _Error(Exception): + + _word_for_schema_in_error_message: ClassVar[str] + _word_for_instance_in_error_message: ClassVar[str] + def __init__( self, message: str, @@ -65,8 +69,34 @@ class _Error(Exception): def __repr__(self): return f"<{self.__class__.__name__}: {self.message!r}>" - def __str__(self) -> str: - return self.message + def __str__(self): + essential_for_verbose = ( + self.validator, self.validator_value, self.instance, self.schema, + ) + if any(m is _unset for m in essential_for_verbose): + return self.message + + schema_path = _utils.format_as_index( + container=self._word_for_schema_in_error_message, + indices=list(self.relative_schema_path)[:-1], + ) + instance_path = _utils.format_as_index( + container=self._word_for_instance_in_error_message, + indices=self.relative_path, + ) + prefix = 16 * " " + + return dedent( + f"""\ + {self.message} + + Failed validating {self.validator!r} in {schema_path}: + {indent(pformat(self.schema, width=72), prefix).lstrip()} + + On {instance_path}: + {indent(pformat(self.instance, width=72), prefix).lstrip()} + """.rstrip(), + ) @classmethod def create_from(cls, other): @@ -112,16 +142,8 @@ class _Error(Exception): def _contents(self): attrs = ( - "message", - "cause", - "context", - "validator", - "validator_value", - "path", - "schema_path", - "instance", - "schema", - "parent", + "message", "cause", "context", "validator", "validator_value", + "path", "schema_path", "instance", "schema", "parent", ) return dict((attr, getattr(self, attr)) for attr in attrs) @@ -140,44 +162,7 @@ class _Error(Exception): ) -class _VerboseError(_Error): - _word_for_schema_in_error_message: ClassVar[str] - _word_for_instance_in_error_message: ClassVar[str] - - def __str__(self): - essential_for_verbose = ( - self.validator, - self.validator_value, - self.instance, - self.schema, - ) - if any(m is _unset for m in essential_for_verbose): - return self.message - - schema_path = _utils.format_as_index( - container=self._word_for_schema_in_error_message, - indices=list(self.relative_schema_path)[:-1], - ) - instance_path = _utils.format_as_index( - container=self._word_for_instance_in_error_message, - indices=self.relative_path, - ) - prefix = 16 * " " - - return dedent( - f"""\ - {self.message} - - Failed validating {self.validator!r} in {schema_path}: - {indent(pformat(self.schema, width=72), prefix).lstrip()} - - On {instance_path}: - {indent(pformat(self.instance, width=72), prefix).lstrip()} - """.rstrip(), - ) - - -class ValidationError(_VerboseError): +class ValidationError(_Error): """ An instance was invalid under a provided schema. """ @@ -186,7 +171,7 @@ class ValidationError(_VerboseError): _word_for_instance_in_error_message = "instance" -class SchemaError(_VerboseError): +class SchemaError(_Error): """ A schema was invalid under its corresponding metaschema. """ |