Skip to content

Commit c0da775

Browse files
truschweichweich
andauthored
feat: harmonize dispatch error names (#441)
## 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>
1 parent 1cb6ffa commit c0da775

File tree

20 files changed

+355
-292
lines changed

20 files changed

+355
-292
lines changed

pallet_error_naming_convention.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Pallet Error Naming Conventions
2+
3+
1) Use capitalized camel case in the variant names. For example, instead of using "NOT_FOUND" you should use "NotFound".
4+
5+
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.
6+
7+
3) Avoid using the pallet name in the variant names. For example instead of "Web3NameNotFound" you should use "NotFound".
8+
9+
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.
10+
11+
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.

pallets/attestation/src/lib.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,12 @@ pub mod pallet {
195195
/// The attestation has already been revoked.
196196
AlreadyRevoked,
197197
/// No attestation on chain matching the claim hash.
198-
AttestationNotFound,
198+
NotFound,
199199
/// The attestation CType does not match the CType specified in the
200200
/// delegation hierarchy root.
201201
CTypeMismatch,
202202
/// The call origin is not authorized to change the attestation.
203-
Unauthorized,
203+
NotAuthorized,
204204
/// The maximum number of delegated attestations has already been
205205
/// reached for the corresponding delegation id such that another one
206206
/// cannot be added.
@@ -248,7 +248,7 @@ pub mod pallet {
248248

249249
ensure!(
250250
ctype::Ctypes::<T>::contains_key(ctype_hash),
251-
ctype::Error::<T>::CTypeNotFound
251+
ctype::Error::<T>::NotFound
252252
);
253253
ensure!(
254254
!Attestations::<T>::contains_key(claim_hash),
@@ -317,13 +317,13 @@ pub mod pallet {
317317
let source = <T as Config>::EnsureOrigin::ensure_origin(origin)?;
318318
let who = source.subject();
319319

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

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

324324
if attestation.attester != who {
325-
let attestation_auth_id = attestation.authorization_id.as_ref().ok_or(Error::<T>::Unauthorized)?;
326-
authorization.ok_or(Error::<T>::Unauthorized)?.can_revoke(
325+
let attestation_auth_id = attestation.authorization_id.as_ref().ok_or(Error::<T>::NotAuthorized)?;
326+
authorization.ok_or(Error::<T>::NotAuthorized)?.can_revoke(
327327
&who,
328328
&attestation.ctype_hash,
329329
&claim_hash,
@@ -377,11 +377,11 @@ pub mod pallet {
377377
let source = <T as Config>::EnsureOrigin::ensure_origin(origin)?;
378378
let who = source.subject();
379379

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

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

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

419419
// *** No Fail beyond this point ***
420420

@@ -440,8 +440,8 @@ pub mod pallet {
440440
let subject = source.subject();
441441
let sender = source.sender();
442442

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

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

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

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

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

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

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

500500
Ok(())

pallets/attestation/src/tests.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fn test_attest_ctype_not_found() {
136136
ctype_hash,
137137
None
138138
),
139-
ctype::Error::<Test>::CTypeNotFound
139+
ctype::Error::<Test>::NotFound
140140
);
141141
});
142142
}
@@ -272,7 +272,7 @@ fn test_revoke_not_found() {
272272
claim_hash,
273273
authorization_info
274274
),
275-
attestation::Error::<Test>::AttestationNotFound
275+
attestation::Error::<Test>::NotFound
276276
);
277277
});
278278
}
@@ -374,7 +374,7 @@ fn test_remove_unauthorized() {
374374
claim_hash,
375375
authorization_info
376376
),
377-
attestation::Error::<Test>::Unauthorized
377+
attestation::Error::<Test>::NotAuthorized
378378
);
379379
});
380380
}
@@ -393,7 +393,7 @@ fn test_remove_not_found() {
393393
assert!(Balances::reserved_balance(ACCOUNT_00).is_zero());
394394
assert_noop!(
395395
Attestation::remove(DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash, None),
396-
attestation::Error::<Test>::AttestationNotFound
396+
attestation::Error::<Test>::NotFound
397397
);
398398
});
399399
}
@@ -465,7 +465,7 @@ fn test_reclaim_unauthorized() {
465465
.execute_with(|| {
466466
assert_noop!(
467467
Attestation::reclaim_deposit(RuntimeOrigin::signed(ACCOUNT_01), claim_hash),
468-
attestation::Error::<Test>::Unauthorized,
468+
attestation::Error::<Test>::NotAuthorized,
469469
);
470470
});
471471
}
@@ -483,7 +483,7 @@ fn test_reclaim_deposit_not_found() {
483483
.execute_with(|| {
484484
assert_noop!(
485485
Attestation::reclaim_deposit(RuntimeOrigin::signed(ACCOUNT_01), claim_hash),
486-
attestation::Error::<Test>::AttestationNotFound,
486+
attestation::Error::<Test>::NotFound,
487487
);
488488
});
489489
}
@@ -564,7 +564,7 @@ fn test_change_deposit_owner_unauthorized() {
564564
.execute_with(|| {
565565
assert_noop!(
566566
Attestation::change_deposit_owner(DoubleOrigin(ACCOUNT_00, evil_actor).into(), claim_hash),
567-
attestation::Error::<Test>::Unauthorized,
567+
attestation::Error::<Test>::NotAuthorized,
568568
);
569569
});
570570
}
@@ -582,7 +582,7 @@ fn test_change_deposit_owner_not_found() {
582582
.execute_with(|| {
583583
assert_noop!(
584584
Attestation::change_deposit_owner(DoubleOrigin(ACCOUNT_00, attester).into(), claim_hash),
585-
attestation::Error::<Test>::AttestationNotFound,
585+
attestation::Error::<Test>::NotFound,
586586
);
587587
});
588588
}
@@ -650,7 +650,7 @@ fn test_update_deposit_unauthorized() {
650650
);
651651
assert_noop!(
652652
Attestation::update_deposit(RuntimeOrigin::signed(ACCOUNT_01), claim_hash),
653-
Error::<Test>::Unauthorized
653+
Error::<Test>::NotAuthorized
654654
);
655655
});
656656
}

