Skip to content

Commit

Permalink
Refactor export validation error handling.
Browse files Browse the repository at this point in the history
Improve error reporting by replacing direct error returns with detailed validation result handling. This ensures better clarity and consistency in identifying and addressing validation errors in both `addexport` and `editexport` commands.

Signed-off-by: Alberto Ricart <alberto@synadia.com>
  • Loading branch information
aricart committed Dec 13, 2024
1 parent 8305343 commit f04894f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
10 changes: 6 additions & 4 deletions cmd/addexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ func (p *AddExportParams) Validate(ctx ActionCtx) error {
}
// get the old validation results
var vr jwt.ValidationResults
if err = p.claim.Exports.Validate(&vr); err != nil {
return err
p.claim.Exports.Validate(&vr)
if len(vr.Errors()) != 0 {
return vr.Errors()[0]
}

// if we have a latency report subject create it
Expand All @@ -296,8 +297,9 @@ func (p *AddExportParams) Validate(ctx ActionCtx) error {
p.claim.Exports.Add(&p.export)

var vr2 jwt.ValidationResults
if err = p.claim.Exports.Validate(&vr2); err != nil {
return err
p.claim.Exports.Validate(&vr2)
if len(vr2.Errors()) != 0 {
return vr2.Errors()[0]
}

// filter out all the old validations
Expand Down
22 changes: 17 additions & 5 deletions cmd/editexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,17 @@ func (p *EditExportParams) Run(ctx ActionCtx) (store.Status, error) {
old := *p.claim.Exports[p.index]
// old vr
var vr jwt.ValidationResults
if err := p.claim.Exports.Validate(&vr); err != nil {
return nil, err
r := store.NewDetailedReport(false)

p.claim.Exports.Validate(&vr)
if len(vr.Errors()) != 0 {
errs := vr.Errors()
for _, err := range errs {
r.AddFromError(err)
}
return r, vr.Errors()[0]
}

r := store.NewDetailedReport(false)
var export jwt.Export
export.Name = p.name
if export.Name != old.Name {
Expand Down Expand Up @@ -437,9 +443,15 @@ func (p *EditExportParams) Run(ctx ActionCtx) (store.Status, error) {
p.claim.Exports[p.index] = &export

var vr2 jwt.ValidationResults
if err := p.claim.Exports.Validate(&vr2); err != nil {
return nil, err
p.claim.Exports.Validate(&vr2)
if len(vr2.Errors()) != 0 {
errs := vr2.Errors()
for _, err := range errs {
r.AddFromError(err)
}
return r, vr2.Errors()[0]
}

// filter out all the old validations
uvr := jwt.CreateValidationResults()
if len(vr.Issues) > 0 {
Expand Down

0 comments on commit f04894f

Please sign in to comment.