1
0
Fork 0

Fixed error handling on restrict. It will now throw an error if query is restricted to an unkown collection

This commit is contained in:
Michael Hackstein 2014-05-20 14:43:42 +02:00
parent f89b50a72e
commit bb9aaaeb4e
2 changed files with 52 additions and 7 deletions

View File

@ -31,6 +31,7 @@
var arangodb = require("org/arangodb"), var arangodb = require("org/arangodb"),
ArangoCollection = arangodb.ArangoCollection, ArangoCollection = arangodb.ArangoCollection,
ArangoError = arangodb.ArangoError,
db = arangodb.db, db = arangodb.db,
errors = arangodb.errors, errors = arangodb.errors,
_ = require("underscore"); _ = require("underscore");
@ -144,11 +145,12 @@ AQLStatement.prototype.isEdgeQuery = function() {
// --SECTION-- AQL Generator // --SECTION-- AQL Generator
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
var AQLGenerator = function(graphName) { var AQLGenerator = function(graph) {
this.stack = []; this.stack = [];
this.bindVars = { this.bindVars = {
"graphName": graphName "graphName": graph.__name
}; };
this.graph = graph;
this.lastEdgeVar = ""; this.lastEdgeVar = "";
}; };
@ -173,6 +175,20 @@ AQLGenerator.prototype.getLastEdgeVar = function() {
}; };
AQLGenerator.prototype.restrict = function(restrictions) { AQLGenerator.prototype.restrict = function(restrictions) {
var rest = stringToArray(restrictions);
var unknown = [];
var g = this.graph;
_.each(rest, function(r) {
if (!g.__edgeCollections[r]) {
unknown.push(r);
}
});
if (unknown.length > 0) {
var err = new ArangoError();
err.errorNum = arangodb.errors.ERROR_BAD_PARAMETER.code;
err.errorMessage = "edge collections: " + unknown.join(" and ") + " are not known to the graph";
throw err;
}
var lastQuery = this.stack.pop(); var lastQuery = this.stack.pop();
if (!lastQuery.isEdgeQuery()) { if (!lastQuery.isEdgeQuery()) {
this.stack.push(lastQuery); this.stack.push(lastQuery);
@ -180,7 +196,7 @@ AQLGenerator.prototype.restrict = function(restrictions) {
} }
lastQuery.query = lastQuery.query.replace(")", ",{},@restrictions_" + this.stack.length + ")"); lastQuery.query = lastQuery.query.replace(")", ",{},@restrictions_" + this.stack.length + ")");
lastQuery.edgeQuery = false; lastQuery.edgeQuery = false;
this.bindVars["restrictions_" + this.stack.length] = stringToArray(restrictions); this.bindVars["restrictions_" + this.stack.length] = rest;
this.stack.push(lastQuery); this.stack.push(lastQuery);
return this; return this;
}; };
@ -547,7 +563,7 @@ Graph.prototype._OUTEDGES = function(vertexId) {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Graph.prototype._edges = function(vertexId) { Graph.prototype._edges = function(vertexId) {
var AQLStmt = new AQLGenerator(this.__name); var AQLStmt = new AQLGenerator(this);
return AQLStmt.edges(vertexId, "any"); return AQLStmt.edges(vertexId, "any");
}; };
@ -556,7 +572,7 @@ Graph.prototype._edges = function(vertexId) {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Graph.prototype._inEdges = function(vertexId) { Graph.prototype._inEdges = function(vertexId) {
var AQLStmt = new AQLGenerator(this.__name); var AQLStmt = new AQLGenerator(this);
return AQLStmt.edges(vertexId, "inbound"); return AQLStmt.edges(vertexId, "inbound");
}; };
@ -565,7 +581,7 @@ Graph.prototype._inEdges = function(vertexId) {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Graph.prototype._outEdges = function(vertexId) { Graph.prototype._outEdges = function(vertexId) {
var AQLStmt = new AQLGenerator(this.__name); var AQLStmt = new AQLGenerator(this);
return AQLStmt.edges(vertexId, "outbound"); return AQLStmt.edges(vertexId, "outbound");
}; };
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -1,5 +1,5 @@
/*jslint indent: 2, nomen: true, maxlen: 80, sloppy: true */ /*jslint indent: 2, nomen: true, maxlen: 80, sloppy: true */
/*global require, assertEqual, assertTrue, assertFalse */ /*global require, assertEqual, assertTrue, assertFalse, fail */
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief test the general-graph class /// @brief test the general-graph class
@ -32,6 +32,7 @@ var jsunity = require("jsunity");
var arangodb = require("org/arangodb"); var arangodb = require("org/arangodb");
var db = arangodb.db; var db = arangodb.db;
var graph = require("org/arangodb/general-graph"); var graph = require("org/arangodb/general-graph");
var ERRORS = arangodb.errors;
var _ = require("underscore"); var _ = require("underscore");
@ -607,6 +608,34 @@ function GeneralGraphAQLQueriesSuite() {
assertFalse(findIdInResult(result, e3), "e3 is not excluded"); assertFalse(findIdInResult(result, e3), "e3 is not excluded");
}, },
////////////////////////////////////////////////////////////////////////////////
/// @brief test: restrict error handling
////////////////////////////////////////////////////////////////////////////////
test_restrictErrorHandlingSingle: function() {
try {
g._outEdges("v1/1").restrict(["included", "unknown"]);
fail();
} catch (err) {
assertEqual(err.errorNum, ERRORS.ERROR_BAD_PARAMETER.code);
assertEqual(err.errorMessage, "edge collections: unknown are not known to the graph");
}
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test: restrict error handling on multiple failures
////////////////////////////////////////////////////////////////////////////////
test_restrictErrorHandlingMultiple: function() {
try {
g._outEdges("v1/1").restrict(["failed", "included", "unknown", "foxxle"]);
fail();
} catch (err) {
assertEqual(err.errorNum, ERRORS.ERROR_BAD_PARAMETER.code);
assertEqual(err.errorMessage, "edge collections: failed and unknown and foxxle are not known to the graph");
}
},
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief test: filter construct on Edges /// @brief test: filter construct on Edges
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////