-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ulamlabs/exception-handling
Improved exception handling, cleanup
- Loading branch information
Showing
10 changed files
with
274 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from decimal import Decimal | ||
|
||
|
||
def xrp_to_drops(amount): | ||
return int((Decimal(1000000) * amount).quantize(Decimal('1'))) | ||
|
||
|
||
def drops_to_xrp(amount): | ||
return Decimal(amount) / Decimal(1000000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,68 @@ | ||
class RippleBaseException(Exception): | ||
pass | ||
|
||
|
||
class RippleUnfundedPaymentException(RippleBaseException): | ||
error = 'unfunded_payment' | ||
|
||
|
||
class RippleSerializerUnsupportedTypeException(RippleBaseException): | ||
error = 'serializer_unsupported_type' | ||
|
||
|
||
class UnknownRippleError(RippleBaseException): | ||
error = 'unknown_error' | ||
|
||
|
||
class DestinationDoesntExistError(RippleBaseException): | ||
error = 'no_dst' | ||
from aioxrpy.definitions import RippleTransactionResultCategory | ||
|
||
|
||
class NotEnoughAmountToCreateDestinationError(DestinationDoesntExistError): | ||
error = 'no_dst_insuf_xrp' | ||
|
||
|
||
class NeedMasterKeyError(RippleBaseException): | ||
error = 'need_master_key' | ||
class RippleBaseException(Exception): | ||
def __init__(self, error, payload={}): | ||
self.payload = payload | ||
self.error = error | ||
|
||
|
||
class InsufficientReserveError(RippleBaseException): | ||
error = 'insufficient_reserve' | ||
class RippleTransactionException(RippleBaseException): | ||
def __init__(self, error, category, payload={}): | ||
super().__init__(error, payload) | ||
self.category = category | ||
|
||
|
||
class InsufficientReserveOfferError(InsufficientReserveError): | ||
error = 'insufficient_reserve_offer' | ||
class RippleTransactionCostlyFailureException(RippleTransactionException): | ||
def __init__(self, error, payload={}): | ||
super().__init__( | ||
error, RippleTransactionResultCategory.CostlyFailure, payload | ||
) | ||
|
||
|
||
class InsufficientReserveLineError(InsufficientReserveError): | ||
error = 'insufficient_reserve_line' | ||
class RippleTransactionLocalFailureException(RippleTransactionException): | ||
def __init__(self, error, payload={}): | ||
super().__init__( | ||
error, RippleTransactionResultCategory.LocalFailure, payload | ||
) | ||
|
||
|
||
class AssetsFrozenError(RippleBaseException): | ||
error = 'frozen' | ||
class RippleTransactionMalformedException(RippleTransactionException): | ||
def __init__(self, error, payload={}): | ||
super().__init__( | ||
error, RippleTransactionResultCategory.MalformedFailure, payload | ||
) | ||
|
||
|
||
class BadAmountError(RippleBaseException): | ||
error = 'bad_amount' | ||
class RippleTransactionRetriableException(RippleTransactionException): | ||
def __init__(self, error, payload={}): | ||
super().__init__( | ||
error, RippleTransactionResultCategory.RetriableFailure, payload | ||
) | ||
|
||
|
||
class BadFeeError(RippleBaseException): | ||
error = 'bad_fee' | ||
class RippleTransactionFailureException(RippleTransactionException): | ||
def __init__(self, error, payload={}): | ||
super().__init__( | ||
error, RippleTransactionResultCategory.Failure, payload | ||
) | ||
|
||
|
||
class AccountNotFoundError(RippleBaseException): | ||
error = 'act_not_found' | ||
class RippleSerializerUnsupportedTypeException(RippleBaseException): | ||
def __init__(self, payload={}): | ||
super().__init__('serializer_unsupported_type', payload) | ||
|
||
|
||
class ValidatedLedgerUnavailable(RippleBaseException): | ||
# Custom error for when validated_ledger field is missing | ||
error = 'validated_ledger_unavailable' | ||
class UnknownRippleException(RippleBaseException): | ||
def __init__(self, payload={}): | ||
super().__init__('unknown_error', payload) | ||
|
||
|
||
def ripple_error_to_exception(error): | ||
return { | ||
'actNotFound': AccountNotFoundError | ||
}.get( | ||
error, | ||
UnknownRippleError({'error': error}) | ||
) | ||
class InvalidTransactionException(RippleBaseException): | ||
def __init__(self, payload={}): | ||
super().__init__('invalid_transaction', payload) | ||
|
||
|
||
def ripple_result_to_exception(category, code): | ||
""" | ||
https://xrpl.org/tec-codes.html | ||
""" | ||
return { | ||
'UNFUNDED_PAYMENT': RippleUnfundedPaymentException, | ||
'NO_DST': DestinationDoesntExistError, | ||
'NO_DST_INSUF_XRP': NotEnoughAmountToCreateDestinationError, | ||
'NEED_MASTER_KEY': NeedMasterKeyError, | ||
'INSUFFICIENT_RESERVE': InsufficientReserveError, | ||
'INSUFFICIENT_RESERVE_OFFER': InsufficientReserveOfferError, | ||
'INSUFFICIENT_RESERVE_LINE': InsufficientReserveLineError, | ||
'FROZEN': AssetsFrozenError, | ||
'BAD_AMOUNT': BadAmountError, | ||
'BAD_FEE': BadFeeError, | ||
}.get( | ||
code, | ||
UnknownRippleError( | ||
data={ | ||
'category': category, | ||
'code': code | ||
} | ||
) | ||
) | ||
class AccountNotFoundException(RippleBaseException): | ||
def __init__(self, payload={}): | ||
super().__init__('act_not_found', payload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from decimal import Decimal | ||
|
||
from aioxrpy import decimals | ||
|
||
|
||
def test_xrp_to_drops(): | ||
assert decimals.xrp_to_drops(Decimal('123.456789')) == 123456789 | ||
|
||
|
||
def test_drops_to_xrp(): | ||
assert decimals.drops_to_xrp(123456789) == Decimal('123.456789') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.