-
Notifications
You must be signed in to change notification settings - Fork 59
Error Handling Analysis
Drift provides deep analysis of error handling patterns, detecting gaps, boundaries, and unhandled error paths in your codebase.
The error handling analyzer:
- Maps try/catch blocks and error boundaries
- Detects swallowed errors and bare catch clauses
- Finds unhandled async error paths
- Calculates error handling quality scores
- Suggests improvements
# Build error handling analysis
drift error-handling build
# View overview
drift error-handling status
# Find gaps
drift error-handling gaps
# List error boundaries
drift error-handling boundaries
# Find unhandled paths
drift error-handling unhandled
# Analyze specific function
drift error-handling analyze src/api/users.ts:createUserdrift error-handling buildAnalyzes your codebase and builds the error handling topology. Requires call graph to be built first.
Output:
π‘οΈ Building Error Handling Analysis
ββββββββββββββββββββββββββββββββββββββββββββββββββ
β Error handling analysis built successfully
π Summary
ββββββββββββββββββββββββββββββββββββββββββββββββββ
Functions: 847
Coverage: 72%
Avg Quality: 68/100
Unhandled Paths: 12
π Quality Distribution
ββββββββββββββββββββββββββββββββββββββββββββββββββ
β Excellent: 234
β Good: 312
β Fair: 189
β Poor: 112
β οΈ Top Issues
ββββββββββββββββββββββββββββββββββββββββββββββββββ
π΄ Swallowed errors: 23
π‘ Unhandled async: 15
π΅ Bare catch: 8
drift error-handling statusShows current error handling health.
drift error-handling gaps [options]Options:
-
-l, --limit <number>β Maximum results (default: 20) -
-s, --min-severity <level>β Minimum severity: low, medium, high, critical
Output:
π Error Handling Gaps
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π΄ processPayment
src/services/payment.ts:45
Type: No error handling
Risk: 85/100
External API call without try/catch
β Add try/catch with specific error handling
π‘ fetchUserData
src/api/users.ts:23
Type: Swallowed error
Risk: 65/100
Catch block logs but doesn't rethrow or handle
β Either rethrow or implement recovery logic
drift error-handling boundariesShows all error boundaries (try/catch blocks that protect code paths).
Output:
π‘οΈ Error Boundaries
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π‘οΈ handleApiRequest
src/middleware/error-handler.ts:12
Coverage: 95%
Catches from: 47 functions
Handles: ApiError, ValidationError
ποΈ ErrorBoundary (React)
src/components/ErrorBoundary.tsx:8
Coverage: 100%
Catches from: 156 components
Framework: react
drift error-handling unhandled [options]Finds error paths that can propagate to entry points without being caught.
Options:
-
-s, --min-severity <level>β Minimum severity (default: medium)
Output:
β οΈ Unhandled Error Paths
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π΄ POST /api/payments
Database errors can reach HTTP handler uncaught
Path length: 4 functions
Error type: DatabaseError
β Suggested boundary: src/api/payments.ts:handlePayment
π‘ WebSocket onMessage
Parse errors propagate to connection handler
Path length: 3 functions
Error type: JSONParseError
β Suggested boundary: src/ws/handler.ts:onMessage
drift error-handling analyze <function>Deep analysis of a specific function's error handling.
Example:
drift error-handling analyze src/services/user.ts:createUserOutput:
π Function Analysis: createUser
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Function Info
ββββββββββββββββββββββββββββββββββββββββββββββββββ
Has try/catch: Yes
Can throw: Yes
Is async: Yes
Quality: 75/100
Protected: Yes
Catch Clauses
ββββββββββββββββββββββββββββββββββββββββββββββββββ
Line 45: catches ValidationError β recover
Line 52: catches DatabaseError β rethrow
Incoming Errors
ββββββββββββββββββββββββββββββββββββββββββββββββββ
β validateUserInput (ValidationError)
β hashPassword (CryptoError)
Outgoing Errors
ββββββββββββββββββββββββββββββββββββββββββββββββββ
β saveToDatabase (caught)
β sendWelcomeEmail (uncaught)
β οΈ Issues
ββββββββββββββββββββββββββββββββββββββββββββββββββ
π‘ Email errors not handled - could fail silently
π‘ Suggestions
ββββββββββββββββββββββββββββββββββββββββββββββββββ
β Add error handling for sendWelcomeEmail
β Consider using a job queue for email sending
drift_error_handling({
action: "status" | "gaps" | "boundaries" | "unhandled" | "analyze",
function?: string, // For analyze action (function path)
limit?: number, // For gaps action (default: 20)
minSeverity?: "low" | "medium" | "high" | "critical" // For gaps/unhandled (default: medium)
})Actions:
-
statusβ Overview of error handling health -
gapsβ Find error handling gaps -
boundariesβ List error boundaries -
unhandledβ Find unhandled error paths -
analyzeβ Analyze specific function
| Type | Description | Risk |
|---|---|---|
no-try-catch |
Function makes risky calls without error handling | High |
swallowed-error |
Catch block doesn't rethrow or handle properly | Medium |
unhandled-async |
Async operation without await or .catch() | High |
bare-catch |
Catch block with no error parameter | Low |
missing-boundary |
Entry point without error protection | Critical |
Each function receives a quality score (0-100) based on:
| Factor | Weight | Description |
|---|---|---|
| Has try/catch | 30% | Function has error handling |
| Specific catches | 20% | Catches specific error types |
| No swallowing | 20% | Doesn't silently swallow errors |
| Async handling | 15% | Properly handles async errors |
| Boundary coverage | 15% | Protected by error boundary |
Quality Levels:
- Excellent (80-100): Comprehensive error handling
- Good (60-79): Adequate error handling
- Fair (40-59): Basic error handling
- Poor (0-39): Insufficient error handling
Drift detects two types of error boundaries:
Standard try/catch blocks that protect code paths:
// Detected as error boundary
try {
await processPayment(order);
} catch (error) {
if (error instanceof PaymentError) {
return { success: false, error: error.message };
}
throw error;
}Framework-specific error handling:
// React Error Boundary
class ErrorBoundary extends React.Component {
componentDidCatch(error, info) {
logError(error, info);
}
}
// Express error middleware
app.use((err, req, res, next) => {
res.status(500).json({ error: err.message });
});
// NestJS exception filter
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) { }
}Every entry point (HTTP handler, WebSocket handler, job processor) should have error handling:
// β
Good
app.post('/api/users', async (req, res) => {
try {
const user = await createUser(req.body);
res.json(user);
} catch (error) {
handleApiError(error, res);
}
});
// β Bad - errors propagate to framework
app.post('/api/users', async (req, res) => {
const user = await createUser(req.body);
res.json(user);
});// β Bad - swallowed error
try {
await riskyOperation();
} catch (error) {
console.log('Error:', error);
// Error is lost!
}
// β
Good - proper handling
try {
await riskyOperation();
} catch (error) {
logger.error('Operation failed', { error });
throw new OperationError('Failed to complete operation', { cause: error });
}// β Bad - catches everything
try {
await processOrder(order);
} catch (error) {
return { error: 'Something went wrong' };
}
// β
Good - specific handling
try {
await processOrder(order);
} catch (error) {
if (error instanceof ValidationError) {
return { error: error.message, fields: error.fields };
}
if (error instanceof PaymentError) {
return { error: 'Payment failed', code: error.code };
}
throw error; // Rethrow unknown errors
}// β Bad - unhandled promise rejection
async function processItems(items) {
items.forEach(item => {
processItem(item); // Missing await!
});
}
// β
Good - proper async handling
async function processItems(items) {
await Promise.all(
items.map(item => processItem(item))
);
}Error handling analysis uses the call graph to trace error propagation paths.
Add error handling checks to your CI:
# .github/workflows/ci.yml
- name: Check Error Handling
run: |
drift error-handling build
drift error-handling gaps --min-severity high --format json > gaps.json
if [ $(jq '.total' gaps.json) -gt 0 ]; then
echo "High severity error handling gaps found"
exit 1
fiCreate constraints to enforce error handling patterns:
drift constraints extract
# Discovers patterns like "all API handlers must have try/catch"- Call Graph Analysis β Understand error propagation
- Quality Gates β Enforce error handling in CI
- Constraints β Define error handling requirements
- Cortex V2 Overview
- Memory Setup Wizard
- Memory CLI
- Universal Memory Types
- Learning System
- Token Efficiency
- Causal Graphs
- Code Generation
- Predictive Retrieval
- Architecture
- Call Graph Analysis
- Impact Analysis
- Security Analysis
- Data Boundaries
- Test Topology
- Coupling Analysis
- Error Handling Analysis
- Wrappers Detection
- Environment Variables
- Constants Analysis
- Styling DNA
- Constraints
- Contracts
- Decision Mining
- Speculative Execution
- Watch Mode
- Trends Analysis
- Projects Management
- Package Context
- Monorepo Support
- Reports & Export
- Dashboard
- 10 Languages
- 21 Frameworks
- 16 ORMs
- 400+ Detectors
- 50+ MCP Tools
- 60+ CLI Commands
- 23 Memory Types