Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved issue calling method on null object #46

Merged
merged 1 commit into from
May 4, 2018
Merged

Resolved issue calling method on null object #46

merged 1 commit into from
May 4, 2018

Conversation

davidkirwan
Copy link
Contributor

@davidkirwan davidkirwan commented May 1, 2018

JIRA: https://issues.jboss.org/browse/RHMAP-20186

What

Getting multiples of the following exception in the fh-mbaas logs on tom1-mbaas2-ship2:

FATAL: UncaughtException, please report: TypeError: Cannot read property 'error' of undefined
    at /opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/lib/impl/dataSources/updateCache.js:24:33
    at /opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/node_modules/underscore/underscore.js:436:8
    at Function._.each._.forEach (/opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/node_modules/underscore/underscore.js:145:9)
    at Function._.partition (/opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/node_modules/underscore/underscore.js:435:7)
    at updateValidAndInvalidDataSources (/opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/lib/impl/dataSources/updateCache.js:23:24)
    at /opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/lib/impl/dataSources/updateCache.js:155:35
    at /opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/node_modules/async/lib/async.js:229:13
    at /opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/node_modules/async/lib/async.js:110:21
    at /opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/node_modules/async/lib/async.js:24:16
    at /opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/node_modules/async/lib/async.js:226:17
    at /opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/node_modules/async/lib/async.js:482:30
    at /opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/lib/impl/dataSources/updateCache.js:232:18
    at /opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/node_modules/mongoose/lib/model.js:3911:16
    at /opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/node_modules/mongoose/lib/services/model/applyHooks.js:167:17
    at /opt/feedhenry/fh-mbaas/releases/5.10.4-5559b0e/node_modules/fh-forms/node_modules/fh-logger/node_modules/continuation-local-storage/node_modules/async-listener/glue.js:188:31
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Why

fh-forms/lib/impl/dataSources/updateCache.js:24:33 contains the following:

var validInvalid = _.partition(dataSourcesToSplit, function(dataSourceUpdateData) {
    return !dataSourceUpdateData.error;
  });

I've not managed to figure out why yet, but it is possible for dataSourcesToSplit to be empty, or a particular element of the array to not have a error method. The fix will handle such situations.

How

The following snippet shows how the non existance of an object will no longer throw exception if checking is performed in this fashion:

> var f = function(test){ test.a; }
undefined
> f(undefined);
TypeError: Cannot read property 'a' of undefined
    at f (repl:1:29)
    at repl:1:1
    at REPLServer.defaultEval (repl.js:262:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:431:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:211:10)
    at REPLServer.Interface._line (readline.js:550:8)
> var f2 = function(test){
... if(test){
..... return !test.a;
..... }
... return true;
... }
undefined
> f2(undefined);
true
> f2({a:false});
true
> f2({a:true});
false
> 

@coveralls
Copy link

coveralls commented May 1, 2018

Coverage Status

Coverage decreased (-0.01%) to 74.979% when pulling 4b1c3ab on davidkirwan:RHMAP-20186_fh-forms-updateCache-exception into 0a198f8 on feedhenry:master.

