This is an automated email from the ASF dual-hosted git repository.

spmallette pushed a commit to branch TINKERPOP-2285
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit 394dd2b0e260756a8bf7c54b4e2882cd011a5bdb
Author: Stephen Mallette <sp...@genoprime.com>
AuthorDate: Wed Sep 11 08:56:40 2019 -0400

    TINKERPOP-2285 Added ResponseError in gremlin-javascript
    
    ResponseError in javascript is akin to ResponseException in java. It 
includes more direct access to protocol lever server error information such as 
the status code and status attributes which means we don't need to pack that 
information into the error message itself at all. Kept that old style to the 
error messages just in case users were parsing those in prior versions - didn't 
want to break that.
---
 CHANGELOG.asciidoc                                 |  1 +
 .../gremlin-javascript/lib/driver/connection.js    | 12 +++++---
 .../lib/driver/response-error.js                   | 33 ++++++++++++++++++++++
 .../test/integration/remote-connection-tests.js    |  5 ++++
 4 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 366d759..45c3bb2 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -23,6 +23,7 @@ 
image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
 [[release-3-3-9]]
 === TinkerPop 3.3.9 (Release Date: NOT OFFICIALLY RELEASED YET)
 
+* Exposed response status attributes in a `ResponseError` in 
gremlin-javascript.
 * Added `ImmutableExplanation` for a `TraversalExplanation` that just contains 
data.
 * Fixed `TraversalExplanation` deserialization in GraphSON 2 and 3 which was 
not supported before in Java.
 * Added support for custom request headers in Python.
diff --git 
a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/connection.js
 
b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/connection.js
index 0ae340c..5b8cd89 100644
--- 
a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/connection.js
+++ 
b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/connection.js
@@ -28,6 +28,7 @@ const util = require('util');
 const utils = require('../utils');
 const serializer = require('../structure/io/graph-serializer');
 const ResultSet = require('./result-set');
+const ResponseError = require('./response-error');
 
 const responseStatusCode = {
   success: 200,
@@ -235,10 +236,12 @@ class Connection extends EventEmitter {
         this._clearHandler(requestId);
         if (response.status !== undefined && response.status.message) {
           return handler.callback(
-            new Error(util.format(
-              'Server error (no request information): %s (%d)', 
response.status.message, response.status.code)));
+            // TINKERPOP-2285: keep the old server error message in case folks 
are parsing that - fix in a future breaking version
+            new ResponseError(util.format(
+              'Server error (no request information): %s (%d)', 
response.status.message, response.status.code), response.status));
         } else {
-          return handler.callback(new Error(util.format('Server error (no 
request information): %j', response)));
+           // TINKERPOP-2285: keep the old server error message in case folks 
are parsing that - fix in a future breaking version
+          return handler.callback(new ResponseError(util.format('Server error 
(no request information): %j', response), response.status));
         }
       });
       return;
@@ -262,7 +265,8 @@ class Connection extends EventEmitter {
     else if (response.status.code >= 400) {
       // callback in error
       return handler.callback(
-        new Error(util.format('Server error: %s (%d)', 
response.status.message, response.status.code)));
+        // TINKERPOP-2285: keep the old server error message in case folks are 
parsing that - fix in a future breaking version
+        new ResponseError(util.format('Server error: %s (%d)', 
response.status.message, response.status.code), response.status));
     }
     switch (response.status.code) {
       case responseStatusCode.noContent:
diff --git 
a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/response-error.js
 
b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/response-error.js
new file mode 100644
index 0000000..5b5e4b6
--- /dev/null
+++ 
b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/response-error.js
@@ -0,0 +1,33 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+'use strict';
+
+class ResponseError extends Error {
+    constructor(message, responseStatus) {
+        super(message);
+        this.name = "ResponseError";
+        this.statusCode = responseStatus.code;
+        this.statusMessage = responseStatus.message;
+        this.statusAttributes = responseStatus.attributes || {};
+    }
+}
+
+
+module.exports = ResponseError;
\ No newline at end of file
diff --git 
a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/remote-connection-tests.js
 
b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/remote-connection-tests.js
index b73d909..f86e252 100644
--- 
a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/remote-connection-tests.js
+++ 
b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/remote-connection-tests.js
@@ -53,6 +53,11 @@ describe('DriverRemoteConnection', function () {
         .catch(function (err) {
           assert.ok(err);
           assert.ok(err.message.indexOf('599') > 0);
+          assert.ok(err.statusCode === 599);
+          assert.ok(err.statusMessage === 'Could not locate method: 
GraphTraversalSource.SYNTAX_ERROR()');
+          assert.ok(err.statusAttributes);
+          assert.ok(err.statusAttributes.has('exceptions'));
+          assert.ok(err.statusAttributes.has('stackTrace'));
         });
     });
   });

Reply via email to