soma.cell

Error handling

Error handling, in the reference of the Soma programming language (soma 3.2.4). A block marked ✓ is a whole program that soma check accepts; the others are excerpts.

let result = try { risky_operation() }     // {value, error, kind, detail}
if result.error != () {
    print("Error: {result.detail}")        // detail = the message alone (Error.message); error = "kind: detail"
    return response(500, map("error", result.detail, "kind", result.kind))
}
let value = result.value

// Short form: `try { … }?` re-raises the error (same kind; the handler is rolled back) or gives the value
let value = try { risky_operation() }?
// Equivalent to: if result has error, return error map; else unwrap value

// Agent builtins
let answer = think("What is 2+2?")           // call LLM (configured in soma.toml)
let data = think_json("Return as JSON: ...")  // LLM returns Map, not String

// Bounded think/http — enables compile-time budget proofs
let answer = think("prompt", map("max_tokens", 500, "timeout", 10000))
let data = http_get(url, map("max_bytes", 65536, "timeout", 5000))

delegate("Writer", "write", facts, topic)     // call another agent's handler
remember("key", value)                        // persistent agent memory
let val = recall("key")                       // recall from agent memory
set_budget(5000)                              // hard token cap
let t = tokens_used()                         // tokens consumed
let left = tokens_remaining()                 // budget minus used (-1 = unlimited)
let log = trace()                             // execution log
clear_trace()                                 // reset the execution log
clear_context()                               // reset multi-turn LLM conversation
approve("publish article")                    // human-in-the-loop gate