Skip to content

chore: improve error indicators when deserializing a delegation record fails #112

@thlorenz

Description

@thlorenz

Problem

Currently the methods generated by the below two macros return the same error no matter which part of the deserialization fails, discriminator check, bytemuck deserialization, etc.

impl_to_bytes_with_discriminator_zero_copy!(DelegationRecord);
impl_try_from_bytes_with_discriminator_zero_copy!(DelegationRecord);

Here is a snippet:

pub fn try_from_bytes_with_discriminator(
    data: &[u8],
) -> Result<&Self, ::solana_program::program_error::ProgramError> {
    if data.len() < 8 {
        return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
    }
    if Self::discriminator().to_bytes().ne(&data[..8]) {
        return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
    }
    bytemuck::try_from_bytes::<Self>(&data[8..]).or(Err(
        ::solana_program::program_error::ProgramError::InvalidAccountData,
    ))
}

This makes it really hard to know which part of this failed.

Solution

Construct a more meaningful error if possible and/or include more information with a common error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions