[
https://issues.apache.org/jira/browse/CB-11223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15274793#comment-15274793
]
ASF GitHub Bot commented on CB-11223:
-------------------------------------
Github user TanaseButcaru commented on a diff in the pull request:
https://github.com/apache/cordova-plugin-contacts/pull/125#discussion_r62396222
--- Diff: www/convertUtils.js ---
@@ -28,10 +28,19 @@ module.exports = {
*/
toCordovaFormat: function (contact) {
var value = contact.birthday;
- try {
- contact.birthday = new Date(parseFloat(value));
- } catch (exception){
- console.log("Cordova Contact toCordovaFormat error: exception
creating date.");
+ if (value !== null) {
+ try {
+ contact.birthday = new Date(parseFloat(value));
+
+ //we might get 'Invalid Date' which does not throw an error
+ //and is an instance of Date.
+ if (isNaN(contact.birthday.getTime())) {
+ contact.birthday = null;
--- End diff --
The original logic was: try to get new date, if error, do nothing (so leave
the contact.birthday as it is).
There is no problem with the original code, but it just not catches all
errors and in this case is the 'Invalid Date' error which never throws.
The error I was getting on iOS was caused by a third party js lib that was
iterating through contact props, checking if the prop was an object and it was
then was doing a toJSON() and because contact.birthday was an object but in
form of an error, that was the key element that made safari throw an error.
Firefox and Chrome does not throw an error if toJSON() is called on an 'Invalid
Date' object.
So the error was indirectly caused by contacts plugin.
> Better check for date validity of contact.birthday / Fix: RangeError Invalid
> Date
> ---------------------------------------------------------------------------------
>
> Key: CB-11223
> URL: https://issues.apache.org/jira/browse/CB-11223
> Project: Apache Cordova
> Issue Type: Improvement
> Components: Plugin Contacts
> Environment: Desktop Firefox & Safari
> Android & iOS Webview
> Reporter: Tanase Butcaru
> Priority: Critical
> Labels: easyfix
>
> The issue: on Safari & iOS webview if we perform a _toJSON()_ on a instanceof
> Date object which is a 'Invalid Date' object, that throws an error. This does
> not happen on Firefox & Chrome!
> I encountered this issue while sending device contacts over _socket.io_ which
> iterates over contacts props and does a _toJSON()_, but the problem was that
> the Date object from _birthday_ property was not valid.
> Simple test case:
> {code:javascript}
> var contact = { birthday: null }; //data provided by cordova-plugin-contacts
> try {
> contact.birthday = new Date(parseFloat(contact.birthday));
> }
> catch(e) {
> console.log(e); //this will never be called
> }
> //where the error occures..
> contact.birthday.toJSON();
> {code}
> An 'Invalid Date' error does not throw anything, so a simple try-catch block
> doesn't pass the validity of that date.
> The changes I made in _convertUtils.js_ resolves this problem.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]