Skip to content

Listener exception generates status 400, error should contain contextual error information #318

Open
@semireg

Description

@semireg

Hi,

It took me a few days to track down the source of a strange subscription bug.

In my case, I had a listener with a presence function such as:

  presence: (presenceEvent) => {
    const { uuid, action } = presenceEvent;
    const someVar = null.replace('foo', 'bar'); // Throws

Upon connection and subscription withPresence, this generates a status listener event with code 400 and empty errorData:

statusEvent:{
   "statusCode": 400,
   "error": true,
   "operation": "PNSubscribeOperation",
   "errorData": {},
   "category": "PNUnknownCategory"
}

I went in and added debug to show me the e.stack on line 237 of core/components/endpoint.js:

    responseP
      .then((result) => {
        if (callback) {
          callback(status, result);
        } else if (promiseComponent) {
          promiseComponent.fulfill(result);
        }
      })
      .catch((e) => {
        console.log(`onResponse caught error at ${JSON.stringify(e.stack, null, 3)}`);  // DEBUG HERE
        
        if (callback) {
          let errorData = e;

          if (endpoint.getOperation() === operationConstants.PNSubscribeOperation) {
            errorData = {
              statusCode: 400,
              error: true,
              operation: endpoint.getOperation(),
              errorData: e,
              category: categoryConstants.PNUnknownCategory,
            };
          }

          callback(errorData, null);
        } else if (promiseComponent) {
          promiseComponent.reject(new PubNubError('PubNub call failed, check status for details', e));
        }
      });

The response was pretty obvious:

onResponse caught error at "TypeError: Cannot read properties of undefined (reading 'replace')\n    at Object.presence ..... pubnub-javascript/lib/core/components/endpoint.js:235:17\n    at processTicksAndRejections (node:internal/process/task_queues:96:5)"

It would have saved me a lot of time if the errorData contained at least some information.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions