Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: harmonize dispatch error names #441

Merged
merged 24 commits into from
Feb 28, 2023
Merged

Conversation

trusch
Copy link
Contributor

@trusch trusch commented Dec 8, 2022

fixes KILTProtocol/ticket#2290

This harmonizes the instance names in the Error enums of our pallets.

Before this change the names were kind of randomly choosen, so we had for example CTypeNotFound and DidNotPresent. Those names were bad for thwo reasons:

  1. They mean the same thing but use different wording
  2. They contain the module name

After this change both instances would be named NotFound.

All the other changes are something along those lines.

How to test:

Please provide a brief step-by-step instruction.
If necessary provide information about dependencies (specific configuration, branches, database dumps, etc.)

  • Step 1
  • Step 2
  • etc.

Checklist:

  • I have verified that the code works
    • No panics! (checked arithmetic ops, no indexing array[3] use get(3), ...)
  • I have verified that the code is easy to understand
    • If not, I have left a well-balanced amount of inline comments
  • I have left the code in a better state
  • I have documented the changes (where applicable)

@trusch trusch requested a review from weichweich December 8, 2022 13:39
Copy link
Contributor

@wischli wischli left a comment

Choose a reason for hiding this comment

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

More harmonization required. For a final statement, the document with all runtime existing KILT runtime errors is mandatory (assuming post PR state) as its too easy to oversee stuff.

pallets/pallet-web3-names/src/lib.rs Show resolved Hide resolved
pallets/pallet-web3-names/src/lib.rs Show resolved Hide resolved
pallets/did/src/lib.rs Show resolved Hide resolved
pallets/did/src/lib.rs Outdated Show resolved Hide resolved
@trusch trusch force-pushed the feature/harmonize-dispatch-errors branch from 2edb846 to 788191c Compare January 5, 2023 09:49
@trusch trusch marked this pull request as ready for review January 5, 2023 12:10
Copy link
Contributor

@weichweich weichweich left a comment

Choose a reason for hiding this comment

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

Can you adjust the PR description? You can remove the parts of the template that don't apply? Also there should be a doc with a description of the naming and conventions? Maybe you could commit that here as a md file?

}
}

