soma.cell

HTTP Status Codes

response() lets you set HTTP status codes for error handling. Serve: soma serve 044_status_codes.cell Test: curl localhost:8080/find/1 -> 404 not found curl localhost:8080/health -> 200 ok

Example 044 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.

How it is built

The cell StatusCodes keeps 1 memory slot: items (Map<String, String>, persistent and consistent). Handlers: health (line 20), find (line 25) and create (line 35).

Generated from soma describe on this file.

The source

044_status_codes.cell

// ============================================================================
// 044 — HTTP Status Codes
// ============================================================================
// response() lets you set HTTP status codes for error handling.
//
// Serve: soma serve 044_status_codes.cell
// Test:  curl localhost:8080/find/1   -> 404 not found
//        curl localhost:8080/health   -> 200 ok
//
// Run:   soma run 044_status_codes.cell health
// Expect: {"status": "ok", "version": "1.0"}

cell StatusCodes {

    memory {
        items: Map<String, String> [persistent, consistent]
    }

    // Health check — always returns 200
    on health() {
        return map("status", "ok", "version", "1.0")
    }

    // Find an item — returns 404 if not found
    on find(id: String) {
        let item = items.get(id)
        if item == () {
            // response(status_code, body) sets the HTTP status
            return response(404, map("error", "item not found", "id", id))
        }
        return from_json(item)
    }

    // Create — returns the created item
    on create(name: String) {
        let id = to_string(next_id())
        let item = map("id", id, "name", name)
        items.set(id, to_json(item))
        return item
    }
}

What the toolchain says

$ soma check 044_status_codes.cell
✓ All checks passed.
$ soma run 044_status_codes.cell health
{"status": "ok", "version": "1.0"}