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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
eaa9cf7
harmonize dispatch error names, so its always NotFound for example or…
trusch Dec 8, 2022
e6eb59c
cargo fmt
trusch Dec 9, 2022
b274b12
make clippy happy with the tests
trusch Dec 9, 2022
788191c
refactored DID errors + one forgoten Unauthorized in attestation pallet.
trusch Jan 5, 2023
d487944
cargo fmt
trusch Jan 5, 2023
7df38bb
cargo clippy --fix
trusch Jan 5, 2023
d9eb328
qualify errors::Input in service_endpoints code
trusch Jan 11, 2023
8188d89
unify did related not-found errors.
trusch Jan 12, 2023
26922f2
cargo fmt
trusch Jan 12, 2023
881abe8
update comments of did::errors::Error variants.
trusch Jan 13, 2023
aa341c1
further unify not found errors
trusch Jan 13, 2023
3fa7430
cargo fmt
trusch Jan 13, 2023
c7d99be
apply clippy suggestion
trusch Jan 13, 2023
77d674a
cargo fmt
trusch Jan 13, 2023
33185e0
Merge branch 'develop' into feature/harmonize-dispatch-errors
trusch Jan 13, 2023
ce7fd6c
Merge branch 'develop' into feature/harmonize-dispatch-errors
trusch Feb 9, 2023
d8af561
bring back the Error suffix in the did::errors module
trusch Feb 9, 2023
4ff8e68
Merge branch 'develop' into feature/harmonize-dispatch-errors
trusch Feb 9, 2023
4b3cc82
fix test
trusch Feb 13, 2023
4981ba4
add pallet error naning convention doc
trusch Feb 17, 2023
563f4e8
Merge branch 'develop' into feature/harmonize-dispatch-errors
trusch Feb 23, 2023
d55cdb0
cargo fmt
trusch Feb 28, 2023
3b558d3
add examples for common vocab
trusch Feb 28, 2023
66e5f8b
Merge branch 'develop' into feature/harmonize-dispatch-errors
weichweich Feb 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pallet_error_naming_convention.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Pallet Error Naming Conventions

1) Use capitalized camel case in the variant names. For example, instead of using "NOT_FOUND" you should use "NotFound".

2) Avoid using the word "error" as a suffix for the variant names. For example, instead of using "NotFoundError" you should use "NotFound". It's clear from the caller's context that this is an error.

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". Common vocabulary includes: NotFound, NotAuthorized, AlreadyExists, MaxXYZExceeded.