/// Error involving the pallet's storage.
#[derive(Debug, Eq, PartialEq, TypeInfo)]
pub enum StorageError {
pub enum Storage {
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the reason to remove the Error suffix here? If I see an enum called Storage I'm not expected error codes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its part of the errors module, so its qualified name is did::errors::Storage which is beautiful. With the suffix it would be did::errors::StorageError.

Copy link
Contributor

Choose a reason for hiding this comment

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

did::errors::StorageError would only be used once to bring StorageError into scope. After that it's always only StorageError. So your proposal is errors::Storage vs StorageError. I personally think StorageError is better since you can't forget to have the error path. If you don't use errors::Storage but Storage this becomes very misleading.

/// The given DID does not contain the right key to verify the signature
/// of a DID operation.
DidKeyNotPresent(DidVerificationKeyRelationship),
DidKeyNotFound(DidVerificationKeyRelationship),
Copy link
Contributor

Choose a reason for hiding this comment

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

We have NotFound, KeyNotFound and DidKeyNotFound. They all sound very similar.

In case of DidKeyNotFound, we can't execute the call since the DID doesn't have the required VerificationKeyRelationship? I would interpret that as disallowing the call and rename it to CallNotAllowed or similar.
In case of NotFound and KeyNotFound I would keep DidNotFound as it reflects that this is specific for the Did. Best would probably be to have a NotFound(Resource) error, where the Resource is an enum that contains all the resources that could be missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think now its completely refactored to be in that NotFound(Resource) shape

@@ -84,7 +84,7 @@ pub enum SignatureError {
/// Error generated when some extrinsic input does not respect the pallet's
/// constraints.
#[derive(Debug, Eq, PartialEq, TypeInfo)]
pub enum InputError {
pub enum Input {
Copy link
Contributor

Choose a reason for hiding this comment

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

Name Input doesn't reflect that it's error codes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

same as above

}

/// Error generated when validating a DID operation.
#[derive(Debug, Eq, PartialEq, TypeInfo)]
pub enum SignatureError {
pub enum Signature {
Copy link
Contributor

Choose a reason for hiding this comment

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

Name Signature doesn't reflect that it's error codes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as with the name of did::errors::Storage, the module indicates that its all about error types

}

/// Error generated when validating a DID operation.
#[derive(Debug, Eq, PartialEq, TypeInfo)]
pub enum SignatureError {
pub enum Signature {
/// The signature is not in the format the verification key expects.
InvalidSignatureFormat,
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we know it's a signature error, we don't require the Signature in the error name? Same with InvalidSignature even though we might want to explicitly say that it's the signature part that is wrong and not the nonce part (we also have InvalidNonce).

Suggested change
InvalidSignatureFormat,
InvalidFormat,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the Signature part from the variants and changed from InvalidSignature case to InvalidData

@@ -59,48 +59,45 @@ pub struct DidEndpoint<T: Config> {
impl<T: Config> DidEndpoint<T> {
/// Validates a given [DidEndpoint] instance against the constraint
/// set in the pallet's [Config].
pub(crate) fn validate_against_constraints(&self) -> Result<(), InputError> {
pub(crate) fn validate_against_constraints(&self) -> Result<(), Input> {
Copy link
Contributor

Choose a reason for hiding this comment

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

If we would use the errors::Input path everywhere I would think that this change would make more sense. Seeing only Input there makes you think that the input is returned in case of an error.

Suggested change
pub(crate) fn validate_against_constraints(&self) -> Result<(), Input> {
pub(crate) fn validate_against_constraints(&self) -> Result<(), errors::Input> {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

pallets/pallet-web3-names/src/lib.rs Show resolved Hide resolved
Comment on lines 26 to 31
/// See [StorageError].
StorageError(StorageError),
Storage(Storage),
/// See [SignatureError].
SignatureError(SignatureError),
Signature(Signature),
/// See [InputError].
InputError(InputError),
Input(Input),
Copy link
Contributor

Choose a reason for hiding this comment

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

The comments need updating

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

@trusch trusch requested a review from weichweich January 13, 2023 08:27
Copy link
Contributor

@weichweich weichweich left a comment

Choose a reason for hiding this comment

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

@ntn-x2 What do you think about StorageError vs errors::Storage? Which one do you prefer?

SignatureError(SignatureError),
/// See [InputError].
InputError(InputError),
pub enum Error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Following the "no error suffixrule, this would beDidand notError`?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's the top-level Error type for DIDs, so calling this Error in the context of the did::error module kind of makes sense. I agree that this also violates the "no error suffix" rule since technically "Error" is a suffix of "Error", but this is really an edgecase. Naming it "Did" could make sense, but it feels counter-intuitive to me. Perhaps we should modify the no-error suffix rule somehow?

.delegation_key
.take()
.ok_or(errors::Storage::NotFound(errors::NotFoundKind::Key(
errors::KeyType::AssertionMethod,
Copy link
Contributor

Choose a reason for hiding this comment

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

from the name of the function this should be the delegation key?


/// Enum describing the different did key types.
#[derive(Debug, Eq, PartialEq, TypeInfo)]
pub enum KeyType {
Copy link
Contributor

Choose a reason for hiding this comment

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

Even tho this introduces another nesting level i would maybe reuse the DidVerificationKeyRelationship here.

pub enum KeyType {
 Verification(DidVerificationKeyRelationship),
 KeyAgreement,
}

You can than implement From<DidVerificationKeyRelationship> for KeyType or maybe even derive that and From<KeyType> for NotFoundKind

When creating an error gets too deeply nested you can also add a helper function to StorageError.

@ntn-x2
Copy link
Member

ntn-x2 commented Jan 13, 2023

@ntn-x2 What do you think about StorageError vs errors::Storage? Which one do you prefer?

My two cents are that types can also be imported directly in Rust. This means that I could do use did::errors::Storage, and then use Storage directly. This could be confusing. That's why I would leave error names with the Error prefix, so that even by fully importing the type and not its enclosing module, we don't have any ambiguities.

@trusch
Copy link
Contributor Author

trusch commented Feb 9, 2023

I reverted the removal of the Error suffix for the error types in the did::errors module.

@ntn-x2 ntn-x2 self-requested a review February 10, 2023 10:51
@ntn-x2
Copy link
Member

ntn-x2 commented Feb 10, 2023

@trusch please fix the CI errors, thanks!


3) Avoid using the pallet name in the variant names. For example instead of "Web3NameNotFound" you should use "NotFound".

4) Try to take words from the vocabulary already defined in the code base. For example instead of introducing a new variant "NotExisting" you should use, once again, "NotFound"
Copy link
Contributor

Choose a reason for hiding this comment

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

could you list the vocabulary here?
e.g. NotAuthorized, NotFound, AlreadyExists, MaxXYZExceeded (thats all? i guess...)

@trusch trusch enabled auto-merge (squash) February 28, 2023 08:45
@trusch trusch merged commit c0da775 into develop Feb 28, 2023
@trusch trusch deleted the feature/harmonize-dispatch-errors branch February 28, 2023 09:19
Ad96el pushed a commit that referenced this pull request Mar 20, 2023
## fixes KILTprotocol/ticket#2290

This harmonizes the instance names in the Error enums of our pallets.

Before this change the names were kind of randomly choosen, so we had
for example CTypeNotFound and DidNotPresent. Those names were bad for
thwo reasons:

1) They mean the same thing but use different wording
2) They contain the module name

After this change both instances would be named NotFound.

All the other changes are something along those lines.

Co-authored-by: Albrecht <albrecht@kilt.io>
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.

4 participants