Result Type
Result Type
Section titled “Result Type”Fizzy MCP uses a Rust-inspired Result type for explicit error handling.
Why Result?
Section titled “Why Result?”Instead of throwing exceptions, functions return either:
Ok(value)- Success with a valueErr(error)- Failure with an error
This makes error handling explicit and type-safe.
Type Definition
Section titled “Type Definition”type Result<T, E = Error> = | { readonly ok: true; readonly value: T } | { readonly ok: false; readonly error: E };Basic Usage
Section titled “Basic Usage”import { Ok, Err, type Result } from '@fizzy-mcp/shared';
function divide(a: number, b: number): Result<number, string> { if (b === 0) { return Err('Division by zero'); } return Ok(a / b);}
const result = divide(10, 2);
if (result.ok) { console.log(`Result: ${result.value}`); // 5} else { console.log(`Error: ${result.error}`);}Helper Functions
Section titled “Helper Functions”Type Guards
Section titled “Type Guards”import { isOk, isErr } from '@fizzy-mcp/shared';
if (isOk(result)) { // TypeScript knows result.value exists console.log(result.value);}
if (isErr(result)) { // TypeScript knows result.error exists console.log(result.error);}Unwrapping
Section titled “Unwrapping”import { unwrap, unwrapOr } from '@fizzy-mcp/shared';
// Throws if errorconst value = unwrap(result);
// Returns default if errorconst value = unwrapOr(result, 0);Transforming
Section titled “Transforming”import { map, mapErr, andThen } from '@fizzy-mcp/shared';
// Transform the success valueconst doubled = map(result, (x) => x * 2);
// Transform the errorconst mapped = mapErr(result, (e) => new Error(e));
// Chain Result-returning functionsconst chained = andThen(result, (x) => x > 0 ? Ok(x) : Err('Must be positive'));From Promises/Try
Section titled “From Promises/Try”import { fromPromise, fromTry } from '@fizzy-mcp/shared';
// Wrap a promise that might rejectconst result = await fromPromise(fetch('/api'));
// Wrap a function that might throwconst result = fromTry(() => JSON.parse(data));
// With custom error mappingconst result = await fromPromise( fetch('/api'), (error) => new FizzyNetworkError(String(error)));With Fizzy Client
Section titled “With Fizzy Client”All client methods return Promise<Result<T, FizzyError>>:
const result = await client.cards.list();
if (result.ok) { for (const card of result.value) { console.log(card.title); }} else { console.error(`Error (${result.error.code}): ${result.error.message}`);
if (result.error.retryable) { // Safe to retry }}Benefits
Section titled “Benefits”- No unexpected exceptions - Errors are always handled
- Type safety - TypeScript knows exactly what can fail
- Composable - Chain operations with
andThen - Explicit - Error handling is visible in the code