internal/mcp/jsonschema: generalize error wrapping
Add a wrapf function to simplify defers that wrap errors. Change-Id: If4bedec7d3f56f609d3ca4620aebe7637a59f6bf Reviewed-on: https://go-review.googlesource.com/c/tools/+/676475 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
parent
e3938ce68c
commit
4e9d520c2e
3 changed files with 9 additions and 10 deletions
|
|
@ -69,11 +69,7 @@ func parseJSONPointer(ptr string) (segments []string, err error) {
|
|||
// This implementation suffices for JSON Schema: pointers are applied only to Schemas,
|
||||
// and refer only to Schemas.
|
||||
func dereferenceJSONPointer(s *Schema, sptr string) (_ *Schema, err error) {
|
||||
defer func() {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("JSON Pointer %q: %w", sptr, err)
|
||||
}
|
||||
}()
|
||||
defer wrapf(&err, "JSON Pointer %q", sptr)
|
||||
|
||||
segments, err := parseJSONPointer(sptr)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -282,3 +282,10 @@ func assert(cond bool, msg string) {
|
|||
panic("assertion failed: " + msg)
|
||||
}
|
||||
}
|
||||
|
||||
// wrapf wraps *errp with the given formatted message if *errp is not nil.
|
||||
func wrapf(errp *error, format string, args ...any) {
|
||||
if *errp != nil {
|
||||
*errp = fmt.Errorf("%s: %w", fmt.Sprintf(format, args...), *errp)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,11 +42,7 @@ type state struct {
|
|||
|
||||
// validate validates the reflected value of the instance.
|
||||
func (st *state) validate(instance reflect.Value, schema *Schema, callerAnns *annotations) (err error) {
|
||||
defer func() {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("%s: %w", schema, err)
|
||||
}
|
||||
}()
|
||||
defer wrapf(&err, "validating %s", schema)
|
||||
|
||||
st.stack = append(st.stack, schema) // push
|
||||
defer func() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue