Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Server Scripts - Are not allowing Try{} catch {} on Important DB Operations, and fail out to the 'outer' catch (e) state

All server scripts - when faced with an DBError - abort - rather than allowing the user to trap for the Error condition (which should be allowed).....

Consider the following Code from a Server script:

function fMassChange(databaseId, collectionName, sWhere, oUpdate, sMasterKey) {
var bReturn = false;
try {
console.log('[JS-INFO] fMassChange running Update in Table: ', collectionName, ' Where Clause ', sWhere, ' Update JSON: ', oUpdate, ' MasterKey: ', sMasterKey);
var oMassUpdate = Collection.multiUpdateObject(databaseId, collectionName, sWhere, oUpdate, null, sMasterKey);
// var result = Collection.multiUpdateObject(databaseId, collectionName, '{"bScannedForDupes": true }', {
// "bScannedForDupes": false, "bIsDupe" : null , "bIsUniqueRow" : null
// }, null, sMasterKey);

Code: Select all

     console.log('[JS-INFO] fMassChange Complete - Result:', oMassUpdate); 
     bReturn = true; 
 } catch (e) { 
     bReturn = false; 
     console.warn('[JS-ERROR] running fMassChange: ', e, ' Action Was in Log Record Immediately Prior.... '); 
 } 
 return bReturn; 

}

When this function is called in the server script like this:
try {....lots of other logic above this.....
//
// Change the OnBoarding Tasks to a blank mentor ID
//
collectionName = 'OnBoardingTasks';
var oMyUpdate = {
"sMentorID": null
};
var sWhere = '{"sOnBoardingEventsID": ' + oEvent[0]._id + ' } ';
fMassChange(dbId, collectionName, sWhere, oMyUpdate, sMasterKey);

} catch (e) {
console.log('there was a Fetal error during action: ', sAction, ' Error Message: ', e.message, ' Error ncode was:', e.code);
responseBody.actionTaken = "Error" + ' during ' + sAction;
responseBody.result = "Error"
responseBody.error = "Fetal Error During Execution - Execution Halted - Error Message: " + e.message + "ncode: " + e.code;
response.error(responseBody, 500);

}

//==========================================================
So - there's a syntax error in the string based where clause required by the multiUpdateObject in the above call.

The expected behavior is that - inside the function call - the code 'fails out' to the catch(e) statement inside the function call. However, it does not - and it fails out to the OUTER catch(e) statement - which abruptly terminates the script - unexpectedly.

This type of result occurs whether or not the multiUpdateObject statement is in a function call - or part of the main script (I moved it to a function to isolate the behavior and debug the error).

This behavior happens in other database functions - and - is a recent behavior. For Example - when an Unique Index is violated by entering a duplicate key - I'd like to trap that behavior - because the error is sometimes 'desired' and results in another set of actions. Yet - it cannot be trapped and results in this error:

there was a Fetal error during action: [JS-INFO] Adding Record to table OnBoardingTasks :{"sTaskName":"Acccompany OnBoarder to Veterans Monthly meeting","sUserID":"5a9092ac0326853b6f214d5a","dDueDate":"2018-02-26T15:54:24.968Z","sTaskID":"59f0ef77f494ed09c0111e81","sAssignmentType":"MANAGER","sOnBoarderUID":"5a957f300326853b6f2209cf","sManagerID":"5a9092ac0326853b6f214d5a","bActive":true,"bComplete":false,"sColor":"#EE82EE","sTaskListID":"59f0ef77f494ed09c0111e8c","sUserName":"a href="mailto:bruce.stuart@amazingorg.com" rel="nofollow"bruce.stuart@amazingorg.com/a","sOrgID":"59f0ef76f494ed09c0111e66"} Error Message: Index does not allow fields with non-unique values Error ncode was: DBSC217

Again - the error is expected - the part that's NOT expected - is that the try / catch loop that was around the code to do the insertObject - is being bypassed - and the code errors out to the 'outer loop'.

I know there are workarounds, but the workaround is to write additional code to account for things that should be handle-able inside the catch(e) code. Most Appery users will NOT be able to do this.

Have you guys seen this problem before? do you need a compact script that illustrates the issue ? I have granted you access to my script if you'd like to view the code. Please advise if this issue can be fixed.

it is here: https://api.appery.io/rest/1/code/Peo...

Best,

Bruce

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Server Scripts - Are not allowing Try{} catch {} on Important DB Operations, and fail out to the 'outer' catch (e) state

Hello Bruce,

Could you please show us simple example to understand what is going wrong there? What function do you expect to have there? It is not clear.

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Server Scripts - Are not allowing Try{} catch {} on Important DB Operations, and fail out to the 'outer' catch (e) state

Serhii,

Here is a more simple example script (Shown in the picture of the script below - along with what I expect to happen listed in the photo).

Image

As stated above:

