Repository: aptos-labs/aptos-core
File: aptos-move/aptos-vm/src/errors.rs (Rust)

Code before:
        } => e,
        status @ VMStatus::ExecutionFailure { .. } | status @ VMStatus::Error { .. } => {
            speculative_error!(
                log_context,
                format!("[aptos_vm] Unexpected prologue error: {:?}", status),
            );
            VMStatus::Error {
                status_code: StatusCode::UNEXPECTED_ERROR_FROM_KNOWN_MOVE_FUNCTION,
                sub_status: status.sub_status(),
                message: None,
            }
        },
    })
}

/// Checks for only Move aborts or successful execution.
/// Any other errors are mapped to the invariant violation
/// `UNEXPECTED_ERROR_FROM_KNOWN_MOVE_FUNCTION`
pub fn convert_epilogue_error(
    error: VMError,
    log_context: &AdapterLogSchema,
) -> Result<(), VMStatus> {
    let status = error.into_vm_status();
    Err(match status {
        VMStatus::Executed => VMStatus::Executed,
        VMStatus::MoveAbort { location, code, .. }
            if !APTOS_TRANSACTION_VALIDATION.is_account_module_abort(&location) =>
        {
            let (category, reason) = error_split(code);
            let err_msg = format!("[aptos_vm] Unexpected success epilogue Move abort: {:?}::{:?} (Category: {:?} Reason: {:?})",
			location, code, category, reason);
            speculative_error!(log_context, err_msg.clone());
            VMStatus::error(
                StatusCode::UNEXPECTED_ERROR_FROM_KNOWN_MOVE_FUNCTION,
                Some(err_msg),
            )
        },

        VMStatus::MoveAbort {
            location,
            code,
            message,
        } => match error_split(code) {
            (LIMIT_EXCEEDED, ECANT_PAY_GAS_DEPOSIT) => VMStatus::MoveAbort {
                location,
                code,
                message,
            },
            (category, reason) => {
                let err_msg = format!("[aptos_vm] Unexpected success epilogue Move abort: {:?}::{:?} (Category: {:?} Reason: {:?})",
			    location, code, category, reason);

Review comment (bug):
can we pass message from abort to `err_msg` below if it is set? Very useful to debug

Code after:
                        err_msg.push_str(&abort_msg);
                    }
                    speculative_error!(log_context, err_msg.clone());
                    return Err(VMStatus::Error {
                        status_code: StatusCode::UNEXPECTED_ERROR_FROM_KNOWN_MOVE_FUNCTION,
                        sub_status: None,
                        message: Some(err_msg),
                    });
                },
            };
            VMStatus::error(new_major_status, None)
        },
        // Speculative errors are returned for caller to handle.
        e @ VMStatus::Error {
            status_code:
                StatusCode::SPECULATIVE_EXECUTION_ABORT_ERROR
                | StatusCode::DELAYED_FIELD_OR_BLOCKSTM_CODE_INVARIANT_ERROR,
            ..
        } => e,
        status @ VMStatus::ExecutionFailure { .. } | status @ VMStatus::Error { .. } => {
            speculative_error!(
                log_context,
                format!("[aptos_vm] Unexpected prologue error: {:?}", status),
            );
            VMStatus::Error {
                status_code: StatusCode::UNEXPECTED_ERROR_FROM_KNOWN_MOVE_FUNCTION,
                sub_status: status.sub_status(),
                message: None,
            }
        },
    })
}

/// Checks for only Move aborts or successful execution.
/// Any other errors are mapped to the invariant violation
/// `UNEXPECTED_ERROR_FROM_KNOWN_MOVE_FUNCTION`
pub fn convert_epilogue_error(
    error: VMError,
    log_context: &AdapterLogSchema,
) -> Result<(), VMStatus> {
    let status = error.into_vm_status();
    Err(match status {
        VMStatus::Executed => VMStatus::Executed,
        VMStatus::MoveAbort {
            location,
            code,
            message,
        } if !APTOS_TRANSACTION_VALIDATION.is_account_module_abort(&location) => {
            let (category, reason) = error_split(code);
            let mut err_msg = format!(
                "[aptos_vm] Unexpected success epilogue Move abort: {:?}::{:?} (Category: {:?} Reason: {:?})",