Skip to content

Commit cf92ffe

Browse files
authored
Code refactorings and clean up (#754)
1 parent 6a37a91 commit cf92ffe

File tree

10 files changed

+1667
-187
lines changed

10 files changed

+1667
-187
lines changed

controllers/coherence_controller.go

Lines changed: 86 additions & 152 deletions
Large diffs are not rendered by default.

controllers/errorhandling/README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Error Handling in Coherence Operator
2+
3+
This package provides standardized error handling patterns for the Coherence Operator. It includes utilities for error wrapping, context addition, and common error scenarios.
4+
5+
## Key Components
6+
7+
### Error Types
8+
9+
- **OperationError**: Represents an error that occurred during an operation. It includes:
10+
- Operation name
11+
- Resource name and namespace (optional)
12+
- Underlying error
13+
- Context map for additional information
14+
15+
### Error Creation
16+
17+
- **NewOperationError**: Creates a new operation error
18+
- **NewResourceError**: Creates an error for a specific resource
19+
20+
### Common Error Scenarios
21+
22+
The package provides helper functions for common error scenarios:
23+
24+
- **NewCreateResourceError**: For resource creation failures
25+
- **NewUpdateResourceError**: For resource update failures
26+
- **NewDeleteResourceError**: For resource deletion failures
27+
- **NewGetResourceError**: For resource retrieval failures
28+
- **NewListResourceError**: For resource listing failures
29+
- **NewPatchResourceError**: For resource patching failures
30+
- **NewReconcileError**: For reconciliation failures
31+
- **NewValidationError**: For validation failures
32+
- **NewTimeoutError**: For timeout failures
33+
- **NewConnectionError**: For connection failures
34+
- **NewAuthenticationError**: For authentication failures
35+
- **NewAuthorizationError**: For authorization failures
36+
37+
### Error Wrapping
38+
39+
- **WrapError**: Wraps an error with context information
40+
- **WrapErrorf**: Wraps an error with formatted context information
41+
- **WithStack**: Adds a stack trace to an error
42+
43+
### Error Handling
44+
45+
- **ErrorHandler**: Handles errors in the reconciliation loop
46+
- Categorizes errors (Transient, Permanent, Recoverable, Unknown)
47+
- Updates error tracking information
48+
- Updates resource status
49+
- Handles errors based on their category
50+
51+
## Usage Examples
52+
53+
### Creating Errors
54+
55+
```go
56+
// Create a simple operation error
57+
err := errorhandling.NewOperationError("update_config", originalErr)
58+
59+
// Add context to the error
60+
err.WithContext("resource_type", "ConfigMap").WithContext("retry_count", "3")
61+
62+
// Create a resource-specific error
63+
err := errorhandling.NewResourceError("update", "my-configmap", "default", originalErr)
64+
65+
// Use helper functions for common scenarios
66+
err := errorhandling.NewCreateResourceError("my-pod", "default", originalErr)
67+
```
68+
69+
### Handling Errors
70+
71+
```go
72+
// Create an error handler
73+
errorHandler := errorhandling.NewErrorHandler(client, logger, recorder)
74+
75+
// Handle an error
76+
result, err := errorHandler.HandleError(ctx, originalErr, resource, "Failed to update resource")
77+
78+
// Handle a resource-specific error
79+
result, err := errorHandler.HandleResourceError(ctx, originalErr, resource, "update", "Failed to update resource")
80+
81+
// Retry an operation with context
82+
err := errorHandler.RetryWithContext(ctx, "update", "my-configmap", "default", func() error {
83+
// Operation that might fail
84+
return client.Update(ctx, configMap)
85+
})
86+
```
87+
88+
## Best Practices
89+
90+
1. **Always add context to errors**: Use WithContext to add relevant information to errors.
91+
2. **Use the appropriate helper function**: Choose the most specific helper function for your error scenario.
92+
3. **Handle errors based on their category**: Use the ErrorHandler to handle errors appropriately based on their category.
93+
4. **Include stack traces**: Use WithStack to add stack traces to errors for better debugging.
94+
5. **Log errors with context**: Use the LogAndWrapError functions to log errors with context.

0 commit comments

Comments
 (0)