Skip to content

Expose typed Python exceptions#114

Merged
piotr-roslaniec merged 10 commits intonucypher:mainfrom
piotr-roslaniec:python-exceptions
Jun 2, 2023
Merged

Expose typed Python exceptions#114
piotr-roslaniec merged 10 commits intonucypher:mainfrom
piotr-roslaniec:python-exceptions

Conversation

@piotr-roslaniec
Copy link
Copy Markdown

@piotr-roslaniec piotr-roslaniec commented May 10, 2023

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 10, 2023

Codecov Report

Merging #114 (b871ce1) into main (ec954d4) will decrease coverage by 1.99%.
The diff coverage is 11.76%.

@@            Coverage Diff             @@
##             main     #114      +/-   ##
==========================================
- Coverage   79.67%   77.68%   -1.99%     
==========================================
  Files          23       24       +1     
  Lines        4796     4924     +128     
==========================================
+ Hits         3821     3825       +4     
- Misses        975     1099     +124     
Impacted Files Coverage Δ
ferveo-python/src/error.rs 0.00% <0.00%> (ø)
ferveo/src/lib.rs 97.40% <ø> (ø)
tpke/src/lib.rs 99.85% <ø> (ø)
ferveo-python/src/lib.rs 58.16% <18.39%> (-6.00%) ⬇️
ferveo/src/dkg.rs 95.49% <100.00%> (ø)
tpke/src/ciphertext.rs 89.41% <100.00%> (-0.06%) ⬇️

@piotr-roslaniec piotr-roslaniec requested a review from KPrasch May 10, 2023 12:23
@piotr-roslaniec piotr-roslaniec marked this pull request as ready for review May 10, 2023 12:23
@piotr-roslaniec
Copy link
Copy Markdown
Author

@KPrasch Let me know how that works for you

Comment on lines +258 to +262
class BincodeError(Exception):
pass


class ArkSerializeError(Exception):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we are leaking implementation details for these...perhaps something more generic? SerializationError or ... ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point and on a similar note: How many typed errors do we need? Specifically, what are the cases where nucypher has to distinguish between Python exceptions in order to act on that information? Perhaps we only need a few o them, like InvalidAggregate and ThresholdEncryptionError, and everything we could put into exception.ValueError or something.

Copy link
Copy Markdown
Member

@derekpierre derekpierre May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Unsure at the moment but something we can look into. Happy to have a more in-depth discussion if you think it's worth exploring - defintely could use @KPrasch here.

That being said, perhaps we could also think about the best inheritance for these classes - eg. is it possible for some exceptions to subclass ValueError instead of Exception where appropriate eg. InvalidShareNumberParameter or InvalidPvssTranscript? Perhaps other logical sub-class groupings make sense.

Then it can potentially be as general or granular as we like in Python - we either check for the superclass or the more specific exception type as needed. The granular work is already done in ferveo in this PR.

Comment on lines +103 to +119
create_exception!(exceptions, ThresholdEncryptionError, PyException);
create_exception!(exceptions, InvalidShareNumberParameter, PyValueError);
create_exception!(exceptions, InvalidDkgStateToDeal, PyRuntimeError);
create_exception!(exceptions, InvalidDkgStateToAggregate, PyRuntimeError);
create_exception!(exceptions, InvalidDkgStateToVerify, PyRuntimeError);
create_exception!(exceptions, InvalidDkgStateToIngest, PyRuntimeError);
create_exception!(exceptions, DealerNotInValidatorSet, PyValueError);
create_exception!(exceptions, UnknownDealer, PyValueError);
create_exception!(exceptions, DuplicateDealer, PyValueError);
create_exception!(exceptions, InvalidPvssTranscript, PyValueError);
create_exception!(exceptions, InsufficientTranscriptsForAggregate, PyException);
create_exception!(exceptions, InvalidDkgPublicKey, PyValueError);
create_exception!(exceptions, InsufficientValidators, PyValueError);
create_exception!(exceptions, InvalidTranscriptAggregate, PyValueError);
create_exception!(exceptions, ValidatorsNotSorted, PyValueError);
create_exception!(exceptions, ValidatorPublicKeyMismatch, PyValueError);
create_exception!(exceptions, SerializationError, PyValueError);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated these exceptions to inherit from certain Python exceptions

Comment on lines +205 to +218
class InvalidDkgStateToDeal(Exception):
pass


class InvalidDkgStateToAggregate(Exception):
pass


class InvalidDkgStateToVerify(Exception):
pass


class InvalidDkgStateToIngest(Exception):
pass
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from the use of Python base exceptions eg. ValueError (thanks for adding that 👍 ), my other thought there was around other logical groupings. For example, this group of exceptions can all potentially subclass a common parent.

So for these highlighted exceptions something like the following for example:

Suggested change
class InvalidDkgStateToDeal(Exception):
pass
class InvalidDkgStateToAggregate(Exception):
pass
class InvalidDkgStateToVerify(Exception):
pass
class InvalidDkgStateToIngest(Exception):
pass
class InvalidDkgStateException(Exception):
pass
class InvalidDkgStateToDeal(InvalidDkgStateException):
pass
class InvalidDkgStateToAggregate(InvalidDkgStateException):
pass
class InvalidDkgStateToVerify(InvalidDkgStateException):
pass
class InvalidDkgStateToIngest(InvalidDkgStateException):
pass

Perhaps something similar can be done for the Dealer exceptions below where the base class is

class DealerException(Exception):
    pass

and the other Dealer Exceptions subclass it.

Perhaps there are other similar groupings that can be done. Would these additional Iogical groupings also make sense for what we are trying to achieve? wdyt?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense, but I don't know if it makes those exceptions more actionable.

For example, InvalidDkgStateToDeal and InvalidDkgStateToAggregate would materialize in completely different situations, so catching InvalidDkgStateException and then trying to distinguish them based on that wouldn't make a lot of sense from my perspective. I would probably just try to catch InvalidDkgStateToAggregate directly.

But maybe there exists a grouping that makes perfect sense, I just don't see one yet. Maybe we could try using those exceptions as they are and see if we learn something from integrating them in nucypher. I can always add these later in another PR.

@piotr-roslaniec piotr-roslaniec merged commit 87d8f1c into nucypher:main Jun 2, 2023
@piotr-roslaniec piotr-roslaniec deleted the python-exceptions branch June 2, 2023 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle ferveo exceptions using a native error

4 participants