Why am I getting an unhandled promise rejection error?

I have some back-end functions that have been working fine for many months. Recently, I’m starting to see errors, that come in pairs. This doesn’t always happen, it is spurrious:

“[”(node:1) UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘message’ of undefined\n at DefaultLogger.error (/elementory/node_modules/@wix/cloud-runtime-logger/src/DefaultLogger.ts:52:24)\n at logException (/elementory/node_modules/@wix/cloud-logging/src/timings.ts:42:14)\n at /elementory/node_modules/@wix/cloud-logging/src/timings.ts:55:13"]"

“[”(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see Command-line API | Node.js v20.5.0 Documentation). (rejection id: 1192)“]”

For the function identified ( getmydeviceinfos ), I am not referencing a ‘message’ property anywhere, and it looks to me like I am properly encapsulating all my await calls with a try…catch, so I am also not clear why I would be getting an unhandledPromiseRejectionWarning exception?

        case 'getmydeviceinfos' : {
            //console.log("getmydevicesinfo function called.");
            response.body = {[RESULT_KEY]:false}; // default false
            memberEmail = request.query[EMAIL_ARG]; 
            deviceId = request.query[DEVICEID_ARG];
            try {
                await isLoginForDeviceValid(deviceId, memberEmail, response);
            } catch (error) {
                response.body = {[RESULT_KEY]: false, 
                                 [ERRORMSG_KEY]:"Invalid website request detected for: " + memberEmail};
                return ok(response);                
            }
            
            if (!response.body.result) {
                // Failed to validate caller
                response.body = {[RESULT_KEY]: false, 
                                 [ERRORMSG_KEY]:"Invalid website request detected for: " + memberEmail};
                return ok(response);
            }
            let connectedMemberDevices = new Array(); // this may end up being empty.
            let currentMemberDeviceIDs = null;
            let results = null;
            try {
                // Need the devices for each connection
                //console.log("Checking for devices")
                results = await wixData.query(SITMEMBERS_TABLE)
                    .eq(EMAIL_ARG, memberEmail)
                    .include(MEMBERDEVICES_FIELD)
                    .find(options);
                if (results.items.length > 0) {
                    //console.log("Found devices for member");
                    currentMemberDeviceIDs = results.items[0].memberDevices;
                } else {
                    //console.log("Did not find any devices for member with ID: " + connection.connectedMember._id);
                    currentMemberDeviceIDs = null;
                }   
                if ( currentMemberDeviceIDs !== null ) {
                    for (var currentDevicesCntr = 0; 
                         currentDevicesCntr < currentMemberDeviceIDs.length; 
                         currentDevicesCntr++) {
                        connectedMemberDevices.push({[DEVICEID_FIELD]:currentMemberDeviceIDs[currentDevicesCntr].deviceId,
                        [DEVICEDESCRIPTION_FIELD]:currentMemberDeviceIDs[currentDevicesCntr].deviceDescription,
                        [FCMTOKEN_FIELD]:currentMemberDeviceIDs[currentDevicesCntr].fcmToken});
                    }
                    response.body = {[RESULT_KEY]: true, [MYDEVICEINFOS_KEY] : connectedMemberDevices};
                } else {
                    response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]:"No devices found for member"};
                }
                
            } catch (error) {
                response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]:"Could not get device info for: " + memberEmail};
            }
            // 
            return ok(response);
        }

The isLoginForDeviceValid() method looks like this:

