json2typescript icon indicating copy to clipboard operation
json2typescript copied to clipboard

Structured errors

Open dleavitt opened this issue 5 years ago • 1 comments

The current error format is good for reading in the console, but is not very suitable for sending to error reporting services or otherwise dealing with in a programmatic way, as it's a big string.

Instead, could throw an error subclass with the same fields as the message uses, something like:

{ 
  classId: instance[Settings.CLASS_IDENTIFIER],
  classProperty: classPropertyName,
  classPropertyValue: classInstancePropertyValue,
  expectedType: this.getExpectedType(expectedJsonType),
  runtimeType: this.getTrueType(classInstancePropertyValue),
  jsonProperty: jsonPropertyName,
  jsonValue: value,
  children: e.jsonConvertErrorDetails, // nested child errors
}

Some other things that seem pretty useful for both error reporting and readability:

  1. Have the option to omit the "JSON value" from the error message / hypothetical error properties above. For errors in the leaves of nested json structures, this property can make the error pretty unwieldy.
  2. Consider displaying errors from the top-down instead of the bottom-up, with the leaf error displayed first. This is usually what you're interested in.
  3. If adding the above properties, consider an option to simplify the displayed messages to just the heading of the lowermost error. For instance, if you're fetching an array of blog posts and one of them is bad, error.message would just be: "Fatal error in JsonConvert. Failed to map the JSON object to the class "BlogPost" because the defined JSON property "title" does not exist".

dleavitt avatar May 21 '19 00:05 dleavitt

Hey, thanks for your input. In fact I had already something like this in mind for the next major release. I'll keep this issue open so we'll make sure that it is included in the future.

andreas-aeschlimann avatar May 21 '19 20:05 andreas-aeschlimann