Skip to content

Latest commit

 

History

History
366 lines (241 loc) · 12 KB

errors.rst

File metadata and controls

366 lines (241 loc) · 12 KB

Error Reference

validator_collection.errors


Handling Errors

Tip

By design, checkers <checker> never raise exceptions. If a given value fails, a checker will just return False.

Validators <validator> always raise exceptions when validation fails.

When validators <validator> fail, they raise exceptions. There are three ways for exceptions to provide you with information that is useful in different circumstances:

  1. Exception Type. The type of the exception itself (and the name of that type) tells you a lot about the nature of the error. On its own, this should be enough for you to understand "what went wrong" and "why validation failed". Most importantly, this is easy to catch in your code using try ... except blocks, giving you fine-grained control over how to handle exceptional situations.
  2. Message. Each exception is raised when a human-readable message, a brief string that says "this is why this exception was raised". This is primarily useful in debugging your code, because at run-time we don't want to parse strings to make control flow decisions.
  3. Stack Trace. Each exception is raised with a stacktrace of the exceptions and calls that preceded it. This helps to provide the context for the error, and is (typically) most useful for debugging and logging purposes. In rare circumstances, we might want to programmatically parse this information...but that's a pretty rare requirement.

We have designed the exceptions raised by the Validator-Collection to leverage all three of these types of information.

Validator Names/Types

By design, all exceptions raised by the Validator-Collection inherit from the built-in exceptions defined in the standard library. This makes it simple to plug the Validator-Collection into existing validation code you have which already catches ValueError <python:ValueError>, TypeError <python:TypeError>, and the like.

However, because we have sub-classed the built-in exceptions, you can easily apply more fine-grained control over your code.

For example, let us imagine a validation which will fail:

from validator_collection import validators

value = validators.decimal('123.45',
                           allow_empty = False,
                           minimum = 0,
                           maximum = 100)

By design, we know that this value will fail validation. We have specified a maximum of 100, and the value being passed in is (a string) with a value of 123.45. This will fail.

We can catch this using a standard/built-in ValueError <python:ValueError> like so:

from validator_collection import validators

try:
    value = validators.decimal('123.45',
                               allow_empty = False,
                               minimum = 0,
                               maximum = 100)
except ValueError as error:
    # Handle the error

Looking at the documentation for validators.decimal() <validator_collection.validators.decimal>, we can see that this will catch all of the following situations:

  • when an empty/false value is passed with allow_empty = False,
  • when a value is less than the allowed minimum,
  • when a value is more than the allowed maximum

But maybe we want to handle each of these situations a little differently? In that case, we can use the custom exceptions defined by the Validator-Collection:

from validator_collection import validators, errors

try:
    value = validators.decimal('123.45',
                               allow_empty = False,
                               minimum = 0,
                               maximum = 100)
except errors.EmptyValueError as error:
    # Handle the situation where an empty value was received.
except errors.MinimumValueError as error:
    # Handle the situation when a value is less than the allowed minimum.
except errors.MaximumValueError as error:
    # Handle the situation when a value is more than the allowed minimum.

Both approaches will work, but one gives you a little more precise control over how your code handles a failed validation.

Tip

We strongly recommend that you review the exceptions raised by each of the validators you use. Each validator precisely documents which exceptions it raises, and each exception's documentation shows what built-in exceptions it inherits from.

Validator Messages

Because the Validator-Collection produces exceptions which inherit from the standard library, we leverage the same API. This means they print to standard output with a human-readable message that provides an explanation for "what went wrong."

Stack Traces

Because the Validator-Collection produces exceptions which inherit from the standard library, it leverages the same API for handling stack trace information. This means that it will be handled just like a normal exception in unit test frameworks, logging solutions, and other tools that might need that information.


Standard Errors

EmptyValueError (from ValueError <python:ValueError>)

EmptyValueError

CannotCoerceError (from TypeError <python:TypeError>)

CannotCoerceError

MinimumValueError (from ValueError <python:ValueError>)

MinimumValueError

MaximumValueError (from ValueError <python:ValueError>)

MaximumValueError

ValidatorUsageError (from ValueError <python:ValueError>)

ValidatorUsageError

CoercionFunctionEmptyError (from ValidatorUsageError)

CoercionFunctionEmptyError

CoercionFunctionError (from ValueError)

CoercionFunctionError


Core

MinimumLengthError (from ValueError <python:ValueError>)

MinimumLengthError

MaximumLengthError (from ValueError <python:ValueError>)

MaximumLengthError

NotNoneError (from ValueError <python:ValueError>)

NotNoneError

NotADictError (from ValueError <python:ValueError>)

NotADictError

NotJSONError (from ValueError <python:ValueError>)

NotJSONError

NotJSONSchemaError (from ValueError <python:ValueError>)

NotJSONSchemaError

JSONValidationError (from ValueError <python:ValueError>)

JSONValidationError

NotAnIterableError (from CannotCoerceError <validator_collection.errors.CannotCoerceError>)

NotAnIterableError

NotCallableError (from ValueError <python:ValueError>)

NotCallableError

InvalidVariableNameError (from ValueError <python:ValueError>)

InvalidVariableNameError


Date / Time

UTCOffsetError (from ValueError <python:ValueError>)

UTCOffsetError

NegativeOffsetMismatchError (from ValueError <python:ValueError>)

NegativeOffsetMismatchError

PositiveOffsetMismatchError (from ValueError <python:ValueError>)

PositiveOffsetMismatchError


Numbers

NotAnIntegerError (from ValueError <python:ValueError>)

NotAnIntegerError


NotPathlikeError (from ValueError <python:ValueError>)

NotPathlikeError

PathExistsError (from IOError <python:IOError>)

PathExistsError

NotAFileError (from IOError <python:IOError>)

NotAFileError

NotADirectoryError (from IOError <python:IOError>)

NotADirectoryError

NotReadableError (from IOError <python:IOError>)

NotReadableError

NotWriteableError (from IOError <python:IOError>)

NotWriteableError

NotExecutableError (from IOError <python:IOError>)

NotExecutableError

NotBytesIOError (from ValueError <python:ValueError>)

NotBytesIOError

NotStringIOError (from ValueError <python:ValueError>)

NotStringIOError


InvalidEmailError (from ValueError <python:ValueError>)

InvalidEmailError

InvalidURLError (from ValueError <python:ValueError>)

InvalidURLError

InvalidDomainError (from ValueError <python:ValueError>)

InvalidDomainError

SlashInDomainError (from InvalidDomainError)

SlashInDomainError

AtInDomainError (from InvalidDomainError)

AtInDomainError

ColonInDomainError (from InvalidDomainError)

ColonInDomainError

WhitespaceInDomainError (from InvalidDomainError)

WhitespaceInDomainError

InvalidIPAddressError (from ValueError <python:ValueError>)

InvalidIPAddressError

InvalidMACAddressError (from ValueError <python:ValueError>)

InvalidMACAddressError