@@ -250,6 +250,57 @@ impl From<ProgramError> for u64 {
250
250
}
251
251
}
252
252
253
+ /// A trait for converting a program error to a `&str`.
254
+ pub trait ToStr {
255
+ fn to_str < E > ( & self ) -> & ' static str
256
+ where
257
+ E : ' static + ToStr + TryFrom < u32 > ;
258
+ }
259
+
260
+ impl ToStr for ProgramError {
261
+ fn to_str < E > ( & self ) -> & ' static str
262
+ where
263
+ E : ' static + ToStr + TryFrom < u32 > ,
264
+ {
265
+ match self {
266
+ Self :: Custom ( error) => {
267
+ if let Ok ( custom_error) = E :: try_from ( * error) {
268
+ custom_error. to_str :: < E > ( )
269
+ } else {
270
+ "Error: Unknown"
271
+ }
272
+ }
273
+ Self :: InvalidArgument => "Error: InvalidArgument" ,
274
+ Self :: InvalidInstructionData => "Error: InvalidInstructionData" ,
275
+ Self :: InvalidAccountData => "Error: InvalidAccountData" ,
276
+ Self :: AccountDataTooSmall => "Error: AccountDataTooSmall" ,
277
+ Self :: InsufficientFunds => "Error: InsufficientFunds" ,
278
+ Self :: IncorrectProgramId => "Error: IncorrectProgramId" ,
279
+ Self :: MissingRequiredSignature => "Error: MissingRequiredSignature" ,
280
+ Self :: AccountAlreadyInitialized => "Error: AccountAlreadyInitialized" ,
281
+ Self :: UninitializedAccount => "Error: UninitializedAccount" ,
282
+ Self :: NotEnoughAccountKeys => "Error: NotEnoughAccountKeys" ,
283
+ Self :: AccountBorrowFailed => "Error: AccountBorrowFailed" ,
284
+ Self :: MaxSeedLengthExceeded => "Error: MaxSeedLengthExceeded" ,
285
+ Self :: InvalidSeeds => "Error: InvalidSeeds" ,
286
+ Self :: BorshIoError => "Error: BorshIoError" ,
287
+ Self :: AccountNotRentExempt => "Error: AccountNotRentExempt" ,
288
+ Self :: UnsupportedSysvar => "Error: UnsupportedSysvar" ,
289
+ Self :: IllegalOwner => "Error: IllegalOwner" ,
290
+ Self :: MaxAccountsDataAllocationsExceeded => "Error: MaxAccountsDataAllocationsExceeded" ,
291
+ Self :: InvalidRealloc => "Error: InvalidRealloc" ,
292
+ Self :: MaxInstructionTraceLengthExceeded => "Error: MaxInstructionTraceLengthExceeded" ,
293
+ Self :: BuiltinProgramsMustConsumeComputeUnits => {
294
+ "Error: BuiltinProgramsMustConsumeComputeUnits"
295
+ }
296
+ Self :: InvalidAccountOwner => "Error: InvalidAccountOwner" ,
297
+ Self :: ArithmeticOverflow => "Error: ArithmeticOverflow" ,
298
+ Self :: Immutable => "Error: Immutable" ,
299
+ Self :: IncorrectAuthority => "Error: IncorrectAuthority" ,
300
+ }
301
+ }
302
+ }
303
+
253
304
impl From < u64 > for ProgramError {
254
305
fn from ( error : u64 ) -> Self {
255
306
match error {
0 commit comments