corenova / yang-js

YANG parser and composer
Apache License 2.0
56 stars 18 forks source link

Validation errors should show "path" to failure and/or name of failing portion. #122

Closed andreas-ibm closed 4 years ago

andreas-ibm commented 4 years ago

Enhancement please!

Subject says it all really, for example in list.js there is a check:

      if (!(obj instanceof Object)) {
        throw this.error("list item must be an object");
      }

This doesn't help me much when there's a large tree it's parsing (I tried adding +typeof obj to the error message, which told me it was of type object which ground my gears).

or this one:

      if (!(this.mutable || (value == null) || opts.force)) {
        throw this.error("cannot set data on read-only (config false) element");
      }

which gave me no clue what I was doing wrong! (I ended up commenting the lines out just to keep going!).

thanks!

sekur commented 4 years ago

The thrown error object has the src and ctx properties that provides more info about where the error was thrown.

You can try/catch (error) and access those properties as error.src and error.ctx

Hope this helps!

andreas-ibm commented 4 years ago

It does indeed, thank you!