Skip to content

fix: customer validation on subscription create - #3409

Merged
chrisgacsal merged 1 commit into
mainfrom
fix/subscription-validation
Sep 22, 2025
Merged

chrisgacsal merged 1 commit into
mainfrom
fix/subscription-validation

Conversation

@chrisgacsal

@chrisgacsal chrisgacsal commented Sep 22, 2025 •

Copy link
Copy Markdown
Collaborator

Overview

Map validation errors returned by Customer lookup for Subscription create operation.

Summary by CodeRabbit

  • Bug Fixes

    • More reliable customer lookup during subscription creation, handling both customer ID and key inputs.
    • Clearer validation when no identifier is provided, reducing ambiguous failures.
  • Improvements

    • Error messages now include namespace and identifier details to aid troubleshooting.
    • Streamlined error handling for custom plan creation paths, improving stability and clarity.

@chrisgacsal chrisgacsal self-assigned this Sep 22, 2025
@chrisgacsal
chrisgacsal requested a review from a team as a code owner September 22, 2025 11:24
@chrisgacsal chrisgacsal added the release-note/bug-fix Release note: Bug Fixes label Sep 22, 2025
@coderabbitai

coderabbitai Bot commented Sep 22, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Refactors the subscription creation handler to rename a customer variable, restructure getCustomer with switch-based lookup by ID or Key, adjust error handling and messages with namespace/context, remove certain preliminary checks, and use the resolved customer ID in workflow inputs. No exported/public interfaces changed.

Changes

Cohort / File(s) Summary of changes
Subscription create handler
openmeter/productcatalog/subscription/http/create.go
Renamed customer variable to cus and updated references; in custom plan path, returns wrapped error on getCustomer failure and uses cus.ID; rewrote getCustomer to switch on ID/Key, removed some preliminary nil/empty checks, added contextual error messages, maintained deletion check, consolidated returns.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "fix: customer validation on subscription create" succinctly and accurately describes the primary change in the PR — adjustments to customer lookup and validation logic during subscription creation (including getCustomer error handling and validation mapping). It is concise, specific to the changeset and aligned with the PR objectives, so a reviewer can quickly understand the main intent.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/subscription-validation

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@chrisgacsal
chrisgacsal enabled auto-merge (squash) September 22, 2025 11:26
@chrisgacsal
chrisgacsal merged commit 0326ef0 into main Sep 22, 2025
@chrisgacsal
chrisgacsal deleted the fix/subscription-validation branch September 22, 2025 11:28

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (4)
openmeter/productcatalog/subscription/http/create.go (4)

178-186: Guard against both id and key being provided (ambiguous input)

If both are set, current logic silently prefers id. Consider rejecting the request explicitly.

Proposed change:

-	switch {
+	// reject ambiguous inputs
+	if id != nil && *id != "" && key != nil && *key != "" {
+		return nil, models.NewGenericValidationError(fmt.Errorf("customer id and key are mutually exclusive"))
+	}
+	switch {

If preferring one over the other is intentional, ignore this and please confirm.


190-199: Fix label in error message (should be customer.key, not customer.id)

This is misleading during debugging/triage.

Apply:

-			return nil, fmt.Errorf("failed to lookup customer by key [namespace=%s customer.id=%s]: %w", namespace, *key, err)
+			return nil, fmt.Errorf("failed to lookup customer by key [namespace=%s customer.key=%s]: %w", namespace, *key, err)

211-211: Return explicit nil error for clarity

No path reaches here with a non-nil err.

-	return cus, err
+	return cus, nil

129-134: Avoid shadowing imported package name with local variable

Local var named customer shadows the imported customer package; prefer cus for consistency with the custom plan path.

-				customer, err := h.getCustomer(ctx, ns, parsedBody.CustomerId, parsedBody.CustomerKey)
+				cus, err := h.getCustomer(ctx, ns, parsedBody.CustomerId, parsedBody.CustomerKey)
 				if err != nil {
 					return CreateSubscriptionRequest{}, fmt.Errorf("failed to get customer: %w", err)
 				}
@@
-						CustomerID:    customer.ID,
+						CustomerID:    cus.ID,

Also applies to: 146-147

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c09492b and 9b09bb7.

📒 Files selected for processing (1)
  • openmeter/productcatalog/subscription/http/create.go (4 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
openmeter/productcatalog/subscription/http/create.go (2)
openmeter/customer/customer.go (3)
  • CustomerKey (119-122)
  • CustomerID (104-104)
  • GetCustomerInput (273-280)
pkg/models/errors.go (1)
  • NewGenericValidationError (138-140)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: Quickstart
  • GitHub Check: E2E
  • GitHub Check: Build
  • GitHub Check: Lint
  • GitHub Check: Code Generators
  • GitHub Check: Test
  • GitHub Check: Migration Checks
  • GitHub Check: Analyze (go)
🔇 Additional comments (2)
openmeter/productcatalog/subscription/http/create.go (2)

201-203: LGTM: clear validation error when neither id nor key provided

Good user-facing error mapping.


86-89: LGTM: customer resolution and CustomerID wiring — error unwrapping verified

fmt.Errorf uses %w (preserves the error chain); the subscription HTTP path uses commonhttp.HandleErrorIfTypeMatches (via openmeter/productcatalog/subscription/http/errors.go) and pkg/models helpers that delegate to errors.As, so wrapped GenericValidationError will be unwrapped and mapped by the encoder (see openmeter/productcatalog/subscription/http/create.go and pkg/framework/commonhttp/errors.go).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants