Skip to content

Commit 1eea565

Browse files
authored
specifying errors (#243)
1 parent 4839435 commit 1eea565

File tree

7 files changed

+32
-15
lines changed

7 files changed

+32
-15
lines changed

program/rust/src/error.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ pub enum OracleError {
2020
UnknownCError = 602,
2121
#[error("UnrecognizedInstruction")]
2222
UnrecognizedInstruction = 603,
23+
#[error("InvalidFundingAccount")]
24+
InvalidFundingAccount = 604,
25+
#[error("InvalidSignableAccount")]
26+
InvalidSignableAccount = 605,
27+
#[error("InvalidSystemAccount")]
28+
InvalidSystemAccount = 606,
29+
#[error("InvalidWritableAccount")]
30+
InvalidWritableAccount = 607,
31+
#[error("InvalidWritableAccount")]
32+
InvalidFreshAccount = 608,
2333
}
2434

2535
impl From<OracleError> for ProgramError {

program/rust/src/rust_oracle.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ fn valid_funding_account(account: &AccountInfo) -> bool {
356356
fn check_valid_funding_account(account: &AccountInfo) -> Result<(), ProgramError> {
357357
pyth_assert(
358358
valid_funding_account(account),
359-
ProgramError::InvalidArgument,
359+
OracleError::InvalidFundingAccount.into(),
360360
)
361361
}
362362

@@ -375,7 +375,7 @@ fn check_valid_signable_account(
375375
) -> Result<(), ProgramError> {
376376
pyth_assert(
377377
valid_signable_account(program_id, account, minimum_size),
378-
ProgramError::InvalidArgument,
378+
OracleError::InvalidSignableAccount.into(),
379379
)
380380
}
381381

@@ -390,7 +390,10 @@ fn valid_fresh_account(account: &AccountInfo) -> bool {
390390
}
391391

392392
fn check_valid_fresh_account(account: &AccountInfo) -> Result<(), ProgramError> {
393-
pyth_assert(valid_fresh_account(account), ProgramError::InvalidArgument)
393+
pyth_assert(
394+
valid_fresh_account(account),
395+
OracleError::InvalidFreshAccount.into(),
396+
)
394397
}
395398

396399
/// Sets the data of account to all-zero

program/rust/src/tests/test_add_price.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::error::OracleError;
12
use crate::tests::test_utils::AccountSetup;
23
use bytemuck::bytes_of;
34
use solana_program::program_error::ProgramError;
@@ -146,7 +147,7 @@ fn test_add_price() {
146147
],
147148
instruction_data_add_price
148149
),
149-
Err(ProgramError::InvalidArgument)
150+
Err(OracleError::InvalidFreshAccount.into())
150151
);
151152

152153
clear_account(&price_account).unwrap();
@@ -195,7 +196,7 @@ fn test_add_price() {
195196
],
196197
instruction_data_add_price
197198
),
198-
Err(ProgramError::InvalidArgument)
199+
Err(OracleError::InvalidSignableAccount.into())
199200
);
200201

201202
// Fresh product account

program/rust/src/tests/test_add_product.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::mem::size_of;
22

3+
use crate::error::OracleError;
34
use crate::tests::test_utils::AccountSetup;
45
use bytemuck::bytes_of;
56
use solana_program::account_info::AccountInfo;
@@ -123,7 +124,7 @@ fn test_add_product() {
123124
],
124125
instruction_data
125126
),
126-
Err(ProgramError::InvalidArgument)
127+
Err(OracleError::InvalidSignableAccount.into())
127128
);
128129

129130
// test fill up of mapping table

program/rust/src/tests/test_add_publisher.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::rust_oracle::{
2323
load_checked,
2424
pubkey_equal,
2525
};
26+
use crate::OracleError;
2627

2728
#[test]
2829
fn test_add_publisher() {
@@ -53,7 +54,7 @@ fn test_add_publisher() {
5354
&[funding_account.clone(), price_account.clone(),],
5455
instruction_data
5556
),
56-
Err(ProgramError::InvalidArgument)
57+
Err(OracleError::InvalidSignableAccount.into())
5758
);
5859

5960
// Now give the price account enough lamports to be rent exempt

program/rust/src/tests/test_init_mapping.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::c_oracle_header::{
77
PC_VERSION,
88
};
99
use crate::deserialize::load_account_as;
10+
use crate::error::OracleError;
1011
use crate::rust_oracle::{
1112
clear_account,
1213
init_mapping,
@@ -57,7 +58,7 @@ fn test_init_mapping() {
5758
&[funding_account.clone(), mapping_account.clone()],
5859
instruction_data
5960
),
60-
Err(ProgramError::InvalidArgument)
61+
Err(OracleError::InvalidFreshAccount.into())
6162
);
6263

6364
clear_account(&mapping_account).unwrap();
@@ -75,7 +76,7 @@ fn test_init_mapping() {
7576
&[funding_account.clone(), mapping_account.clone()],
7677
instruction_data
7778
),
78-
Err(ProgramError::InvalidArgument)
79+
Err(OracleError::InvalidFundingAccount.into())
7980
);
8081

8182
funding_account.is_signer = true;
@@ -87,7 +88,7 @@ fn test_init_mapping() {
8788
&[funding_account.clone(), mapping_account.clone()],
8889
instruction_data
8990
),
90-
Err(ProgramError::InvalidArgument)
91+
Err(OracleError::InvalidSignableAccount.into())
9192
);
9293

9394
mapping_account.is_signer = true;
@@ -99,7 +100,7 @@ fn test_init_mapping() {
99100
&[funding_account.clone(), mapping_account.clone()],
100101
instruction_data
101102
),
102-
Err(ProgramError::InvalidArgument)
103+
Err(OracleError::InvalidFundingAccount.into())
103104
);
104105

105106
funding_account.is_writable = true;
@@ -111,7 +112,7 @@ fn test_init_mapping() {
111112
&[funding_account.clone(), mapping_account.clone()],
112113
instruction_data
113114
),
114-
Err(ProgramError::InvalidArgument)
115+
Err(OracleError::InvalidSignableAccount.into())
115116
);
116117

117118
mapping_account.is_writable = true;
@@ -123,7 +124,7 @@ fn test_init_mapping() {
123124
&[funding_account.clone(), mapping_account.clone()],
124125
instruction_data
125126
),
126-
Err(ProgramError::InvalidArgument)
127+
Err(OracleError::InvalidSignableAccount.into())
127128
);
128129

129130
mapping_account.owner = &program_id;
@@ -136,7 +137,7 @@ fn test_init_mapping() {
136137
&[funding_account.clone(), mapping_account.clone()],
137138
instruction_data
138139
),
139-
Err(ProgramError::InvalidArgument)
140+
Err(OracleError::InvalidSignableAccount.into())
140141
);
141142

142143
mapping_account.data = prev_data;

program/rust/src/tests/test_upd_product.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::mem::size_of;
22

3+
use crate::rust_oracle::try_convert;
34
use crate::tests::test_utils::AccountSetup;
45
use solana_program::account_info::AccountInfo;
56
use solana_program::program_error::ProgramError;
@@ -19,7 +20,6 @@ use crate::rust_oracle::{
1920
initialize_checked,
2021
load_checked,
2122
read_pc_str_t,
22-
try_convert,
2323
upd_product,
2424
};
2525

0 commit comments

Comments
 (0)