Repository navigation
refactor(notification): error handling - #3008
Conversation
📝 WalkthroughWalkthroughThis update restructures notification and webhook handling by extracting event-to-payload conversion into a helper, modularizing event reconciliation, and centralizing Svix error handling in a new internal package. Validation logic is unified to aggregate errors, and secret validation is moved to a dedicated file. Svix webhook integration now uses structured error parsing and improved conflict handling. Changes
Possibly related PRs
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (1.64.8)Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2 ✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (6)
openmeter/notification/webhook/svix/event.go (1)
23-26: Log message says “create” while the call performs an “update”
EventType.Updateis invoked but the wrapped error reads “failed to create event type”, which is confusing when troubleshooting.- return fmt.Errorf("failed to create event type: %w", err) + return fmt.Errorf("failed to update event type %q: %w", eventType.Name, err)openmeter/notification/webhook/secret.go (1)
30-43: Early-exit to avoid allocating/wrapping when no validation errors
errors.Join(nil)already returnsnil, but we still allocate a slice and callNewValidationError.
A quick short-circuit keeps the happy path cheaper and simpler.- var errs []error + var errs []error ... - return NewValidationError(errors.Join(errs...)) +if len(errs) == 0 { + return nil +} + +return NewValidationError(errors.Join(errs...))openmeter/notification/webhook/errors.go (1)
17-23: Guard against double-wrapping ValidationErrorIf callers accidentally pass an existing
ValidationErrortoNewValidationError, it will be wrapped again, breakingerrors.Is/Aschecks.
Consider short-circuiting whenerrors.As(err, &ValidationError{})is true.func NewValidationError(err error) error { if err == nil { return nil } + + var ve ValidationError + if errors.As(err, &ve) { // already wrapped + return err + } return ValidationError{err: err} }openmeter/notification/webhook/svix/webhook.go (1)
27-37: Repeatedinternal.AsSvixErrorpattern can be centralisedAlmost every SDK call now follows:
err := client.Foo(...) if err = internal.AsSvixError(err); err != nil { return ..., fmt.Errorf("failed to ...: %w", err) }Extracting this into a tiny helper (or embedding
clientin a thin wrapper) would:• remove duplication
• ensure future calls follow the same policy
• keep individual handlers focused on business logic.No functional change required, but worth considering for maintainability.
Also applies to: 70-76, 239-243, 331-334, 345-347, 371-373
openmeter/notification/webhook/svix/application.go (1)
14-17: Consider differentiating between Name and Uid fieldsWhile setting both
NameandUidto the same value works, it might be semantically clearer to use a more descriptive name. Consider whether these should be different values for better clarity.input := svix.ApplicationIn{ - Name: id, + Name: fmt.Sprintf("Application %s", id), Uid: &id, }openmeter/notification/webhook/svix/internal/error.go (1)
63-65: Consider adding context when JSON unmarshaling fails.When JSON unmarshaling fails, the function returns the original error without indicating that parsing failed. This could make debugging more difficult.
Consider wrapping the error with additional context:
if e := json.Unmarshal(svixErr.Body(), &body); e != nil { - return err + return fmt.Errorf("failed to parse svix validation error response: %w", err) }And similarly for the default case:
if e := json.Unmarshal(svixErr.Body(), &body); e != nil { - return err + return fmt.Errorf("failed to parse svix error response: %w", err) }Note: This change requires adding
"fmt"to the imports if not already present (which it already is in this file).Also applies to: 76-78
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (12)
openmeter/notification/eventhandler/dispatch.go(1 hunks)openmeter/notification/eventhandler/reconcile.go(2 hunks)openmeter/notification/httpdriver/errors.go(1 hunks)openmeter/notification/webhook/errors.go(1 hunks)openmeter/notification/webhook/handler.go(6 hunks)openmeter/notification/webhook/secret.go(2 hunks)openmeter/notification/webhook/svix/application.go(1 hunks)openmeter/notification/webhook/svix/event.go(2 hunks)openmeter/notification/webhook/svix/internal/error.go(1 hunks)openmeter/notification/webhook/svix/message.go(3 hunks)openmeter/notification/webhook/svix/svix.go(2 hunks)openmeter/notification/webhook/svix/webhook.go(8 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (13)
- GitHub Check: Artifacts / Container image
- GitHub Check: Artifacts / Benthos Collector Container image
- GitHub Check: Commit hooks
- GitHub Check: Quickstart
- GitHub Check: Migration Checks
- GitHub Check: CI
- GitHub Check: Developer environment
- GitHub Check: Lint
- GitHub Check: E2E
- GitHub Check: Test
- GitHub Check: Build
- GitHub Check: Analyze (javascript)
- GitHub Check: Analyze (go)
🔇 Additional comments (9)
openmeter/notification/webhook/svix/application.go (1)
13-32: LGTM! Clean implementation with proper error handlingThe method correctly implements application creation with idempotency support and follows the new error handling pattern consistently.
openmeter/notification/eventhandler/dispatch.go (2)
15-54: Excellent refactoring - improves separation of concernsThe extraction of payload conversion logic into a dedicated helper function enhances code readability and maintainability. The error handling is consistent and comprehensive.
56-61: Clean integration with the new helper functionThe simplified
dispatchWebhookmethod now focuses solely on webhook dispatch logic, making the code more maintainable.openmeter/notification/webhook/svix/svix.go (1)
17-17: Good consolidation of error handlingThe use of a type alias to
internal.SvixErrormaintains API compatibility while centralizing the error handling implementation. This aligns well with the PR's objective of improving error handling.Also applies to: 30-30
openmeter/notification/webhook/svix/message.go (2)
42-58: Excellent improvement to conflict handlingThe enhanced error handling now properly differentiates between error types and implements idempotent behavior by retrieving existing messages on conflicts. This is a significant improvement that aligns with the PR's goal of better error handling.
70-97: Well-structured helper method with consistent error handlingThe
getMessagehelper maintains consistency with the error handling pattern established inSendMessageand properly includes content in the response.openmeter/notification/eventhandler/reconcile.go (2)
26-47: Good modularization of reconciliation logicThe extraction of event reconciliation into a dedicated method improves code organization and makes the logic easier to test and maintain.
61-72: Improved error handling with better contextThe change from fail-fast to error accumulation allows all events to be processed, and the enhanced error messages with namespace and event ID provide valuable debugging context.
openmeter/notification/webhook/handler.go (1)
40-48: Excellent refactor of validation error handling!The consistent pattern of accumulating all validation errors and returning them together significantly improves the user experience. Users can now see all validation issues at once instead of discovering them one by one.
Also applies to: 67-85, 104-128, 140-152, 162-174, 199-215
Overview
Summary by CodeRabbit
New Features
Bug Fixes
Refactor
Chores