1 # Error Handling and Defensive Programming Critic Framework (Universal)
3 This framework guides the Critic role when evaluating code for error handling completeness, robustness, and proper error propagation strategies across all programming languages and paradigms. This critic focuses on error detection, validation, reporting, recovery, and resource management to ensure reliable, robust, and maintainable code regardless of the programming environment.
5 ## Universal Error Handling Evaluation Areas
7 ### 1. Error Detection and Validation
9 - Comprehensive error checking at appropriate boundaries
10 - Proper input validation and sanitization
11 - Clear error detection mechanisms
12 - Appropriate use of assertions and preconditions
13 - Robust boundary condition checking
14 - Consistent error checking patterns
17 - Missing error checks for critical operations
18 - Inadequate input validation
19 - Poor error detection mechanisms
20 - Missing assertions for programming errors
21 - Insufficient boundary condition checking
22 - Inconsistent error checking patterns
24 **Evaluation Questions:**
25 - Are all potential error conditions checked?
26 - Is input validation comprehensive and robust?
27 - Are error detection mechanisms clear and effective?
28 - Are assertions used to catch programming errors?
29 - Are boundary conditions properly checked?
30 - Is error checking consistent throughout the codebase?
32 ### 2. Error Reporting and Communication
34 - Clear, actionable error messages
35 - Consistent error reporting patterns
36 - Appropriate error detail levels
37 - Effective error logging mechanisms
38 - User-friendly error communication
39 - Proper error context information
42 - Unclear or unhelpful error messages
43 - Inconsistent error reporting patterns
44 - Missing or inadequate error details
45 - Poor error logging mechanisms
46 - User-unfriendly error communication
47 - Insufficient error context
49 **Evaluation Questions:**
50 - Are error messages clear and actionable?
51 - Is error reporting consistent throughout the code?
52 - Are error details appropriate for the context?
53 - Is error logging effective and comprehensive?
54 - Is error communication user-friendly?
55 - Is sufficient error context provided?
57 ### 3. Resource Management and Cleanup
59 - Proper resource cleanup when errors occur
60 - Consistent cleanup patterns across error paths
61 - Appropriate use of cleanup mechanisms
62 - Prevention of resource leaks during error conditions
63 - Proper resource acquisition and release ordering
64 - Graceful resource degradation
67 - Resource leaks on error paths
68 - Inconsistent cleanup patterns
69 - Missing cleanup in error handling code
70 - Improper resource ordering in cleanup
71 - Incomplete cleanup when multiple resources are involved
72 - No graceful degradation strategies
74 **Evaluation Questions:**
75 - Are resources cleaned up on all error paths?
76 - Is cleanup performed in the correct order?
77 - Are cleanup patterns consistent throughout the code?
78 - Are all allocated resources properly released?
79 - Is cleanup idempotent and safe?
80 - Is graceful degradation implemented?
82 ### 4. Error Recovery and Graceful Degradation
84 - Effective error recovery mechanisms
85 - Graceful degradation when errors occur
86 - Proper error state management
87 - Clear error recovery paths
88 - Robust error handling strategies
89 - Appropriate retry mechanisms
92 - Missing error recovery mechanisms
93 - Poor graceful degradation
94 - Inadequate error state management
95 - Unclear error recovery paths
96 - Weak error handling strategies
97 - No retry mechanisms for transient errors
99 **Evaluation Questions:**
100 - Are error recovery mechanisms in place?
101 - Does the code fail gracefully when errors occur?
102 - Is error state managed correctly?
103 - Are error recovery paths clear and effective?
104 - Are error handling strategies robust?
105 - Are retry mechanisms implemented for transient errors?
107 ### 5. Error Propagation and Consistency
108 **What to Look For:**
109 - Proper propagation of error information to callers
110 - Consistent error handling patterns throughout code
111 - Uniform error reporting mechanisms
112 - Standardized error recovery approaches
113 - Clear error handling conventions
114 - Predictable error behavior
117 - Lost error information during propagation
118 - Inconsistent error handling patterns
119 - Non-uniform error reporting mechanisms
120 - Unclear error handling conventions
121 - Unpredictable error behavior
122 - Silent failures without error indication
124 **Evaluation Questions:**
125 - Is error information properly propagated to callers?
126 - Are error handling patterns consistent throughout the code?
127 - Is error reporting uniform across the codebase?
128 - Are error recovery approaches standardized?
129 - Are error handling conventions clear and followed?
130 - Is error behavior predictable and consistent?
132 ## Universal Error Handling Criticism Process
134 ### Step 1: Error Detection Analysis
135 1. **Evaluate Error Coverage**: Are all potential errors checked?
136 2. **Assess Input Validation**: Is input validation comprehensive and robust?
137 3. **Review Error Detection**: Are error detection mechanisms clear and effective?
138 4. **Check Assertions**: Are assertions used appropriately to catch programming errors?
139 5. **Verify Consistency**: Is error checking consistent throughout the codebase?
141 ### Step 2: Error Reporting Assessment
142 1. **Audit Error Messages**: Are error messages clear and actionable?
143 2. **Check Error Consistency**: Is error reporting consistent throughout the code?
144 3. **Evaluate Error Details**: Are error details appropriate for the context?
145 4. **Assess Error Logging**: Is error logging effective and comprehensive?
146 5. **Review Error Context**: Is sufficient error context provided?
148 ### Step 3: Resource Management Evaluation
149 1. **Check Resource Cleanup**: Are resources cleaned up on all error paths?
150 2. **Verify Cleanup Ordering**: Is cleanup performed in the correct order?
151 3. **Assess Cleanup Completeness**: Are all allocated resources properly released?
152 4. **Evaluate Graceful Degradation**: Is graceful degradation implemented?
153 5. **Review Resource Patterns**: Are cleanup patterns consistent throughout the code?
155 ### Step 4: Error Recovery Analysis
156 1. **Review Recovery Mechanisms**: Are error recovery mechanisms in place?
157 2. **Check Graceful Degradation**: Does the code fail gracefully when errors occur?
158 3. **Evaluate Error State Management**: Is error state managed correctly?
159 4. **Assess Recovery Paths**: Are error recovery paths clear and effective?
160 5. **Review Retry Mechanisms**: Are retry mechanisms implemented for transient errors?
162 ### Step 5: Error Propagation Assessment
163 1. **Check Error Propagation**: Is error information properly propagated to callers?
164 2. **Evaluate Consistency**: Are error handling patterns consistent throughout the code?
165 3. **Assess Error Conventions**: Are error handling conventions clear and followed?
166 4. **Review Error Behavior**: Is error behavior predictable and consistent?
167 5. **Check Silent Failures**: Are silent failures avoided?
169 ## Universal Error Handling Criticism Guidelines
171 ### Focus on Error Handling Quality
173 - "This function doesn't check for potential errors in the operation, which could lead to silent failures"
174 - "The error message here is unclear and doesn't provide enough information for debugging"
175 - "The error handling is inconsistent with the rest of the codebase and should follow the established patterns"
176 - "This input validation is insufficient and could allow invalid data to cause runtime errors"
177 - "Resource cleanup is missing in the error path, which could lead to resource leaks"
180 - "This error handling is bad"
181 - "I don't like this approach"
182 - "This should be done differently"
183 - "This is not robust"
185 ### Emphasize Robustness and Reliability
187 - "The error recovery here is inadequate and could leave the system in an inconsistent state"
188 - "This defensive programming is insufficient and doesn't protect against common failure modes"
189 - "The error boundaries are unclear, making it difficult to understand where error handling should occur"
190 - "The resource cleanup during errors is missing, which could lead to resource leaks"
191 - "Error propagation is inconsistent, making it difficult to handle errors at higher levels"
194 - "This is not robust"
195 - "This is unreliable"
196 - "This needs to be more defensive"
198 ### Consider Professional Standards
200 - "This violates the principle of defensive programming by not validating inputs at system boundaries"
201 - "The error handling doesn't follow the established patterns in this codebase"
202 - "The error recovery mechanism is missing, which violates the fail-fast principle"
203 - "This error handling is inconsistent with similar functions in the codebase"
204 - "The error propagation loses important context that would be needed for debugging"
208 - "This doesn't follow best practices"
209 - "This is inefficient"
211 ## Universal Error Handling Problem Categories
213 ### Error Detection Problems
214 - **Missing Checks**: Critical error conditions that are not checked
215 - **Inadequate Validation**: Insufficient input validation and sanitization
216 - **Poor Detection**: Ineffective error detection mechanisms
217 - **Missing Assertions**: Programming errors not caught by assertions
218 - **Inconsistent Checking**: Different error checking patterns for similar operations
220 ### Error Reporting Problems
221 - **Unclear Messages**: Error messages that are unclear or unhelpful
222 - **Inconsistent Reporting**: Non-uniform error reporting patterns
223 - **Missing Details**: Inadequate error details for debugging
224 - **Poor Logging**: Ineffective error logging mechanisms
225 - **Missing Context**: Error messages without sufficient context information
227 ### Resource Management Problems
228 - **Resource Leaks**: Failing to clean up resources on error paths
229 - **Inconsistent Cleanup**: Different cleanup patterns in similar contexts
230 - **Incomplete Cleanup**: Not cleaning up all resources when errors occur
231 - **Cleanup Ordering**: Incorrect order of resource cleanup operations
232 - **No Graceful Degradation**: Lack of graceful degradation strategies
234 ### Error Recovery Problems
235 - **Missing Recovery**: No error recovery mechanisms in place
236 - **Poor Degradation**: Inadequate graceful degradation when errors occur
237 - **Resource Issues**: Poor resource cleanup during errors
238 - **Unclear Paths**: Unclear error recovery paths
239 - **No Retry Mechanisms**: Missing retry mechanisms for transient errors
241 ### Error Propagation Problems
242 - **Lost Error Information**: Failing to propagate error information to callers
243 - **Incorrect Propagation**: Propagating errors incorrectly or inconsistently
244 - **Silent Failures**: Functions that fail silently without indicating errors
245 - **Error Masking**: Hiding or masking important error information
246 - **Inconsistent Patterns**: Different error handling patterns in similar contexts
248 ## Universal Error Handling Criticism Templates
250 ### For Error Detection Issues
252 Error Detection Issue: [Specific error detection problem]
253 Problem: [How this violates good error detection principles]
254 Impact: [Reduced reliability, maintainability, or debugging capability]
255 Evidence: [Specific error detection examples and principles]
256 Priority: [Critical/High/Medium/Low]
259 ### For Error Reporting Issues
261 Error Reporting Issue: [Specific error reporting problem]
262 Problem: [What makes this error reporting problematic]
263 Impact: [Debugging, usability, or maintenance issues]
264 Evidence: [Specific error reporting examples and patterns]
265 Priority: [High/Medium/Low]
268 ### For Resource Management Issues
270 Resource Management Issue: [Specific resource management problem]
271 Problem: [What causes the resource management issue]
272 Impact: [Resource leaks, crashes, or system instability]
273 Evidence: [Specific code paths showing resource acquisition and cleanup]
274 Priority: [Critical/High/Medium/Low]
277 ### For Error Recovery Issues
279 Error Recovery Issue: [Specific error recovery problem]
280 Problem: [What makes this error recovery inadequate]
281 Impact: [Reliability, consistency, or user experience issues]
282 Evidence: [Specific error recovery examples and patterns]
283 Priority: [High/Medium/Low]
286 ### For Error Propagation Issues
288 Error Propagation Issue: [Specific error propagation problem]
289 Problem: [What makes this error propagation inadequate]
290 Impact: [Lost error information, silent failures, or incorrect error handling]
291 Evidence: [Specific code examples showing propagation issues]
292 Priority: [Critical/High/Medium/Low]
295 ## Universal Error Handling Criticism Best Practices
298 - **Reference Universal Principles**: Always connect criticism to specific error handling principles
299 - **Focus on Error Handling Quality**: Evaluate error handling quality and professional standards
300 - **Consider Reliability**: Think about long-term system reliability and robustness
301 - **Emphasize Defensive Programming**: Prioritize code that's robust against errors
302 - **Provide Actionable Feedback**: Give specific suggestions for improvement
303 - **Consider Cross-Language Patterns**: Identify patterns that work across multiple languages
306 - **Ignore Context**: Don't criticize without understanding the error handling context
307 - **Over-Engineer**: Don't suggest overly complex error handling for simple problems
308 - **Assume Incompetence**: Don't assume the developer is incompetent or careless
309 - **Skip the Why**: Don't just say something is wrong without explaining why
310 - **Ignore Trade-offs**: Don't suggest improvements without considering trade-offs
311 - **Be Language-Specific**: Don't focus on language-specific details when universal principles apply
313 ## Universal Error Handling Criticism Checklist
315 ### Error Detection Assessment
316 - [ ] Are all potential error conditions checked?
317 - [ ] Is input validation comprehensive and robust?
318 - [ ] Are error detection mechanisms clear and effective?
319 - [ ] Are assertions used to catch programming errors?
320 - [ ] Are boundary conditions properly checked?
321 - [ ] Is error checking consistent throughout the codebase?
323 ### Error Reporting Assessment
324 - [ ] Are error messages clear and actionable?
325 - [ ] Is error reporting consistent throughout the code?
326 - [ ] Are error details appropriate for the context?
327 - [ ] Is error logging effective and comprehensive?
328 - [ ] Is error communication user-friendly?
329 - [ ] Is sufficient error context provided?
331 ### Resource Management Assessment
332 - [ ] Are resources cleaned up on all error paths?
333 - [ ] Is cleanup performed in the correct order?
334 - [ ] Are cleanup patterns consistent throughout the code?
335 - [ ] Are all allocated resources properly released?
336 - [ ] Is cleanup idempotent and safe?
337 - [ ] Is graceful degradation implemented?
339 ### Error Recovery Assessment
340 - [ ] Are error recovery mechanisms in place?
341 - [ ] Does the code fail gracefully when errors occur?
342 - [ ] Is error state managed correctly?
343 - [ ] Are error recovery paths clear and effective?
344 - [ ] Are error handling strategies robust?
345 - [ ] Are retry mechanisms implemented for transient errors?
347 ### Error Propagation Assessment
348 - [ ] Is error information properly propagated to callers?
349 - [ ] Are error handling patterns consistent throughout the code?
350 - [ ] Is error reporting uniform across the codebase?
351 - [ ] Are error recovery approaches standardized?
352 - [ ] Are error handling conventions clear and followed?
353 - [ ] Is error behavior predictable and consistent?
355 ## Universal Error Handling Evaluation Questions
358 1. **Are all potential errors checked and handled?**
359 2. **Is error reporting clear and consistent?**
360 3. **Are error recovery mechanisms in place?**
361 4. **Is defensive programming used appropriately?**
362 5. **Are error boundaries clearly defined?**
363 6. **Is the error handling robust and reliable?**
364 7. **Are error messages clear and actionable?**
365 8. **Is error logging comprehensive and effective?**
366 9. **Are error patterns consistent throughout the code?**
367 10. **Does the code fail gracefully when errors occur?**
370 1. **Are public error interfaces well-designed and documented?**
371 2. **Is error handling consistent and clear?**
372 3. **Are error conditions properly validated?**
373 4. **Is the error handling easy to use correctly?**
374 5. **Are all error resources properly managed?**
376 ### For Application Code
377 1. **Is business logic clearly separated from error handling?**
378 2. **Are user inputs properly validated and sanitized?**
379 3. **Is error handling user-friendly and informative?**
380 4. **Is the error handling organized for easy maintenance?**
381 5. **Are error characteristics appropriate for the use case?**
383 ## Universal Error Handling Principles Applied
385 ### "Handle Errors Gracefully"
386 - Check for errors at appropriate boundaries
387 - Provide clear, actionable error messages
388 - Use consistent error handling patterns
389 - Fail gracefully when errors cannot be recovered from
391 ### "Write Defensive Code"
392 - Validate inputs at system boundaries
393 - Use assertions to catch programming errors
394 - Assume that external data may be invalid
395 - Protect against common failure modes
397 ### "Check for Errors at Boundaries"
398 - Validate all inputs at system boundaries
399 - Check for errors at appropriate interfaces
400 - Use assertions to catch programming errors
401 - Protect against common failure modes
403 ### "Provide Clear Error Messages"
404 - Make error messages clear and actionable
405 - Provide appropriate error details for debugging
406 - Use consistent error reporting patterns
407 - Ensure error communication is user-friendly
409 ### "Fail Fast and Gracefully"
410 - Detect errors as early as possible
411 - Fail gracefully when errors cannot be recovered from
412 - Provide clear error recovery paths
413 - Ensure robust error handling strategies
415 ### "Manage Resources Properly"
416 - Clean up resources on all error paths
417 - Use consistent cleanup patterns
418 - Ensure proper resource ordering
419 - Implement graceful degradation
421 ### "Propagate Errors Consistently"
422 - Propagate error information to callers
423 - Use consistent error handling patterns
424 - Avoid silent failures
425 - Maintain error context through propagation
427 ## Universal Error Handling Quality Metrics
429 ### Error Detection Metrics
430 - **Error Coverage**: All potential errors should be checked
431 - **Input Validation**: Input validation should be comprehensive and robust
432 - **Error Detection**: Error detection mechanisms should be clear and effective
433 - **Assertion Usage**: Assertions should be used to catch programming errors
435 ### Error Reporting Metrics
436 - **Message Clarity**: Error messages should be clear and actionable
437 - **Reporting Consistency**: Error reporting should be consistent throughout code
438 - **Detail Appropriateness**: Error details should be appropriate for the context
439 - **Logging Effectiveness**: Error logging should be effective and comprehensive
441 ### Resource Management Metrics
442 - **Resource Cleanup**: Resources should be cleaned up on all error paths
443 - **Cleanup Completeness**: All allocated resources should be properly released
444 - **Cleanup Consistency**: Cleanup patterns should be consistent throughout code
445 - **Graceful Degradation**: Graceful degradation should be implemented
447 ### Error Recovery Metrics
448 - **Recovery Mechanisms**: Error recovery mechanisms should be in place
449 - **Graceful Degradation**: Code should fail gracefully when errors occur
450 - **Error State Management**: Error state should be managed correctly
451 - **Recovery Paths**: Error recovery paths should be clear and effective
453 ### Error Propagation Metrics
454 - **Error Propagation**: Error information should be properly propagated to callers
455 - **Pattern Consistency**: Error handling patterns should be consistent throughout code
456 - **Error Conventions**: Error handling conventions should be clear and followed
457 - **Error Behavior**: Error behavior should be predictable and consistent
459 ## Professional Standards for Error Handling
461 ### Error Handling Standards
462 - **Comprehensive Checking**: All potential errors should be checked
463 - **Clear Reporting**: Error reporting should be clear and consistent
464 - **Robust Recovery**: Error recovery mechanisms should be in place
465 - **Defensive Programming**: Defensive coding techniques should be used effectively
467 ### Error Communication Standards
468 - **Clear Messages**: Error messages should be clear and actionable
469 - **Consistent Patterns**: Error reporting patterns should be consistent
470 - **Appropriate Details**: Error details should be appropriate for the context
471 - **User-Friendly**: Error communication should be user-friendly
473 ### Resource Management Standards
474 - **Resource Cleanup**: Resources should be cleaned up on all error paths
475 - **Cleanup Completeness**: All allocated resources should be properly released
476 - **Cleanup Consistency**: Cleanup patterns should be consistent throughout code
477 - **Graceful Degradation**: Graceful degradation should be implemented
479 ### Quality Assurance Standards
480 - **Error Handling**: Comprehensive error checking and handling
481 - **Input Validation**: Validate all inputs at system boundaries
482 - **Resource Management**: Proper acquisition and release of error resources
483 - **Security Considerations**: Consider security implications of error handling