0

I'm sending query to Azure cosmos graph db using gremlin node package. For correct query it returns the respponse but for incorrect query it crashed with error - unhandled promise rejection. I have try catch in place but it still doesn't come in catch.

My code [Incorrect query]:-

module.exports.getUserRole = async (event) => {
  console.log("In getUserRole");
  return new Promise(async (resolve, reject)=> {
    try {
      const { P, g, translator, client } = await cosmos.getGraphTraversalhelpers();
      console.log("P", P);
      console.log("graphTraversal", g);
      console.log("translator", translator);
      console.log("client._connection", client._connection);
      console.log("client._connection.addListener", client._connection.on);

      let query = g
        .V()
        .hasLabel("user")
        .has("userName", event)
        .has("status", P.neq("disabled"))
        .out("hasUserRole")
        .has("status", P.neq("disabled"))
        .valueMap(true)
        .getBytecode(); // prepares the query
      client._connection.on('error', (error) => {
        console.log('addListener', error);
        reject(error);
      })
      let queryResult = await cosmos.executeQuery(translator.translate(query));
      console.log("executeQuery is", query);
      // driver.close(); // closes the db driver
      if (Array.isArray(queryResult) && queryResult.length > 0) {
        queryResult = queryResult[0];
      }
      return queryResult;
    } catch (error) {
      console.log("getUserRole cosmosgraph dao", error);
      reject(error);
    }
  });  
};

Error message:-

 "errorMessage": "ResponseError: Server error: \r\n\nActivityId : a3a04a88-7112-466b-99d9-9cf9413edd4a\nExceptionType : GraphSyntaxException\nExceptionMessage :\r\n\tGremlin query syntax error: Unsupported groovy language rule: 'parExpression' text: '('username'' @ line 1, column 40.\r\n\t1 Error(s)\nSource : Microsoft.Azure.Cosmos.Gremlin.Core\n\tGremlinRequestId : a3a04a88-7112-466b-99d9-9cf9413edd4a\n\tContext : graphcompute\n\tScope : graphparse-translate-outer\n\tGraphInterOpStatusCode : QuerySyntaxError\n\tHResult : 0x80131500\r\n (597)",

Error stack:-

