Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@0xzrf
Copy link

@0xzrf 0xzrf commented Oct 14, 2025

Fixes #253

Overview

This PR introduces custom error definitions and enhanced logging for program errors.

Currently, when a custom error occurs, the output is not very informative:

Current error output

This lack of clarity increases debugging time for developers and can be particularly problematic when deployed on-chain.


Problem

  • Developers struggle to quickly identify the source of errors in their programs.
  • Debugging on-chain contracts becomes time-consuming and error-prone.

Solution

By defining custom errors and leveraging enhanced logging, this PR aims to reduce debugging time significantly for contracts built on Pinocchio.


Implementation

  1. Define a custom error in your program using the pinocchio_err crate:
Custom error definition
  1. Use the require! macro from the pinocchio crate to return program errors:
Using require macro

Result

When a custom error occurs, the output becomes clear and actionable:

Improved error output

This improvement helps developers quickly identify and resolve issues, making on-chain program debugging far more efficient.


/// Defines a custom error macro to create custom errors in pinocchio framework
#[proc_macro_derive(ErrorCode, attributes(msg))]
pub fn error_code(input: TokenStream) -> TokenStream {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to replicate the logic from thiserror, so not sure if that is necessary.

Comment on lines +301 to +313
// If the error type has a `message()` function, call it.
// Otherwise, do nothing.
#[allow(unused_variables)]
{
if false {
// just to scope-check type
} else {
#[allow(unused_unsafe)]
unsafe {
$crate::msg!($error.message());
}
}
}
Copy link
Collaborator

@febo febo Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super strange. I am not sure why it is not written just like:

$crate::msg!($error.message());

Also, it is inefficient in terms of program size since it will generate a call to the log syscall in every use of require!. It would be much better to follow a pattern where any error is printed at the end of the process_instruction, similar to p-token: https://github.com/solana-program/token/blob/main/pinocchio/program/src/entrypoint.rs#L258

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Add require and error_code equivalent of anchor

2 participants