Skip to content

Commit

Permalink
encoding/jsonschema: only one error per URL
Browse files Browse the repository at this point in the history
When processing a schema with many references, if there
is an error mapping one URL to an import path, it's
likely that there will be the same error on all other
similar URLs, but producing many near-identical error
messages is just noise, so suppress all but the first
error for any given URL.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: I8284e641a68c54b997db4f69673a213757eb27c3
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199307
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
rogpeppe committed Aug 13, 2024
1 parent 709741d commit 8e99f5e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
7 changes: 4 additions & 3 deletions encoding/jsonschema/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ const rootDefs = "#"

// A decoder converts JSON schema to CUE.
type decoder struct {
cfg *Config
errs errors.Error
numID int // for creating unique numbers: increment on each use
cfg *Config
errs errors.Error
numID int // for creating unique numbers: increment on each use
mapURLErrors map[string]bool
}

// addImport registers
Expand Down
2 changes: 0 additions & 2 deletions encoding/jsonschema/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ properties: {
qt.Assert(t, qt.Equals(errors.Details(err, nil), `
cannot determine import path from URL "https://something.test/foo": some error:
foo.cue:4:5
cannot determine import path from URL "https://something.test/foo": some error:
foo.cue:5:5
`[1:]))
}

Expand Down
5 changes: 4 additions & 1 deletion encoding/jsonschema/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ func Extract(data cue.InstanceOrValue, cfg *Config) (f *ast.File, err error) {
cfg = &cfg1
cfg1.MapURL = DefaultMapURL
}
d := &decoder{cfg: cfg}
d := &decoder{
cfg: cfg,
mapURLErrors: make(map[string]bool),
}

f = d.decode(data.Value())
if d.errs != nil {
Expand Down
7 changes: 6 additions & 1 deletion encoding/jsonschema/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ func (s *state) makeCUERef(n cue.Value, u *url.URL, fragmentParts []string) (_e
// are not in scope.
importPath, err := s.cfg.MapURL(u)
if err != nil {
s.errf(n, "cannot determine import path from URL %q: %v", u, err)
ustr := u.String()
// Avoid producing many errors for the same URL.
if !s.mapURLErrors[ustr] {
s.mapURLErrors[ustr] = true
s.errf(n, "cannot determine import path from URL %q: %v", ustr, err)
}
return nil
}
ip := module.ParseImportPath(importPath)
Expand Down

0 comments on commit 8e99f5e

Please sign in to comment.