Skip to content

Latest commit

 

History

History
110 lines (91 loc) · 3.51 KB

File metadata and controls

110 lines (91 loc) · 3.51 KB

Error Path Mapping Fixes

Summary

All bare panic!() calls have been replaced with stable Error variants, ensuring every failure path maps to a well-defined error code.

Changes Made

1. Added New Error Variant ✅

File: src/err.rs

  • New Variant: GasBudgetExceeded = 417
  • Location: General Errors category (400-418)
  • Description: "Gas budget cap has been exceeded for the operation."

2. Fixed Admin Not Set Panics ✅

File: src/lib.rs

Location 1: Market Creation Function (~Line 374)

Before:

let stored_admin: Address = env
    .storage()
    .persistent()
    .get(&Symbol::new(&env, "Admin"))
    .unwrap_or_else(|| {
        panic!("Admin not set");  // ❌ Bare panic
    });

After:

let stored_admin: Address = match env
    .storage()
    .persistent()
    .get(&Symbol::new(&env, "Admin"))
{
    Some(admin_addr) => admin_addr,
    None => panic_with_error!(env, Error::AdminNotSet),  // ✅ Maps to Error::AdminNotSet
};

Location 2: Event Creation Function (~Line 486)

Before:

let stored_admin: Address = env
    .storage()
    .persistent()
    .get(&Symbol::new(&env, "Admin"))
    .unwrap_or_else(|| {
        panic!("Admin not set");  // ❌ Bare panic
    });

After:

let stored_admin: Address = match env
    .storage()
    .persistent()
    .get(&Symbol::new(&env, "Admin"))
{
    Some(admin_addr) => admin_addr,
    None => panic_with_error!(env, Error::AdminNotSet),  // ✅ Maps to Error::AdminNotSet
};

3. Fixed Gas Budget Exceeded Panic ✅

File: src/gas.rs

Before:

if let Some(limit) = Self::get_limit(env, operation) {
    if actual_cost > limit {
        panic!("Gas budget cap exceeded");  // ❌ Bare panic
    }
}

After:

if let Some(limit) = Self::get_limit(env, operation) {
    if actual_cost > limit {
        panic_with_error!(env, crate::err::Error::GasBudgetExceeded);  // ✅ Maps to Error::GasBudgetExceeded
    }
}

Error Code Mapping Summary

Failure Path Previous Now Error Code
Admin not configured panic!("Admin not set") Error::AdminNotSet 418
Gas budget exceeded panic!("Gas budget cap exceeded") Error::GasBudgetExceeded 417

Benefits

  1. Stable Error Codes: All failures now map to defined error variants with unique numeric codes (417, 418)
  2. Better Diagnostics: Contract clients can now handle these errors programmatically instead of unexpected panics
  3. Improved Reliability: Error variants have associated metadata (severity, recovery strategy, messages)
  4. Contract Compatibility: Clients and integrations can safely decode these error codes

Verification

  • ✅ All bare panic!() calls related to failure paths have been identified and replaced
  • ✅ New error variant added to the error enum with proper documentation
  • ✅ Both locations using Admin checks now use standardized error handling
  • ✅ Gas tracking operations now report errors instead of panicking
  • ✅ Pattern follows existing codebase conventions (panic_with_error! macro)

Related Documentation

See ERROR_HANDLING_ANALYSIS.md for complete error handling analysis and best practices.