Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions openmeter/credit/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package credit

import "github.com/openmeterio/openmeter/pkg/models"

const ErrCodeGrantAmountMustBePositive models.ErrorCode = "grant_amount_must_be_positive"

var ErrGrantAmountMustBePositive = models.NewValidationIssue(
ErrCodeGrantAmountMustBePositive,
"amount must be positive",
models.WithFieldString("amount"),
models.WithWarningSeverity(),
)
Comment on lines +7 to +12
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Severity mismatch: returning “warning” issues to fail the request.
You’re using WithWarningSeverity yet Validate() returns these as errors that block creation. Align severity with behavior.

Option A (preferred if available): switch to error severity.

 var ErrGrantAmountMustBePositive = models.NewValidationIssue(
 	ErrCodeGrantAmountMustBePositive,
 	"amount must be positive",
 	models.WithFieldString("amount"),
-	models.WithWarningSeverity(),
+	models.WithErrorSeverity(),
 )
 
 var ErrGrantEffectiveAtMustBeSet = models.NewValidationIssue(
 	ErrCodeEffectiveAtMustBeSet,
 	"effective at must be set",
-	models.WithFieldString("effective_at"),
-	models.WithWarningSeverity(),
+	models.WithFieldString("effectiveAt"),
+	models.WithErrorSeverity(),
 )

Option B (if no WithErrorSeverity exists): drop the explicit warning so it uses the default error severity.

-	models.WithWarningSeverity(),
+	// default to error severity

Also applies to: 16-21

🤖 Prompt for AI Agents
In openmeter/credit/errors.go around lines 7-12 (and similarly for lines 16-21),
the validation issues are created with WithWarningSeverity but Validate() treats
them as blocking errors; change the severity to an error by replacing
WithWarningSeverity() with WithErrorSeverity(), or if WithErrorSeverity() is not
available, remove the explicit WithWarningSeverity() call so the default (error)
severity is used, ensuring the declaration matches the runtime behavior.


const ErrCodeEffectiveAtMustBeSet models.ErrorCode = "grant_effective_at_must_be_set"

var ErrGrantEffectiveAtMustBeSet = models.NewValidationIssue(
ErrCodeEffectiveAtMustBeSet,
"effective at must be set",
models.WithFieldString("effective_at"),
models.WithWarningSeverity(),
)
Comment on lines +16 to +21
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Field selector casing likely mismatched with API (“effective_at” vs “effectiveAt”).
Use the JSON field to help clients pinpoint issues.

Apply this diff if you keep Option A above:

-	models.WithFieldString("effective_at"),
+	models.WithFieldString("effectiveAt"),
🤖 Prompt for AI Agents
In openmeter/credit/errors.go around lines 16 to 21, the field selector uses
snake_case "effective_at" which likely mismatches the API JSON field; update the
validation issue to use the JSON field name "effectiveAt" (e.g., change
models.WithFieldString("effective_at") to models.WithFieldString("effectiveAt"))
so clients receive the correct field path for errors; keep the rest of the error
construction and warning severity unchanged.

14 changes: 14 additions & 0 deletions openmeter/credit/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,24 @@ type CreateGrantInput struct {
Recurrence *timeutil.Recurrence
}

func (i CreateGrantInput) Validate() error {
if i.Amount <= 0 {
return ErrGrantAmountMustBePositive.WithAttr("amount", i.Amount)
}
if i.EffectiveAt.IsZero() {
return ErrGrantEffectiveAtMustBeSet.WithAttr("effective_at", i.EffectiveAt)
}
return nil
}

func (m *connector) CreateGrant(ctx context.Context, ownerID models.NamespacedID, input CreateGrantInput) (*grant.Grant, error) {
ctx, span := m.Tracer.Start(ctx, "credit.CreateGrant", cTrace.WithOwner(ownerID))
defer span.End()

if err := input.Validate(); err != nil {
return nil, err
}

return transaction.Run(ctx, m.GrantRepo, func(ctx context.Context) (*grant.Grant, error) {
tx, err := entutils.GetDriverFromContext(ctx)
if err != nil {
Expand Down
Loading