/*
  This function determines whether a login is valid for the given device. 
  It also checks to make sure login should occur based on other conditions, and
  if not, indicates false in the body for result but also with an error message in the body 
  that identifies the reason for the false result value:
*/
async function isLoginForDeviceValid (deviceId, memberEmail, response, buildNumber) {
    // First see if we even know about this member
    let deviceToUpdate;
    //console.log("isLoginForDeviceValid function called - myDeviceId:" + deviceId + " email: " + memberEmail);
    if (typeof deviceId === 'undefined') {
        response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]: USER_ERROR_UNDEFINED};
        return "";
    }
    // Second see if the website is not in "Available" status
    //console.log("Checking site availability");
    let results;
    try {
        results = await wixData.query(SITEADMIN_TABLE)
            .eq(TITLE_FIELD,SITEADMIN_TITLE_AVAILABILITY)
            .find(options);
    } catch (error) {
        console.log("Got error in query for site admin table.");
        response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]:USER_ERROR_WEBSITE_UNAVAILABLE};
        return "";
    }

    if (results.items.length > 0) {

        var item = results.items[0];
        if ( item.value !== SITEADMIN_IS_AVAILABLE) {
            response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]:USER_ERROR_WEBSITE_UNAVAILABLE};
            return "";
        }
    } else {
        // This should never happen!
        //console.log("Could not find the SITEADMIN_AVAILABILITY entry!");
        response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]:USER_ERROR_WEBSITE_UNAVAILABLE};
        return "";
    }
    //console.log("Site available");

    //console.log("Gathering device info");
    let userid = "";
    try {
        results = await wixData.query(SITMEMBERS_TABLE)
            .eq(EMAIL_ARG, memberEmail)
            .include(MEMBERDEVICES_FIELD)
            .find(options);
    } catch (error) {
        console.log("Got error attempting to login user with email: " + memberEmail + " device NOT VALID.");
        response.body = {[RESULT_KEY]: false,[ERRORMSG_KEY] : error};
        return userid;
    }

    if (results.items.length === 0 ) {
        //console.log("Did not find member with email: " + memberEmail + " device NOT VALID.");
        response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]:USER_ERROR_USER_NOT_FOUND};
        return "";
    } else {
        //console.log("Devices gathered");
        // See if the user is suspended
        if ( results.items[0].isSuspended) {
            //console.log("Found suspended member: " + memberEmail);
            response.body = {[RESULT_KEY]: false, [ERRORMSG_KEY]:USER_ERROR_USER_IS_SUSPENDED};
            return "";
        }
        // Find user's associated devices and check them
        userid = results.items[0]._id;
        let memberDevices = results.items[0].memberDevices;
        //console.log("Validating current device against device list.");
        let foundCurrentId = false;
        let foundDescriptionForId = false;
        if ( memberDevices.length > 0) {
            for (var i = 0; i < memberDevices.length; i++) {
                var device = memberDevices[i];
                if (deviceId === device.deviceId) {
                    foundCurrentId = true;
                    deviceToUpdate = device;
                    if ( device.deviceDescription !== "") {
                        foundDescriptionForId = true;
                    }                           
                    break;
                }
            }
            if ( !foundCurrentId || !foundDescriptionForId) {
                response.body = {[RESULT_KEY]: false,[ERRORMSG_KEY] : "You have not registered this device."}; 
                return "";
            } else {
                response.body.result = true; 
                // See if we need to update the app build number being used by the user for this device
                if (typeof buildNumber !== 'undefined') {
                    deviceToUpdate.buildNumber = buildNumber;
                    try {
                        await wixData.update(MEMBERDEVICES_TABLE, deviceToUpdate, options);
                        //console.log("Updated device with build number.");
                    } catch (error) {
                        //console.log("Error updating build number on device");
                    }
                }

            }
        } else {
            response.body = {[RESULT_KEY]: false,[ERRORMSG_KEY] : "You have not registered this device."}; 
            return "";                  
        }
    }

    return userid;
    
}

This looks ok to me also, so what is causing these errors?

Tried taking a look, quite difficult to understand the code. At first glance, couldn’t find unhandled promises.

Maybe someone else has better reading skills than I have.

I am wondering if this is some kind of caching problem on the Wix back-end. In the past I had some references to a “message” property on error objects passed in the catch() clauses, but I fairly recently got rid of all of them, and now now longer reference the error object at all.

Welcome to the club. I have exactly the same thing happening to me as of August 22. Wix is looking into it, but so far nada. Will keep you posted if I get a solution.

P.S. It does not break any flow of my code, as far as I can see. If I had not checked my logs, I prob. would have never noticed. You?

EDIT: it looks like it is happening (to me) on a http-function. Looks like to you too. Can you confirm?

Yes, http-function. Also it doesn’t appear to break any flow of code. I just happen to notice it in the event window.

The other weird thing is the time/date stamp. See second error in this screenshot:

That time stamp could be another thing. Site Monitor is notoriously bad at labeling time stamps. If you pipe it to Google Operations, I’ll bet you a beer the time stamp is different (and close to the other one).

EDIT: interesting date, BTW. Unix epoch birth minus something. Bet you another beer you are on UTC -6. The missing millisecond? Prob. somebody/somewhere is trying to write “true” (-1) into a JS-date.

Interestingly enough, these errors have ceased. Anyone seeing them anymore?

Nope. They are gone at my end too. Spontaneous cure, I love it :wink: