Result Pattern
The Result pattern is a clean, type-safe way to handle success or failure in operations without throwing exceptions.
✅ Prevents uncontrolled exceptions from leaking into your application.
✅ Makes function return types clear and predictable.
What is the Result Pattern?
Section titled “What is the Result Pattern?”Instead of returning raw values or throwing errors, you wrap the outcome in a Result
object.
This object has:
- A success/failure status.
- Either the successful result or the failure reason.
Example:
const result = await service.doSomething();
if (result.isSuccess()) { const value = result.value;} else { const error = result.error;}
✅ This forces the caller to explicitly handle both success and error cases.
Result API
Section titled “Result API”DomusJS provides the following:
Interface
Section titled “Interface”export class Result<T, E> { isSuccess(): boolean; isFailure(): boolean; value: T; error: E;}
Static Constructors
Section titled “Static Constructors”Result.ok(value);Result.fail(error);
Example
Section titled “Example”const result = Result.ok('User created');
if (result.isSuccess()) { console.log(result.value); // 'User created'}
Example Use Case: Register User
Section titled “Example Use Case: Register User”Service Method
Section titled “Service Method”import { Result } from '@domusjs/core';
export async function registerUser(email, password): Promise<Result<User, string>> { if (await userExists(email)) { return Result.fail('Email already taken'); }
const user = await createUser(email, password); return Result.ok(user);}
Caller Handling
Section titled “Caller Handling”const result = await registerUser('test@example.com', 'password123');
if (result.isSuccess()) { console.log('User registered:', result.value);} else { console.error('Failed to register user:', result.error);}
✅ This makes sure no uncaught exceptions happen.
Best Practices
Section titled “Best Practices”✅ Use Result
to wrap operations where failure is a normal, expected possibility.
✅ Avoid using it for critical, unexpected failures (those should be proper exceptions).
✅ Always handle both paths (ok
and fail
) at the call site.
Benefits
Section titled “Benefits”- Clearer code: success and error cases are always visible.
- Safer APIs: avoids forgotten
.catch()
blocks or missed error branches. - Easier testing: you can easily test both successful and failing scenarios.