-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Invalid ng-repeat dupes error handling #9838
Comments
Would it be a good idea to wrap JSON.stringify inside toJson function in try-catch block? It would of course imply a change that toJson doesnt throw anything anymore (which wasnt documented anyway) so if someone used something like this below, it would be a breaking change. try{
angular.toJson(obj);
} catch(e) {
//dosomething
} Other solution would be to use some extra function in places such as the one reported to check if the object has circular references and remove them before stringify ex. http://stackoverflow.com/questions/14962018/detecting-and-fixing-circular-references-in-javascript I could prepare pull request if any of these solutions would be acceptable 👍 |
Thank you @kwypchlo . |
I prepared a fix so angular.toJson doesn't throw any exceptions and returns undefined if something went wrong but... I don't like it very much. I thought about it a while and as a developer, if I would use angular.toJson(someObj) and it would return undefined and not a string represantation of my object, I would be like However, in some cases like yours, you never intended to use angular.toJson on your object, and you are completely allowed to have a circular reference inside it. Nevertheless angular wanted to give you feedback on some other error and passed your object to angular.toJson. In such case you shouldn't be slapped with and exception. My new idea would be to add 3rd parameter to toJson called suppressExceptions, defaulted to false which would suppress exceptions. It would look like this: function toJson(obj, pretty, suppressExceptions) {
if (typeof obj === 'undefined') return undefined;
try {
return JSON.stringify(obj, toJsonReplacer, pretty ? ' ' : null);
} catch(e) {
// if suppressing exceptions, just return undefined
if( suppressExceptions ) return undefined;
throw e;
}
} Then we could find internal usages of toJson function inside angular and set suppressExceptions to true. Actually even better idea would be not to return undefined in such cases but use some helper function that detects and removes circular references and leaves rest of the object alone. This would allow to show at least most of the object. I've seen similar approach in dojox and it could be adopted. What do you guys think? |
Like @kwypchlo already mentioned, the framework is built around the notion that toJson will throw on cyclic objects, which also helps developers find the source of problems quickly. So you're correct that suppressing by default is not a good approach. I think this exact issue has been opened before, but I can't find it. It would be good to fix this, and I like the idea of adding a third argument to @petebacondarwin what do you think of adding a third parameter to |
Just chatted with @petebacondarwin, and here is the approach we want to take:
A PR would be welcome @kwypchlo! |
@jeffbcross I'm going back from vacations tomorrow and I will gladly contribute and prepare PR :) |
Adds third parameter called options to toJson function (for the time being undocumented, private). Adds support for suppressExceptions option (with test cases) that suppresses any exceptions that JSON.stringify might throw and returns undefined in such case. This fix has been discussed on issue angular#9838
…e error messages for user Makes use of suppressExceptions option parameter in toJson function. Closes angular#9838
Now that `minErr` can cope with objects that cannot be normally stringified to JSON, just pass the error arguments straight through without trying to stringify them first. Closes angular#9838 Closes angular#10065
Now that `minErr` can cope with objects that cannot be normally stringified to JSON, just pass the error arguments straight through without trying to stringify them first. Closes angular#9838 Closes angular#10065
Now that `minErr` can cope with objects that cannot be normally stringified to JSON, just pass the error arguments straight through without trying to stringify them first. Closes angular#9838 Closes angular#10065
When value can't be converted to JSON due to circular structure, dupes checking in ng-repeat fails to display a proper error message.
See
angular.js/src/ng/directive/ngRepeat.js
Line 368 in d3b1f50
toJson(value)
call fails if value contains circual reference. This is causing this error message, and hides the proper error message :Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys.
The text was updated successfully, but these errors were encountered: