Oh fine I'll have an exceptions module.

I generally hate this, but there's enough ugliness and eventually it'd be nice
to have these in a public module for when we might want them out of __init__.py
This commit is contained in:
Julian Berman
2013-05-20 18:34:32 -04:00
parent 0fe509c9c0
commit 25fb92cd2c
5 changed files with 122 additions and 107 deletions

View File

@@ -9,13 +9,14 @@ instance under a schema, and will create a validator for you.
"""
from jsonschema._format import (
FormatChecker, FormatError, draft3_format_checker, draft4_format_checker,
from jsonschema.exceptions import (
FormatError, RefResolutionError, SchemaError, UnknownType, ValidationError
)
from jsonschema._format import (
FormatChecker, draft3_format_checker, draft4_format_checker,
)
from jsonschema._validators import ValidationError
from jsonschema.validators import (
RefResolutionError, SchemaError, UnknownType, ErrorTree,
Draft3Validator, Draft4Validator, RefResolver, ValidatorMixin,
ErrorTree, Draft3Validator, Draft4Validator, RefResolver, ValidatorMixin,
validate, validates,
)

View File

@@ -2,23 +2,7 @@ import datetime
import re
import socket
from jsonschema.compat import PY3
class FormatError(Exception):
def __init__(self, message, cause=None):
super(FormatError, self).__init__(message, cause)
self.message = message
self.cause = self.__cause__ = cause
def __str__(self):
return self.message.encode("utf-8")
def __unicode__(self):
return self.message
if PY3:
__str__ = __unicode__
from jsonschema.exceptions import FormatError
class FormatChecker(object):

View File

@@ -1,89 +1,11 @@
import collections
import pprint
import re
import textwrap
from jsonschema import _utils
from jsonschema._format import FormatError
from jsonschema.compat import PY3, iteritems
from jsonschema.exceptions import FormatError, ValidationError
from jsonschema.compat import iteritems
FLOAT_TOLERANCE = 10 ** -15
unset = _utils.Unset()
class _Error(Exception):
def __init__(
self, message, validator=unset, path=(), cause=None, context=(),
validator_value=unset, instance=unset, schema=unset, schema_path=(),
):
self.message = message
self.path = collections.deque(path)
self.schema_path = collections.deque(schema_path)
self.context = list(context)
self.cause = self.__cause__ = cause
self.validator = validator
self.validator_value = validator_value
self.instance = instance
self.schema = schema
@classmethod
def create_from(cls, other):
return cls(
message=other.message,
cause=other.cause,
context=other.context,
path=other.path,
schema_path=other.schema_path,
validator=other.validator,
validator_value=other.validator_value,
instance=other.instance,
schema=other.schema,
)
def _set(self, **kwargs):
for k, v in iteritems(kwargs):
if getattr(self, k) is unset:
setattr(self, k, v)
def __repr__(self):
return "<%s: %r>" % (self.__class__.__name__, self.message)
def __str__(self):
return unicode(self).encode("utf-8")
def __unicode__(self):
if unset in (
self.validator, self.validator_value, self.instance, self.schema,
):
return self.message
path = _utils.format_as_index(self.path)
schema_path = _utils.format_as_index(list(self.schema_path)[:-1])
pschema = pprint.pformat(self.schema, width=72)
pinstance = pprint.pformat(self.instance, width=72)
return self.message + textwrap.dedent("""
Failed validating %r in schema%s:
%s
On instance%s:
%s
""".rstrip()
) % (
self.validator,
schema_path,
_utils.indent(pschema),
path,
_utils.indent(pinstance),
)
if PY3:
__str__ = __unicode__
class ValidationError(_Error): pass
def patternProperties(validator, patternProperties, instance, schema):

112
jsonschema/exceptions.py Normal file
View File

@@ -0,0 +1,112 @@
import collections
import pprint
import textwrap
from jsonschema import _utils
from jsonschema.compat import PY3, iteritems
_unset = _utils.Unset()
class _Error(Exception):
def __init__(
self, message, validator=_unset, path=(), cause=None, context=(),
validator_value=_unset, instance=_unset, schema=_unset, schema_path=(),
):
self.message = message
self.path = collections.deque(path)
self.schema_path = collections.deque(schema_path)
self.context = list(context)
self.cause = self.__cause__ = cause
self.validator = validator
self.validator_value = validator_value
self.instance = instance
self.schema = schema
@classmethod
def create_from(cls, other):
return cls(
message=other.message,
cause=other.cause,
context=other.context,
path=other.path,
schema_path=other.schema_path,
validator=other.validator,
validator_value=other.validator_value,
instance=other.instance,
schema=other.schema,
)
def _set(self, **kwargs):
for k, v in iteritems(kwargs):
if getattr(self, k) is _unset:
setattr(self, k, v)
def __repr__(self):
return "<%s: %r>" % (self.__class__.__name__, self.message)
def __str__(self):
return unicode(self).encode("utf-8")
def __unicode__(self):
if _unset in (
self.validator, self.validator_value, self.instance, self.schema,
):
return self.message
path = _utils.format_as_index(self.path)
schema_path = _utils.format_as_index(list(self.schema_path)[:-1])
pschema = pprint.pformat(self.schema, width=72)
pinstance = pprint.pformat(self.instance, width=72)
return self.message + textwrap.dedent("""
Failed validating %r in schema%s:
%s
On instance%s:
%s
""".rstrip()
) % (
self.validator,
schema_path,
_utils.indent(pschema),
path,
_utils.indent(pinstance),
)
if PY3:
__str__ = __unicode__
class ValidationError(_Error):
pass
class SchemaError(_Error):
pass
class RefResolutionError(Exception):
pass
class UnknownType(Exception):
pass
class FormatError(Exception):
def __init__(self, message, cause=None):
super(FormatError, self).__init__(message, cause)
self.message = message
self.cause = self.__cause__ = cause
def __str__(self):
return self.message.encode("utf-8")
def __unicode__(self):
return self.message
if PY3:
__str__ = __unicode__

View File

@@ -15,11 +15,7 @@ from jsonschema.compat import (
PY3, Sequence, urljoin, urlsplit, urldefrag, unquote, urlopen,
str_types, int_types, iteritems,
)
class RefResolutionError(Exception): pass
class SchemaError(_validators._Error): pass
class UnknownType(Exception): pass
from jsonschema.exceptions import RefResolutionError, SchemaError, UnknownType
_unset = _utils.Unset()