mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-19 10:59:21 +02:00
Better error handling for EPP
This commit is contained in:
parent
0b5f90762a
commit
a40146a914
3 changed files with 100 additions and 30 deletions
|
@ -43,10 +43,15 @@ except NameError:
|
||||||
# Attn: these imports should NOT be at the top of the file
|
# Attn: these imports should NOT be at the top of the file
|
||||||
try:
|
try:
|
||||||
from .client import CLIENT, commands
|
from .client import CLIENT, commands
|
||||||
|
from .errors import RegistryError, ErrorCode
|
||||||
|
from epplib.models import common
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"CLIENT",
|
"CLIENT",
|
||||||
"commands",
|
"commands",
|
||||||
|
"common",
|
||||||
|
"ErrorCode",
|
||||||
|
"RegistryError",
|
||||||
]
|
]
|
||||||
|
|
|
@ -67,54 +67,47 @@ class EPPLibWrapper:
|
||||||
def _send(self, command):
|
def _send(self, command):
|
||||||
"""Helper function used by `send`."""
|
"""Helper function used by `send`."""
|
||||||
try:
|
try:
|
||||||
|
cmd_type = command.__class__.__name__
|
||||||
with self._connect as wire:
|
with self._connect as wire:
|
||||||
response = wire.send(command)
|
response = wire.send(command)
|
||||||
except (ValueError, ParsingError) as err:
|
except (ValueError, ParsingError) as err:
|
||||||
logger.warning(
|
message = "%s failed to execute due to some syntax error." % cmd_type
|
||||||
"%s failed to execute due to some syntax error."
|
logger.warning(message, exc_info=True)
|
||||||
% command.__class__.__name__,
|
raise RegistryError(message) from err
|
||||||
exc_info=True,
|
|
||||||
)
|
|
||||||
raise RegistryError() from err
|
|
||||||
except TransportError as err:
|
except TransportError as err:
|
||||||
logger.warning(
|
message = "%s failed to execute due to a connection error." % cmd_type
|
||||||
"%s failed to execute due to a connection error."
|
logger.warning(message, exc_info=True)
|
||||||
% command.__class__.__name__,
|
raise RegistryError(message) from err
|
||||||
exc_info=True,
|
|
||||||
)
|
|
||||||
raise RegistryError() from err
|
|
||||||
except LoginError as err:
|
except LoginError as err:
|
||||||
logger.warning(
|
message = "%s failed to execute due to a registry login error." % cmd_type
|
||||||
"%s failed to execute due to a registry login error."
|
logger.warning(message, exc_info=True)
|
||||||
% command.__class__.__name__,
|
raise RegistryError(message) from err
|
||||||
exc_info=True,
|
|
||||||
)
|
|
||||||
raise RegistryError() from err
|
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
logger.warning(
|
message = "%s failed to execute due to an unknown error." % cmd_type
|
||||||
"%s failed to execute due to an unknown error."
|
logger.warning(message, exc_info=True)
|
||||||
% command.__class__.__name__,
|
raise RegistryError(message) from err
|
||||||
exc_info=True,
|
|
||||||
)
|
|
||||||
raise RegistryError() from err
|
|
||||||
else:
|
else:
|
||||||
if response.code >= 2000:
|
if response.code >= 2000:
|
||||||
raise RegistryError(response.msg)
|
raise RegistryError(response.msg, code=response.code)
|
||||||
else:
|
else:
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def send(self, command):
|
def send(self, command, *, cleaned=False):
|
||||||
"""Login, send the command, then close the connection. Tries 3 times."""
|
"""Login, send the command, then close the connection. Tries 3 times."""
|
||||||
|
# try to prevent use of this method without appropriate safeguards
|
||||||
|
if not cleaned:
|
||||||
|
raise ValueError("Please sanitize user input before sending it.")
|
||||||
|
|
||||||
counter = 0 # we'll try 3 times
|
counter = 0 # we'll try 3 times
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
return self._send(command)
|
return self._send(command)
|
||||||
except RegistryError as err:
|
except RegistryError as err:
|
||||||
if counter == 3: # don't try again
|
if err.should_retry() and counter < 3:
|
||||||
raise err
|
|
||||||
else:
|
|
||||||
counter += 1
|
counter += 1
|
||||||
sleep((counter * 50) / 1000) # sleep 50 ms to 150 ms
|
sleep((counter * 50) / 1000) # sleep 50 ms to 150 ms
|
||||||
|
else: # don't try again
|
||||||
|
raise err
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -1,5 +1,77 @@
|
||||||
|
from enum import IntEnum
|
||||||
|
|
||||||
|
|
||||||
|
class ErrorCode(IntEnum):
|
||||||
|
"""
|
||||||
|
Overview of registry response codes from RFC 5730. See RFC 5730 for full text.
|
||||||
|
|
||||||
|
- 1000 - 1500 Success
|
||||||
|
- 2000 - 2308 Registrar did something silly
|
||||||
|
- 2400 - 2500 Registry did something silly
|
||||||
|
- 2501 - 2502 Something malicious or abusive may have occurred
|
||||||
|
"""
|
||||||
|
|
||||||
|
COMMAND_COMPLETED_SUCCESSFULLY = 1000
|
||||||
|
COMMAND_COMPLETED_SUCCESSFULLY_ACTION_PENDING = 1001
|
||||||
|
COMMAND_COMPLETED_SUCCESSFULLY_NO_MESSAGES = 1300
|
||||||
|
COMMAND_COMPLETED_SUCCESSFULLY_ACK_TO_DEQUEUE = 1301
|
||||||
|
COMMAND_COMPLETED_SUCCESSFULLY_ENDING_SESSION = 1500
|
||||||
|
|
||||||
|
UNKNOWN_COMMAND = 2000
|
||||||
|
COMMAND_SYNTAX_ERROR = 2001
|
||||||
|
COMMAND_USE_ERROR = 2002
|
||||||
|
REQUIRED_PARAMETER_MISSING = 2003
|
||||||
|
PARAMETER_VALUE_RANGE_ERROR = 2004
|
||||||
|
PARAMETER_VALUE_SYNTAX_ERROR = 2005
|
||||||
|
UNIMPLEMENTED_PROTOCOL_VERSION = 2100
|
||||||
|
UNIMPLEMENTED_COMMAND = 2101
|
||||||
|
UNIMPLEMENTED_OPTION = 2102
|
||||||
|
UNIMPLEMENTED_EXTENSION = 2103
|
||||||
|
BILLING_FAILURE = 2104
|
||||||
|
OBJECT_IS_NOT_ELIGIBLE_FOR_RENEWAL = 2105
|
||||||
|
OBJECT_IS_NOT_ELIGIBLE_FOR_TRANSFER = 2106
|
||||||
|
AUTHENTICATION_ERROR = 2200
|
||||||
|
AUTHORIZATION_ERROR = 2201
|
||||||
|
INVALID_AUTHORIZATION_INFORMATION = 2202
|
||||||
|
OBJECT_PENDING_TRANSFER = 2300
|
||||||
|
OBJECT_NOT_PENDING_TRANSFER = 2301
|
||||||
|
OBJECT_EXISTS = 2302
|
||||||
|
OBJECT_DOES_NOT_EXIST = 2303
|
||||||
|
OBJECT_STATUS_PROHIBITS_OPERATION = 2304
|
||||||
|
OBJECT_ASSOCIATION_PROHIBITS_OPERATION = 2305
|
||||||
|
PARAMETER_VALUE_POLICY_ERROR = 2306
|
||||||
|
UNIMPLEMENTED_OBJECT_SERVICE = 2307
|
||||||
|
DATA_MANAGEMENT_POLICY_VIOLATION = 2308
|
||||||
|
|
||||||
|
COMMAND_FAILED = 2400
|
||||||
|
COMMAND_FAILED_SERVER_CLOSING_CONNECTION = 2500
|
||||||
|
|
||||||
|
AUTHENTICATION_ERROR_SERVER_CLOSING_CONNECTION = 2501
|
||||||
|
SESSION_LIMIT_EXCEEDED_SERVER_CLOSING_CONNECTION = 2502
|
||||||
|
|
||||||
|
|
||||||
class RegistryError(Exception):
|
class RegistryError(Exception):
|
||||||
pass
|
"""
|
||||||
|
Overview of registry response codes from RFC 5730. See RFC 5730 for full text.
|
||||||
|
|
||||||
|
- 1000 - 1500 Success
|
||||||
|
- 2000 - 2308 Registrar did something silly
|
||||||
|
- 2400 - 2500 Registry did something silly
|
||||||
|
- 2501 - 2502 Something malicious or abusive may have occurred
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, *args, code=None, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.code = code
|
||||||
|
|
||||||
|
def should_retry(self):
|
||||||
|
return self.code == ErrorCode.COMMAND_FAILED
|
||||||
|
|
||||||
|
def is_server_error(self):
|
||||||
|
return self.code is not None and (self.code >= 2400 and self.code <= 2500)
|
||||||
|
|
||||||
|
def is_client_error(self):
|
||||||
|
return self.code is not None and (self.code >= 2000 and self.code <= 2308)
|
||||||
|
|
||||||
|
|
||||||
class LoginError(RegistryError):
|
class LoginError(RegistryError):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue