result-types
Never throw for expected failures. Use Result<T, E> types with explicit error handling and workflow composition.
What this skill does
# Typed Errors: Never Throw
## Core Principle
Exceptions are invisible, bypass composition, and conflate different failures. Return `Result<T, E>` instead.
```typescript
// WRONG - Signature lies
async function getUser(args): Promise<User> {
const user = await deps.db.findUser(args.userId);
if (!user) throw new Error('User not found'); // Hidden!
return user;
}
// CORRECT - Signature tells the truth
async function getUser(args, deps): Promise<Result<User, 'NOT_FOUND' | 'DB_ERROR'>> {
try {
const user = await deps.db.findUser(args.userId);
return user ? ok(user) : err('NOT_FOUND');
} catch {
return err('DB_ERROR');
}
}
```
## The Result Type
```typescript
type Result<T, E> =
| { ok: true; value: T }
| { ok: false; error: E };
type AsyncResult<T, E> = Promise<Result<T, E>>;
const ok = <T>(value: T): Result<T, never> => ({ ok: true, value });
const err = <E>(error: E): Result<never, E> => ({ ok: false, error });
```
## Required Behaviors
### 1. Business Functions Return Results
```typescript
async function getUser(
args: { userId: string },
deps: GetUserDeps
): Promise<Result<User, 'NOT_FOUND' | 'DB_ERROR'>> {
try {
const user = await deps.db.findUser(args.userId);
if (!user) return err('NOT_FOUND');
return ok(user);
} catch {
return err('DB_ERROR');
}
}
```
### 2. Use createWorkflow() for Composition
Avoid verbose if-checking with railway-oriented programming:
```typescript
import { createWorkflow } from '@jagreehal/workflow';
// Declare dependencies -> error union computed automatically
const loadUserData = createWorkflow({ getUser, getPosts, enrichUser });
const result = await loadUserData(async (step) => {
const user = await step(() => getUser({ userId }, deps));
const posts = await step(() => getPosts({ userId: user.id }, deps));
const enriched = await step(() => enrichUser({ user, posts }, deps));
return { user: enriched };
});
// result: Result<{ user: EnrichedUser }, 'NOT_FOUND' | 'DB_ERROR' | 'FETCH_ERROR' | ...>
```
The `step()` function:
- Unwraps `ok` results and continues on happy path
- On `err`, immediately short-circuits and skips remaining steps
### 3. Use step.try() for Throwing Code
Bridge between throwing code and Result pipeline:
```typescript
const workflow = createWorkflow({ getUser });
const result = await workflow(async (step) => {
const user = await step(() => getUser({ userId }, deps));
// Throwing function: use step.try() with error mapping
const config = await step.try(
() => JSON.parse(user.configJson),
{ error: 'INVALID_CONFIG' as const }
);
return { user, config };
});
```
- `step()`: For functions that already return Result (your code)
- `step.try()`: For functions that throw (third-party, built-in)
- `step.fromResult()`: For Result-returning functions where you need to map errors
**For Result-returning functions:** Use `step.fromResult()` to preserve typed errors:
```typescript
// callProvider returns Result<Response, ProviderError>
const callProvider = async (input: string): AsyncResult<Response, ProviderError> => { ... };
const response = await step.fromResult(
() => callProvider(input),
{
onError: (e) => ({
type: 'PROVIDER_FAILED' as const,
provider: e.provider, // TypeScript knows e is ProviderError
code: e.code,
})
}
);
```
### 4. Map Results to HTTP at Boundary
```typescript
const errorToStatus: Record<string, number> = {
NOT_FOUND: 404,
UNAUTHORIZED: 401,
FORBIDDEN: 403,
VALIDATION_FAILED: 400,
CONFLICT: 409,
};
function resultToResponse<T, E extends string>(
result: Result<T, E>,
res: Response
): Response {
if (result.ok) {
return res.status(200).json(result.value);
}
const status = errorToStatus[result.error] ?? 500;
return res.status(status).json({
error: result.error,
code: result.error,
});
}
// Handler becomes simple
app.get('/users/:id', async (req, res) => {
const result = await getUser({ userId: req.params.id }, deps);
return resultToResponse(result, res);
});
```
### 5. Exhaustive Error Handling
TypeScript enforces handling all error cases:
```typescript
if (!result.ok) {
switch (result.error) {
case 'NOT_FOUND':
return res.status(404).json({ error: 'User not found' });
case 'DB_ERROR':
case 'FETCH_ERROR':
return res.status(500).json({ error: 'Internal error' });
// TypeScript will error if you miss a case!
}
}
```
## Error Type Patterns
### String Literals (Simple)
```typescript
type AppError = 'NOT_FOUND' | 'UNAUTHORIZED' | 'DB_ERROR';
```
### Discriminated Unions (Rich)
```typescript
type AppError =
| { type: 'NOT_FOUND'; resource: string }
| { type: 'VALIDATION'; field: string; message: string }
| { type: 'DB_ERROR'; query: string };
```
### Const Objects (Runtime + Type)
```typescript
const Errors = {
NOT_FOUND: 'NOT_FOUND',
DB_ERROR: 'DB_ERROR',
} as const;
type AppError = (typeof Errors)[keyof typeof Errors];
return err(Errors.NOT_FOUND); // Runtime value available
```
## Error Grouping at Scale
As applications grow, error unions become unwieldy:
```typescript
// This becomes a "Type Wall"
type AllErrors =
| 'NOT_FOUND'
| 'DB_ERROR'
| 'DB_CONNECTION_FAILED'
| 'DB_TIMEOUT'
| 'FETCH_ERROR'
| 'HTTP_TIMEOUT'
| 'RATE_LIMITED'
| 'CIRCUIT_OPEN'
| 'VALIDATION_FAILED'
// ... 20 more errors
```
**Solution:** Group related errors into categories:
```typescript
// Group by domain
type DatabaseError = 'DB_ERROR' | 'DB_CONNECTION_FAILED' | 'DB_TIMEOUT';
type NetworkError = 'FETCH_ERROR' | 'HTTP_TIMEOUT' | 'RATE_LIMITED';
type BusinessError = 'NOT_FOUND' | 'VALIDATION_FAILED' | 'UNAUTHORIZED';
type AppError = DatabaseError | NetworkError | BusinessError;
// Or use discriminated unions for richer context
type AppError =
| { type: 'DATABASE'; code: 'CONNECTION_FAILED' | 'TIMEOUT' | 'QUERY_FAILED' }
| { type: 'NETWORK'; code: 'TIMEOUT' | 'RATE_LIMITED' | 'UNREACHABLE' }
| { type: 'BUSINESS'; code: 'NOT_FOUND' | 'VALIDATION_FAILED' };
```
This keeps error types manageable while preserving type safety.
## When Throwing Is Still Right
Throw only for:
- **Invariant violation** (programmer error, impossible state)
- **Corrupted process state** (can't recover)
- **Truly unrecoverable** situations
```typescript
// Good: throw for impossible states
if (!user) throw new Error('Unreachable: user should exist after insert');
```
### Using `asserts` for Type Narrowing
The `asserts` keyword creates runtime checks that also narrow types:
```typescript
// Assert function: throws if condition fails, narrows type if succeeds
function assertUser(user: User | null): asserts user is User {
if (!user) throw new Error('Invariant violated: user must exist');
}
function assertDefined<T>(value: T | undefined, name: string): asserts value is T {
if (value === undefined) throw new Error(`${name} must be defined`);
}
// Usage: TypeScript narrows the type after the assertion
const user = await deps.db.findUser(userId);
assertUser(user); // Throws if null
// TypeScript now knows `user` is `User`, not `User | null`
console.log(user.name); // Safe access
```
**When to use `asserts`:**
- After database inserts (record MUST exist)
- After config loading (values MUST be present)
- After state transitions (state MUST be valid)
**Don't use for:** Normal business logic failures (use Result instead)
## Quick Reference
| Situation | Use |
|-----------|-----|
| Domain failure (not found, validation) | Result |
| Infrastructure failure (recoverable) | Result |
| Programmer error | throw |
| Corrupted state | throw |
## Architecture Layer
```
Handlers / Routes
-> map Result -> HTTP response
Business Logic
-> createWorkflow({ ... })(async (step) => { ... })
Core Functions
-> fn(args, deps): Result<T, E>
Infrastructure
-> catch exceptions, return Results
```
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.