@@ -21,7 +21,11 @@ function updateValidAndInvalidDataSources(dataSourcesToSplit, currentInvalidData
logger.debug("dataSourcesToSplit", dataSourcesToSplit);
currentInvalidDataSources = currentInvalidDataSources || [];
var validInvalid = _.partition(dataSourcesToSplit, function(dataSourceUpdateData) {
return !dataSourceUpdateData.error;
if (typeof dataSourceUpdateData.error === 'function') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @davidkirwan. I have a few questions. How do we know that dataSourceUpdateData.error should always be a function? and if it is not a function, the logic is now returning true and that there is no error. If this logic is true that it is a function, it then returns !dataSourceUpdateData.error which is not invoking the function it self.

@@ -720,8 +720,8 @@ module.exports.test = {
forms.dataSources.updateCache(options, [dataSourceDataSingleChoice], {
currentTime: new Date()
}, function(err, badUpdateResult){
assert.ok(err, "Expected An Error When Updating Data Sources With Bad Data");
assert.equal(ERROR_CODES.FH_FORMS_UNEXPECTED_ERROR, err.code, "Expected A FH_FORMS_UNEXPECTED_ERROR");
//assert.ok(err, "Expected An Error When Updating Data Sources With Bad Data: " + util.inspect(err));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were these assertions commented out by accident?

@camilamacedo86
Copy link
Contributor

Failling in the tests.

  164 passing (36s)
  1 failing
  1) test Test Update Data Source Data Set Wrong Format:
      Uncaught AssertionError: Expected 0 valid update
      + expected - actual
      -0
      +1
      
      at test/accept/test_dataSource.js:729:18
      at prepareResponse (lib/impl/dataSources/updateCache.js:9:16764)
      at fn (node_modules/async/lib/async.js:579:34)
      at Immediate.<anonymous> (node_modules/async/lib/async.js:495:34)
      at Immediate.<anonymous> (node_modules/fh-logger/node_modules/continuation-local-storage/node_modules/async-listener/glue.js:188:31)

@davidkirwan
Copy link
Contributor Author

@camilamacedo86 @davidffrench Folks I'll add you as reviewers when I'm ready to have other eyes on the PR :) until then please ignore ! I cannot run these builds successfully locally, so having to rely on Jenkins/Travis builds to have the tests run etc for feedback.

@davidffrench
Copy link
Contributor

ah sorry @davidkirwan :-)

@@ -21,7 +21,11 @@ function updateValidAndInvalidDataSources(dataSourcesToSplit, currentInvalidData
logger.debug("dataSourcesToSplit", dataSourcesToSplit);
currentInvalidDataSources = currentInvalidDataSources || [];
var validInvalid = _.partition(dataSourcesToSplit, function(dataSourceUpdateData) {
return !dataSourceUpdateData.error;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion to fix it follows. ( do just the following code )

if ( dataSourceUpdateData ){
    return !dataSourceUpdateData.error;
} 
return true;

PS. In JS/Node to check if the variable is != of undefined you can just if (var)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original exceptions being raised are the following:

TypeError: Cannot read property 'error' of undefined

There seem to be situations where dataSourceUpdateData is being passed and is undefined, and calling .error on it raises an exception. So the suggested fix you make will not resolve this see the following as an example:

> var f = function(test){ test.a; }
undefined
> f(undefined);
TypeError: Cannot read property 'a' of undefined
    at f (repl:1:29)
    at repl:1:1
    at REPLServer.defaultEval (repl.js:262:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:431:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:211:10)
    at REPLServer.Interface._line (readline.js:550:8)
> 

Now compare to the fix I'm proposing:

> var f2 = function(test) {
... if(typeof test === 'undefined'){
..... return true;
..... }
... else{
..... return test.a;
..... }
... }
undefined
> 
> 
> f2({a:1});
1
> f2(undefined);
true
> 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opps my bad! I'm wrong here, this does appear to work!

> var f = function(test){
... if(test){
..... return !test.a;
..... }
... return true;
... }
undefined
> f(undefined);
true
> f({a:false});
true
> f({a:true});
false
> 

@davidkirwan
Copy link
Contributor Author

@camilamacedo86 @davidffrench the PR should now be in a position to be reviewed please!

@@ -720,7 +720,7 @@ module.exports.test = {
forms.dataSources.updateCache(options, [dataSourceDataSingleChoice], {
currentTime: new Date()
}, function(err, badUpdateResult){
assert.ok(err, "Expected An Error When Updating Data Sources With Bad Data");
assert.ok(err, "Expected An Error When Updating Data Sources With Bad Data: " + util.inspect(err));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO you should not change the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is fine, as it aided in debugging that function. By adding the contents of the err object there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@davidffrench davidffrench left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the one comment from Camila, looks good to me

Copy link
Contributor

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shows fine the change ( LGTB for me ).
For we are able to merge it:

  • Rebase with master
  • Bump version and CHANGELOG file

@@ -720,7 +720,7 @@ module.exports.test = {
forms.dataSources.updateCache(options, [dataSourceDataSingleChoice], {
currentTime: new Date()
}, function(err, badUpdateResult){
assert.ok(err, "Expected An Error When Updating Data Sources With Bad Data");
assert.ok(err, "Expected An Error When Updating Data Sources With Bad Data: " + util.inspect(err));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@davidkirwan
Copy link
Contributor Author

@camilamacedo86 seems since the rebase its got new changes you added in PR #45 can you take a look at the travis output perhaps you can determine why its failing based on your new changes?

@camilamacedo86
Copy link
Contributor

@davidkirwan all PR's before this one worked fine in the CI/Trevis. If it is not working shows that something broke in the rebase. I will give a look and let you know.

@davidffrench
Copy link
Contributor

@camilamacedo86 @davidkirwan Travis can fail sometimes, always worth restarting the build when you think the build should have passed. I have restarted it now.

@davidffrench
Copy link
Contributor

@davidkirwan Build has passed now

@davidkirwan
Copy link
Contributor Author

Repeatable builds would be a nice feature to have eh. @davidffrench @camilamacedo86 if you are happy can you approve the PR. Thanks!

@camilamacedo86
Copy link
Contributor

@davidkirwan it still failing in the coverage/coveralls which means that you need to improve the tests in order to cover this scenario.

@davidkirwan
Copy link
Contributor Author

@camilamacedo86 feel free to suggest an improvement here.

@camilamacedo86
Copy link
Contributor

camilamacedo86 commented May 2, 2018

@davidkirwan the coverage check is automatic. To check it see the following:

screen shot 2018-05-02 at 16 05 52

Click on the datails for further information.

For you be allowed to merge it the PR need pass in all automatic checks + the code review ( some approval )

I'd like to recommend you add a test here which covers the scenario which was fixed in the PR.

PS.: I approved the PR but please wait for the @davidffrench confirmation too.
@davidffrench could we merge it without adding more tests here? ( the coverage/coveralls — Coverage decreased (-0.01%) to 74.979% is falling because it is -0,01% , however for this case I think it is fine and more tests will be unnecessary )

@camilamacedo86 camilamacedo86 merged commit cbd257a into feedhenry:master May 4, 2018
@camilamacedo86
Copy link
Contributor

@davidkirwan merged

@davidkirwan davidkirwan deleted the RHMAP-20186_fh-forms-updateCache-exception branch May 4, 2018 10:33
@davidkirwan
Copy link
Contributor Author

@camilamacedo86 @davidffrench @grdryn cheers folks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants