From f04894fcf5d248d1e49b3457e9a23295e0f1bbcc Mon Sep 17 00:00:00 2001 From: Alberto Ricart Date: Fri, 13 Dec 2024 11:08:36 -0600 Subject: [PATCH] Refactor export validation error handling. 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 --- cmd/addexport.go | 10 ++++++---- cmd/editexport.go | 22 +++++++++++++++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cmd/addexport.go b/cmd/addexport.go index 22df4a96..4e91d8fb 100644 --- a/cmd/addexport.go +++ b/cmd/addexport.go @@ -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 @@ -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 diff --git a/cmd/editexport.go b/cmd/editexport.go index 92ba9888..6de579ac 100644 --- a/cmd/editexport.go +++ b/cmd/editexport.go @@ -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 { @@ -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 {