{
    "errorType": "Runtime.UnhandledPromiseRejection",
    "errorMessage": "ResponseError: Server error: \r\n\nActivityId : a3a04a88-7112-466b-99d9-9cf9413edd4a\nExceptionType : GraphSyntaxException\nExceptionMessage :\r\n\tGremlin query syntax error: Unsupported groovy language rule: 'parExpression' text: '('username'' @ line 1, column 40.\r\n\t1 Error(s)\nSource : Microsoft.Azure.Cosmos.Gremlin.Core\n\tGremlinRequestId : a3a04a88-7112-466b-99d9-9cf9413edd4a\n\tContext : graphcompute\n\tScope : graphparse-translate-outer\n\tGraphInterOpStatusCode : QuerySyntaxError\n\tHResult : 0x80131500\r\n (597)",
    "reason": {
        "errorType": "ResponseError",
        "errorMessage": "Server error: \r\n\nActivityId : a3a04a88-7112-466b-99d9-9cf9413edd4a\nExceptionType : GraphSyntaxException\nExceptionMessage :\r\n\tGremlin query syntax error: Unsupported groovy language rule: 'parExpression' text: '('username'' @ line 1, column 40.\r\n\t1 Error(s)\nSource : Microsoft.Azure.Cosmos.Gremlin.Core\n\tGremlinRequestId : a3a04a88-7112-466b-99d9-9cf9413edd4a\n\tContext : graphcompute\n\tScope : graphparse-translate-outer\n\tGraphInterOpStatusCode : QuerySyntaxError\n\tHResult : 0x80131500\r\n (597)",
        "name": "ResponseError",
        "statusCode": 597,
        "statusMessage": "\r\n\nActivityId : a3a04a88-7112-466b-99d9-9cf9413edd4a\nExceptionType : GraphSyntaxException\nExceptionMessage :\r\n\tGremlin query syntax error: Unsupported groovy language rule: 'parExpression' text: '('username'' @ line 1, column 40.\r\n\t1 Error(s)\nSource : Microsoft.Azure.Cosmos.Gremlin.Core\n\tGremlinRequestId : a3a04a88-7112-466b-99d9-9cf9413edd4a\n\tContext : graphcompute\n\tScope : graphparse-translate-outer\n\tGraphInterOpStatusCode : QuerySyntaxError\n\tHResult : 0x80131500\r\n",
        "statusAttributes": {
            "x-ms-status-code": 400,
            "x-ms-request-charge": 0,
            "x-ms-total-request-charge": 0,
            "x-ms-server-time-ms": 1.3698,
            "x-ms-total-server-time-ms": 1.3698,
            "x-ms-activity-id": "a3a04a88-7112-466b-99d9-9cf9413edd4a"
        },
        "stack": [
            "ResponseError: Server error: \r",
            "",
            "ActivityId : a3a04a88-7112-466b-99d9-9cf9413edd4a",
            "ExceptionType : GraphSyntaxException",
            "ExceptionMessage :\r",
            "\tGremlin query syntax error: Unsupported groovy language rule: 'parExpression' text: '('username'' @ line 1, column 40.\r",
            "\t1 Error(s)",
            "Source : Microsoft.Azure.Cosmos.Gremlin.Core",
            "\tGremlinRequestId : a3a04a88-7112-466b-99d9-9cf9413edd4a",
            "\tContext : graphcompute",
            "\tScope : graphparse-translate-outer",
            "\tGraphInterOpStatusCode : QuerySyntaxError",
            "\tHResult : 0x80131500\r",
            " (597)",
            "    at Connection._handleMessage (/opt/nodejs/node_modules/gremlin/lib/driver/connection.js:289:9)",
            "    at WebSocket.<anonymous> (/opt/nodejs/node_modules/gremlin/lib/driver/connection.js:131:43)",
            "    at WebSocket.emit (events.js:314:20)",
            "    at WebSocket.EventEmitter.emit (domain.js:483:12)",
            "    at Receiver.receiverOnMessage (/opt/nodejs/node_modules/ws/lib/websocket.js:789:20)",
            "    at Receiver.emit (events.js:314:20)",
            "    at Receiver.EventEmitter.emit (domain.js:483:12)",
            "    at Receiver.dataMessage (/opt/nodejs/node_modules/ws/lib/receiver.js:413:14)",
            "    at Receiver.getData (/opt/nodejs/node_modules/ws/lib/receiver.js:352:17)",
            "    at Receiver.startLoop (/opt/nodejs/node_modules/ws/lib/receiver.js:138:22)"
        ]
    },
    "promise": {},
    "stack": [
        "Runtime.UnhandledPromiseRejection: ResponseError: Server error: \r",
        "",
        "ActivityId : a3a04a88-7112-466b-99d9-9cf9413edd4a",
        "ExceptionType : GraphSyntaxException",
        "ExceptionMessage :\r",
        "\tGremlin query syntax error: Unsupported groovy language rule: 'parExpression' text: '('username'' @ line 1, column 40.\r",
        "\t1 Error(s)",
        "Source : Microsoft.Azure.Cosmos.Gremlin.Core",
        "\tGremlinRequestId : a3a04a88-7112-466b-99d9-9cf9413edd4a",
        "\tContext : graphcompute",
        "\tScope : graphparse-translate-outer",
        "\tGraphInterOpStatusCode : QuerySyntaxError",
        "\tHResult : 0x80131500\r",
        " (597)",
        "    at process.<anonymous> (/var/runtime/index.js:35:15)",
        "    at process.emit (events.js:314:20)",
        "    at process.EventEmitter.emit (domain.js:483:12)",
        "    at processPromiseRejections (internal/process/promises.js:209:33)",
        "    at processTicksAndRejections (internal/process/task_queues.js:98:32)"
    ]
}

As you see above it crashes here and throws error, but my catch is never called in code above.

P.S:- Above code is running in AWS lambda

David Makogon
  • 69,407
  • 21
  • 141
  • 189
iAviator
  • 1,310
  • 13
  • 31

1 Answers1

0

The unhandled exception was not related to the Gremlin library but my code of parsing the gremlin response. I was parsing the submit function response wrongly as result the code crashed with unhandled exception

iAviator
  • 1,310
  • 13
  • 31