10

Error Handling

Okanran domain and Èw?` assertions

Two Approaches

??? Recoverable Errors (Okanran)

Expected failures that you can handle gracefully

?? Assertions (Èw?`)

Invariants that must never be violated — program panics

Okanran: Recoverable Errors

Use Okanran for operations that might fail (file not found, network error, invalid input).

Try / Catch Pattern

// Wrap risky operation
ayanmo result = Okanran.try(|| {
    padap? Odi.read("config.txt");
});

// Check if it's an error
ti (Okanran.is_error(result)) {
    Irosu.fo("Failed to read config, using defaults");
    ayanmo result = default_config();
} bib?k? {
    Irosu.fo("Config loaded successfully");
}

unwrap_or — Default on Error

// Get value or use default
ayanmo data = Okanran.unwrap_or(
    Okanran.try(|| Odi.read("optional.txt")),
    "default content"
);

Irosu.fo(data);  // Either file content or "default content"

Creating Errors

ise divide(a, b) {
    ti (b == 0) {
        padap? Okanran.error("Division by zero");
    }
    padap? a / b;
}

ayanmo result = divide(10, 0);
ti (Okanran.is_error(result)) {
    Irosu.kigbe("Error: " + Okanran.message(result));
}

Èw?`: Assertions (Taboos)

Use ewo for conditions that must ALWAYS be true. If violated, the program panics.

// Basic assertion
ayanmo age = 25;
ewo age >= 0;  // OK - continues

ayanmo balance = -100;
ewo balance >= 0;  // PANIC! Program stops

With Error Messages

ise withdraw(account, amount) {
    ewo amount > 0, "Amount must be positive";
    ewo amount <= account, "Insufficient funds";
    
    padap? account - amount;
}

// This will panic with message:
withdraw(100, 200);  // "Insufficient funds"

English Alias: assert

// assert is the English alias for ewo
assert x > 0, "x must be positive";
assert list != nil, "list cannot be nil";

When to Use Each

Use Okanran when:

  • File might not exist
  • Network request might fail
  • User input might be invalid
  • Recovery is possible

Use Èw?` when:

  • Condition should never be false
  • Bug in logic if violated
  • No recovery is possible
  • Security invariant

Practical Example: Input Validation

ise process_order(items, quantity) {
    // Assertions for programming errors
    ewo items != nil, "Items list required";
    ewo Ogbe.type(quantity) == "number", "Quantity must be number";
    
    // Recoverable validation
    ti (quantity <= 0) {
        padap? Okanran.error("Quantity must be positive");
    }
    
    ti (quantity > Ogunda.len(items)) {
        padap? Okanran.error("Not enough items in stock");
    }
    
    // Process order...
    Irosu.fo("Order processed for " + quantity + " items");
    padap? otito;
}

// Usage
ayanmo result = process_order(["apple", "banana"], 1);
ti (Okanran.is_error(result)) {
    Irosu.kigbe(Okanran.message(result));
}

What You Learned