5) Use descriptive and concise names for the variants. Avoid using abbreviations or acronyms unless they are widely recognized and understood by other developers who may be working on the codebase.
34 changes: 17 additions & 17 deletions pallets/attestation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ pub mod pallet {
/// The attestation has already been revoked.
AlreadyRevoked,
/// No attestation on chain matching the claim hash.
AttestationNotFound,
NotFound,
/// The attestation CType does not match the CType specified in the
/// delegation hierarchy root.
CTypeMismatch,
/// The call origin is not authorized to change the attestation.
Unauthorized,
NotAuthorized,
/// The maximum number of delegated attestations has already been
/// reached for the corresponding delegation id such that another one
/// cannot be added.
Expand Down Expand Up @@ -248,7 +248,7 @@ pub mod pallet {

ensure!(
ctype::Ctypes::<T>::contains_key(ctype_hash),
ctype::Error::<T>::CTypeNotFound
ctype::Error::<T>::NotFound
);
ensure!(
!Attestations::<T>::contains_key(claim_hash),
Expand Down Expand Up @@ -317,13 +317,13 @@ pub mod pallet {
let source = <T as Config>::EnsureOrigin::ensure_origin(origin)?;
let who = source.subject();

let attestation = Attestations::<T>::get(claim_hash).ok_or(Error::<T>::AttestationNotFound)?;
let attestation = Attestations::<T>::get(claim_hash).ok_or(Error::<T>::NotFound)?;

ensure!(!attestation.revoked, Error::<T>::AlreadyRevoked);

if attestation.attester != who {
let attestation_auth_id = attestation.authorization_id.as_ref().ok_or(Error::<T>::Unauthorized)?;
authorization.ok_or(Error::<T>::Unauthorized)?.can_revoke(
let attestation_auth_id = attestation.authorization_id.as_ref().ok_or(Error::<T>::NotAuthorized)?;
authorization.ok_or(Error::<T>::NotAuthorized)?.can_revoke(
&who,
&attestation.ctype_hash,
&claim_hash,
Expand Down Expand Up @@ -377,11 +377,11 @@ pub mod pallet {
let source = <T as Config>::EnsureOrigin::ensure_origin(origin)?;
let who = source.subject();

let attestation = Attestations::<T>::get(claim_hash).ok_or(Error::<T>::AttestationNotFound)?;
let attestation = Attestations::<T>::get(claim_hash).ok_or(Error::<T>::NotFound)?;

if attestation.attester != who {
let attestation_auth_id = attestation.authorization_id.as_ref().ok_or(Error::<T>::Unauthorized)?;
authorization.ok_or(Error::<T>::Unauthorized)?.can_remove(
let attestation_auth_id = attestation.authorization_id.as_ref().ok_or(Error::<T>::NotAuthorized)?;
authorization.ok_or(Error::<T>::NotAuthorized)?.can_remove(
&who,
&attestation.ctype_hash,
&claim_hash,
Expand Down Expand Up @@ -412,9 +412,9 @@ pub mod pallet {
#[pallet::weight(<T as pallet::Config>::WeightInfo::reclaim_deposit())]
pub fn reclaim_deposit(origin: OriginFor<T>, claim_hash: ClaimHashOf<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
let attestation = Attestations::<T>::get(claim_hash).ok_or(Error::<T>::AttestationNotFound)?;
let attestation = Attestations::<T>::get(claim_hash).ok_or(Error::<T>::NotFound)?;

ensure!(attestation.deposit.owner == who, Error::<T>::Unauthorized);
ensure!(attestation.deposit.owner == who, Error::<T>::NotAuthorized);

// *** No Fail beyond this point ***

Expand All @@ -440,8 +440,8 @@ pub mod pallet {
let subject = source.subject();
let sender = source.sender();

let attestation = Attestations::<T>::get(claim_hash).ok_or(Error::<T>::AttestationNotFound)?;
ensure!(attestation.attester == subject, Error::<T>::Unauthorized);
let attestation = Attestations::<T>::get(claim_hash).ok_or(Error::<T>::NotFound)?;
ensure!(attestation.attester == subject, Error::<T>::NotAuthorized);

AttestationStorageDepositCollector::<T>::change_deposit_owner(&claim_hash, sender)?;

Expand All @@ -456,8 +456,8 @@ pub mod pallet {
pub fn update_deposit(origin: OriginFor<T>, claim_hash: ClaimHashOf<T>) -> DispatchResult {
let sender = ensure_signed(origin)?;

let attestation = Attestations::<T>::get(claim_hash).ok_or(Error::<T>::AttestationNotFound)?;
ensure!(attestation.deposit.owner == sender, Error::<T>::Unauthorized);
let attestation = Attestations::<T>::get(claim_hash).ok_or(Error::<T>::NotFound)?;
ensure!(attestation.deposit.owner == sender, Error::<T>::NotAuthorized);

AttestationStorageDepositCollector::<T>::update_deposit(&claim_hash)?;

Expand All @@ -482,7 +482,7 @@ pub mod pallet {
fn deposit(
key: &ClaimHashOf<T>,
) -> Result<Deposit<AccountIdOf<T>, <Self::Currency as Currency<AccountIdOf<T>>>::Balance>, DispatchError> {
let attestation = Attestations::<T>::get(key).ok_or(Error::<T>::AttestationNotFound)?;
let attestation = Attestations::<T>::get(key).ok_or(Error::<T>::NotFound)?;
Ok(attestation.deposit)
}

Expand All @@ -494,7 +494,7 @@ pub mod pallet {
key: &ClaimHashOf<T>,
deposit: Deposit<AccountIdOf<T>, <Self::Currency as Currency<AccountIdOf<T>>>::Balance>,
) -> Result<(), DispatchError> {
let attestation = Attestations::<T>::get(key).ok_or(Error::<T>::AttestationNotFound)?;
let attestation = Attestations::<T>::get(key).ok_or(Error::<T>::NotFound)?;
Attestations::<T>::insert(key, AttestationDetails { deposit, ..attestation });

Ok(())
Expand Down
18 changes: 9 additions & 9 deletions pallets/attestation/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn test_attest_ctype_not_found() {
ctype_hash,
None
),
ctype::Error::<Test>::CTypeNotFound
ctype::Error::<Test>::NotFound
);
});
}
Expand Down Expand Up @@ -272,7 +272,7 @@ fn test_revoke_not_found() {
claim_hash,
authorization_info
),
attestation::Error::<Test>::AttestationNotFound
attestation::Error::<Test>::NotFound
);
});
}
Expand Down Expand Up @@ -374,7 +374,7 @@ fn test_remove_unauthorized() {
claim_hash,
authorization_info
),
attestation::Error::<Test>::Unauthorized
attestation::Error::<Test>::NotAuthorized
);
});
}
Expand All @@ -393,7 +393,7 @@ fn test_remove_not_found() {
assert!(Balances::reserved_balance(ACCOUNT_00).is_zero());
assert_noop!(
Attestation::remove(DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash, None),
attestation::Error::<Test>::AttestationNotFound
attestation::Error::<Test>::NotFound
);
});
}
Expand Down Expand Up @@ -465,7 +465,7 @@ fn test_reclaim_unauthorized() {
.execute_with(|| {
assert_noop!(
Attestation::reclaim_deposit(RuntimeOrigin::signed(ACCOUNT_01), claim_hash),
attestation::Error::<Test>::Unauthorized,
attestation::Error::<Test>::NotAuthorized,
);
});
}
Expand All @@ -483,7 +483,7 @@ fn test_reclaim_deposit_not_found() {
.execute_with(|| {
assert_noop!(
Attestation::reclaim_deposit(RuntimeOrigin::signed(ACCOUNT_01), claim_hash),
attestation::Error::<Test>::AttestationNotFound,
attestation::Error::<Test>::NotFound,
);
});
}
Expand Down Expand Up @@ -564,7 +564,7 @@ fn test_change_deposit_owner_unauthorized() {
.execute_with(|| {
assert_noop!(
Attestation::change_deposit_owner(DoubleOrigin(ACCOUNT_00, evil_actor).into(), claim_hash),
attestation::Error::<Test>::Unauthorized,
attestation::Error::<Test>::NotAuthorized,
);
});
}
Expand All @@ -582,7 +582,7 @@ fn test_change_deposit_owner_not_found() {
.execute_with(|| {
assert_noop!(
Attestation::change_deposit_owner(DoubleOrigin(ACCOUNT_00, attester).into(), claim_hash),
attestation::Error::<Test>::AttestationNotFound,
attestation::Error::<Test>::NotFound,
);
});
}
Expand Down Expand Up @@ -650,7 +650,7 @@ fn test_update_deposit_unauthorized() {
);
assert_noop!(
Attestation::update_deposit(RuntimeOrigin::signed(ACCOUNT_01), claim_hash),
Error::<Test>::Unauthorized
Error::<Test>::NotAuthorized
);
});
}
8 changes: 4 additions & 4 deletions pallets/ctype/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ pub mod pallet {
#[pallet::error]
pub enum Error<T> {
/// There is no CType with the given hash.
CTypeNotFound,
NotFound,
/// The CType already exists.
CTypeAlreadyExists,
AlreadyExists,
/// The paying account was unable to pay the fees for creating a ctype.
UnableToPayFees,
}
Expand Down Expand Up @@ -171,7 +171,7 @@ pub mod pallet {

let hash = <T as frame_system::Config>::Hashing::hash(&ctype[..]);

ensure!(!Ctypes::<T>::contains_key(hash), Error::<T>::CTypeAlreadyExists);
ensure!(!Ctypes::<T>::contains_key(hash), Error::<T>::AlreadyExists);

// *** No Fail except during withdraw beyond this point ***

Expand Down Expand Up @@ -216,7 +216,7 @@ pub mod pallet {
ctype_entry.created_at = block_number;
Ok(())
} else {
Err(Error::<T>::CTypeNotFound)
Err(Error::<T>::NotFound)
}
})?;

Expand Down
4 changes: 2 additions & 2 deletions pallets/ctype/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn check_duplicate_ctype_creation() {
.execute_with(|| {
assert_noop!(
Ctype::add(DoubleOrigin(deposit_owner, creator).into(), ctype),
ctype::Error::<Test>::CTypeAlreadyExists
ctype::Error::<Test>::AlreadyExists
);
});
}
Expand Down Expand Up @@ -127,7 +127,7 @@ fn set_block_number_ctype_not_found() {
ExtBuilder::default().build().execute_with(|| {
assert_noop!(
Ctype::set_block_number(RawOrigin::Signed(ACCOUNT_00).into(), ctype_hash, 100u64),
ctype::Error::<Test>::CTypeNotFound
ctype::Error::<Test>::NotFound
);
})
}
Expand Down
4 changes: 2 additions & 2 deletions pallets/delegation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ pub mod pallet {
/// The max number of parent checks exceeds the limit for the pallet.
MaxParentChecksTooLarge,
/// An error that is not supposed to take place, yet it happened.
InternalError,
Internal,
/// The max number of all children has been reached for the
/// corresponding delegation node.
MaxChildrenExceeded,
Expand Down Expand Up @@ -331,7 +331,7 @@ pub mod pallet {

ensure!(
<ctype::Ctypes<T>>::contains_key(ctype_hash),
<ctype::Error<T>>::CTypeNotFound
<ctype::Error<T>>::NotFound
);

// *** No Fail beyond this point ***
Expand Down
2 changes: 1 addition & 1 deletion pallets/delegation/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn ctype_not_found_create_root_delegation_error() {
operation.id,
operation.ctype_hash
),
ctype::Error::<Test>::CTypeNotFound
ctype::Error::<Test>::NotFound
);
});
}
Expand Down
Loading