What I expect:

  • Any execution error , including a Database constraint error (like attempting to add a duplicate key to a table with a unique index on it) - errors out TO MY CATCH (E) LOOP as shown above.

    What is occurring - and what should NOT occur (ie - this is a defect in the infrastructure that is running my script):

  • The script terminates, does NOT transfer execution to my Catch (e) statement.

    Here is the log file from the script (there's good logging to help debug issues just like this):

    20.4.2018, 8:34:01 PM: Script IAPConsumable_Record_Consumption: TypeError: :43:12 ApperyioAPI: Index does not allow fields with non-unique values is not an Object
    20.4.2018, 8:34:01 PM: [JS-INFO] - Inserting Record to Consumable History... { sProductOwnedKey: '5ad6557308bb3f0994bee328', sProductID: 'FaceFitTest', sBundleID: 'com.stouteindustries.thefittest', sUserID: '5ad587c28de72209a0841767', dTranDate: '2018-04-20 03:34:01.462', sReceiptID: '123456', sUserDBKey: '57e014ede4b0d6e66faef2fc', sUserName: '93E1B358-B12F-4B9C-8A5C-067BA4D05078', nQuantity: -1, sDescription: 'Consume Fit Test on 04/19/2018 at 10:00pm PST' }
    20.4.2018, 8:34:01 PM: [JS-INFO] Retrieving Product Ownership Record from DB- Success - Rows(s) returned: [ { _id: '5ad6557308bb3f0994bee328', dLastAccessed: { '$date': '2018-04-19T00:54:42.593Z' }, sUserID: '5ad587c28de72209a0841767', nConsumableBalance: 6, sProductID: 'FaceFitTest', sStorePopUpDescription: 'This Product allows you to purchase the face fitness test consumable', sStore: 'IOS', dLastUsed: { '$date': '2018-04-19T00:54:42.592Z' }, sUserType: 'CONSUMABLE', bConsumable: true, bExpiredTrial: true, sUserName: '93E1B358-B12F-4B9C-8A5C-067BA4D05078', dSubscriptionStarts: { '$date': '2018-04-19T00:54:42.602Z' }, dAdded: { '$date': '2018-04-17T20:13:39.752Z' }, sBundleID: 'com.stouteindustries.thefittest', sAlias: 'FaceFitTest', bConsumed: false, _createdAt: { '$date': '2018-04-17T20:13:39.779Z' }, _updatedAt: { '$date': '2018-04-19T00:54:42.652Z' }, dSubscriptionEnds: { '$date': '1970-01-01T00:00:00.000Z' }, nLevel: 1, bExpiredSubscription: false, dLastPurchase: { '$date': '2018-04-19T00:54:42.601Z' }, nQtyPerPurchase: 1, sLastTransactionID: '1000000391716360' } ]
    20.4.2018, 8:34:01 PM: [JS-INFO] Retrieving Product Ownership Record from DB
    20.4.2018, 8:34:01 PM: [JS-INFO] Recording Consumption for bundle: com.stouteindustries.thefittest Product: FaceFitTest User: 5ad587c28de72209a0841767 Quantity : 1 Description of Transaction: Consume Fit Test on 04/19/2018 at 10:00pm PST Receipt ID: 123456
    20.4.2018, 8:34:01 PM: [JS-INFO] Request at start is: { requestBody: '', requestParams: { nQuantity: '1', sUserID: '5ad587c28de72209a0841767', sDescription: 'Consume Fit Test on 04/19/2018 at 10:00pm PST', sReceipt: '123456', sProductID: 'FaceFitTest', sUserDBKey: '57e014ede4b0d6e66faef2fc', sUserName: '93E1B358-B12F-4B9C-8A5C-067BA4D05078', sBundleID: 'com.stouteindustries.thefittest', sTransactionID: '123456' }, method: 'POST' }

    The name of the script is:

    IAPConsumable_Record_Consumption

    and it has been shared with support.

    Please review the script - and advise as soon as possible. I'd prefer not to continue to code around this defect.

    Best,

    Bruce

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Server Scripts - Are not allowing Try{} catch {} on Important DB Operations, and fail out to the 'outer' catch (e) state

PS - it's my hunch - that when you guys moved up the Database Support from whatever the older version of the Mongo DB was - to the newer version you are supporting now - you broke this and created this undesireable set of outcomes.

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Server Scripts - Are not allowing Try{} catch {} on Important DB Operations, and fail out to the 'outer' catch (e) state

Could you check the trace tab to see exceptions? Or the provided log is a trace tab?

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Server Scripts - Are not allowing Try{} catch {} on Important DB Operations, and fail out to the 'outer' catch (e) state

Serhii - the provided log - IS the trace tab.

The 'exception' is what is supposed to have been caught by the 'catch (e) statement.'.

Bruce

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Server Scripts - Are not allowing Try{} catch {} on Important DB Operations, and fail out to the 'outer' catch (e) state

Here's the 'exception' that stops execution of the script - which is NOT supposed to stop execution of the script - it IS supposed to transfer execution to the catch (e) portion of the script - so it can be handled gracefully - instead of killing the execution of the script.

20.4.2018, 8:34:01 PM: Script IAPConsumable_Record_Consumption: TypeError: :43:12 ApperyioAPI: Index does not allow fields with non-unique values is not an Object

(First line of the log - which - obviously is the LAST line of the log)

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Server Scripts - Are not allowing Try{} catch {} on Important DB Operations, and fail out to the 'outer' catch (e) state

This log gives you / the developers everything they need to recreate the issue.

The bottom part of the log - tells you what the value of EACH of the inbound parameters was a the time of execution (so that they can simply enter the same parameters to recreate this issue - with this script) - and the log shows in precise detail - where execution stopped.and why it stopped.

The screen print - as well as examination of the script - can easily show you what should have occurred - versus what did occur.

My belief - your code that executes the scripts - has a syntax error in it - as characterized by the 'unusual' error message that is thrown from the script:

"ApperyioAPI: Index does not allow fields with non-unique values is not an Object"

(the last part of the error ...."Is not an object" - is telling that the code that's trying to gracefully throw the exception back to the user - has a syntax or other error in it)

Best,

Bruce

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

Server Scripts - Are not allowing Try{} catch {} on Important DB Operations, and fail out to the 'outer' catch (e) state

We processing your request and we will let you know when we got any news from our development team.

Return to “Issues”