Skip to content

Commit

Permalink
feature: error segregation into errors.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kadenlnelson committed Feb 27, 2019
1 parent 04065b8 commit 33ee61a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
11 changes: 10 additions & 1 deletion doc/api.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
RealValidation API Reference
============================
.. automodule:: realvalidation.realvalidation

Application Object
------------------

.. autoclass:: realvalidation.RealValidation
:members:

Errors
------
.. automodule:: realvalidation.errors
:members:
33 changes: 33 additions & 0 deletions realvalidation/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@


class RealValidationError(Exception):
"""Base class for exceptions in this module."""
pass

class InvalidTokenError(RealValidationError):
"""Raised when a token couldn't be enumerated. Tokens can passed as a
parameter when initializing a ``RealValidation`` object or by setting the
``RV_TOKEN`` environmental variable before execution.
"""
pass

class InvalidPhoneFormatError(RealValidationError):
"""Raised when a phone string doesn't match ``PHONE_REGEX``"""
pass


class InvalidJSONResponseError(RealValidationError):
"""Raised when we couldn't decode a JSON response from a RealValidation
API Request
"""
pass


class ResponseCodeNotOkError(RealValidationError):
"""Raised when ``RESPONSECODE`` in a RealValidation JSON response does not
equal ``OK``
"""
pass
39 changes: 8 additions & 31 deletions realvalidation/realvalidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,10 @@
import re
import os


class InvalidTokenException(Exception):
"""Raised when a token couldn't be enumerated. Tokens can passed as a
parameter when initializing a ``RealValidation`` object or by setting the
``RV_TOKEN`` environmental variable before execution.
"""
pass


class InvalidPhoneFormatException(Exception):
"""Raised when a phone string doesn't match ``PHONE_REGEX``"""
pass


class InvalidJSONResponseException(Exception):
"""Raised when we couldn't decode a JSON response from a RealValidation
API Request
"""
pass


class ResponseCodeNotOkException(Exception):
"""Raised when ``RESPONSECODE`` in a RealValidation JSON response does not
equal ``OK``"""
pass
from .errors import (
InvalidTokenError, InvalidPhoneFormatError, InvalidJSONResponseError,
ResponseCodeNotOkError
)


class RealValidation:
Expand Down Expand Up @@ -76,7 +53,7 @@ def _verify_phone(self, phone):
if re.match(self.phone_regex, phone):
return True

raise InvalidPhoneFormatException
raise InvalidPhoneFormatError

def __init__(self,
token=os.environ.get('RV_TOKEN'),
Expand All @@ -86,7 +63,7 @@ def __init__(self,
self.log = logging.getLogger(__name__)

if token is None:
raise InvalidTokenException
raise InvalidTokenError

self.output = output
self.token = token
Expand Down Expand Up @@ -130,10 +107,10 @@ def lookup_phone(self, phone):
try:
data = req.json()
except ValueError:
raise InvalidJSONResponseException
raise InvalidJSONResponseError

# if realvalidation RESPONSECODE isn't OK raise error
if data.get('RESPONSECODE') != 'OK':
raise ResponseCodeNotOkException
raise ResponseCodeNotOkError

return data

0 comments on commit 33ee61a

Please sign in to comment.