pallets/ctype/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ pub mod pallet {
131131
#[pallet::error]
132132
pub enum Error<T> {
133133
/// There is no CType with the given hash.
134-
CTypeNotFound,
134+
NotFound,
135135
/// The CType already exists.
136-
CTypeAlreadyExists,
136+
AlreadyExists,
137137
/// The paying account was unable to pay the fees for creating a ctype.
138138
UnableToPayFees,
139139
}
@@ -171,7 +171,7 @@ pub mod pallet {
171171

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

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

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

@@ -216,7 +216,7 @@ pub mod pallet {
216216
ctype_entry.created_at = block_number;
217217
Ok(())
218218
} else {
219-
Err(Error::<T>::CTypeNotFound)
219+
Err(Error::<T>::NotFound)
220220
}
221221
})?;
222222

pallets/ctype/src/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn check_duplicate_ctype_creation() {
8787
.execute_with(|| {
8888
assert_noop!(
8989
Ctype::add(DoubleOrigin(deposit_owner, creator).into(), ctype),
90-
ctype::Error::<Test>::CTypeAlreadyExists
90+
ctype::Error::<Test>::AlreadyExists
9191
);
9292
});
9393
}
@@ -127,7 +127,7 @@ fn set_block_number_ctype_not_found() {
127127
ExtBuilder::default().build().execute_with(|| {
128128
assert_noop!(
129129
Ctype::set_block_number(RawOrigin::Signed(ACCOUNT_00).into(), ctype_hash, 100u64),
130-
ctype::Error::<Test>::CTypeNotFound
130+
ctype::Error::<Test>::NotFound
131131
);
132132
})
133133
}

pallets/delegation/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ pub mod pallet {
280280
/// The max number of parent checks exceeds the limit for the pallet.
281281
MaxParentChecksTooLarge,
282282
/// An error that is not supposed to take place, yet it happened.
283-
InternalError,
283+
Internal,
284284
/// The max number of all children has been reached for the
285285
/// corresponding delegation node.
286286
MaxChildrenExceeded,
@@ -331,7 +331,7 @@ pub mod pallet {
331331

332332
ensure!(
333333
<ctype::Ctypes<T>>::contains_key(ctype_hash),
334-
<ctype::Error<T>>::CTypeNotFound
334+
<ctype::Error<T>>::NotFound
335335
);
336336

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

pallets/delegation/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn ctype_not_found_create_root_delegation_error() {
114114
operation.id,
115115
operation.ctype_hash
116116
),
117-
ctype::Error::<Test>::CTypeNotFound
117+
ctype::Error::<Test>::NotFound
118118
);
119119
});
120120
}

0 commit comments

Comments
 (0)