json2typescript
json2typescript copied to clipboard
Structured errors
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:
- 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.
- 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.
- 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".
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.