Skip to content

INFO. NearExpect for Result: include original error in panic message #4

@olga24912

Description

@olga24912

Currently, NearExpect implementation for Result<T, E> discards the original error:

impl<T, E> NearExpect<T> for Result<T, E> {
    fn near_expect(self, msg: impl AsRef<str>) -> T {
        self.unwrap_or_else(|_| near_sdk::env::panic_str(msg.as_ref()))
    }
}

The original error E is silently ignored, which makes debugging harder — the caller only sees the static message, not what actually went wrong.

Suggestion:

Add a bound E: core::fmt::Debug (or Display) and format the original error into the panic message:

impl<T, E: core::fmt::Debug> NearExpect<T> for Result<T, E> {
    fn near_expect(self, msg: impl AsRef<str>) -> T {
        self.unwrap_or_else(|err| {
            near_sdk::env::panic_str(&format!("{}: {:?}", msg.as_ref(), err))
        })
    }
}

Trade-offs to consider:

  • Adding E: Debug is a stricter bound — some Result types may not satisfy it (though in practice most errors implement Debug)
  • Could keep the current impl as-is and add a separate method like near_expect_dbg to avoid breaking changes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions