Skip to content

Commit

Permalink
custom exception and parsingerror
Browse files Browse the repository at this point in the history
  • Loading branch information
diminator committed Apr 26, 2016
1 parent 5da30b3 commit 4941392
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
6 changes: 6 additions & 0 deletions cryptoconditions/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Custom exceptions used in the `cryptoconditions` package.
"""


class ParsingError(Exception):
"""Raised when a URI cannot be parsed"""
15 changes: 9 additions & 6 deletions cryptoconditions/fulfillment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from cryptoconditions import TypeRegistry
from cryptoconditions.condition import Condition
from cryptoconditions.crypto import base64_remove_padding, base64_add_padding
from cryptoconditions.exceptions import ParsingError
from cryptoconditions.lib import Writer, Reader, Predictor

FULFILLMENT_REGEX = r'^cf:([1-9a-f][0-9a-f]{0,3}|0):[a-zA-Z0-9_-]*$'
Expand Down Expand Up @@ -37,14 +38,16 @@ def from_uri(serialized_fulfillment):

if not re.match(Fulfillment.REGEX, serialized_fulfillment):
raise ValueError('Invalid fulfillment format')
try:
type_id = int(pieces[1], 16)
payload = base64.urlsafe_b64decode(base64_add_padding(pieces[2]))

type_id = int(pieces[1], 16)
payload = base64.urlsafe_b64decode(base64_add_padding(pieces[2]))
cls = TypeRegistry.get_class_from_type_id(type_id)
fulfillment = cls()

cls = TypeRegistry.get_class_from_type_id(type_id)
fulfillment = cls()

fulfillment.parse_payload(Reader.from_source(payload), len(payload))
fulfillment.parse_payload(Reader.from_source(payload), len(payload))
except Exception as e:

This comment has been minimized.

Copy link
@vrde

vrde Apr 26, 2016

Contributor

Catch only what you can handle. A "catch all" condition can be extremely difficult to debug.

raise ParsingError(str(e))

return fulfillment

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

setup(
name='cryptoconditions',
version='0.2.1',
version='0.2.2',
description='Multi-algorithm, multi-level, multi-signature format for '
'expressing conditions and fulfillments according to the Interledger Protocol (ILP).',
long_description=__doc__,
Expand Down

0 comments on commit 4941392

Please sign in to comment.