From 7ba1920b88105d03ce0dffe69db9a10ba074498e Mon Sep 17 00:00:00 2001 From: Jan Steemann Date: Thu, 11 Oct 2012 16:20:22 +0200 Subject: [PATCH] moved try ... catch blocks to outermost scope --- js/actions/system/api-blueprint.js | 129 ++++++---- js/actions/system/api-collection.js | 31 ++- js/actions/system/api-cursor.js | 126 +++++---- js/actions/system/api-edges.js | 41 ++- js/actions/system/api-explain.js | 37 ++- js/actions/system/api-index.js | 245 ++++++++---------- js/actions/system/api-query.js | 38 +-- js/actions/system/api-simple.js | 381 ++++++++++++++-------------- js/actions/system/api-system.js | 51 ++-- js/actions/system/key-value.js | 343 ++++++++++++------------- 10 files changed, 711 insertions(+), 711 deletions(-) diff --git a/js/actions/system/api-blueprint.js b/js/actions/system/api-blueprint.js index fe03292adc..22e01e9739 100644 --- a/js/actions/system/api-blueprint.js +++ b/js/actions/system/api-blueprint.js @@ -163,21 +163,26 @@ context : BLUEPRINT_CONTEXT, callback : function (req, res) { - switch (req.requestType) { - case (actions.POST) : - postGraph(req, res); - break; + try { + switch (req.requestType) { + case (actions.POST) : + postGraph(req, res); + break; - case (actions.GET) : - getGraph(req, res); - break; + case (actions.GET) : + getGraph(req, res); + break; - case (actions.DELETE) : - deleteGraph(req, res); - break; + case (actions.DELETE) : + deleteGraph(req, res); + break; - default: - actions.resultUnsupported(req, res); + default: + actions.resultUnsupported(req, res); + } + } + catch (err) { + actions.resultException(req, res, err); } } }); @@ -381,21 +386,26 @@ context : BLUEPRINT_CONTEXT, callback : function (req, res) { - switch (req.requestType) { - case (actions.POST) : - postVertex(req, res); - break; + try { + switch (req.requestType) { + case (actions.POST) : + postVertex(req, res); + break; - case (actions.GET) : - getVertex(req, res); - break; + case (actions.GET) : + getVertex(req, res); + break; - case (actions.PUT) : - putVertex(req, res); - break; + case (actions.PUT) : + putVertex(req, res); + break; - default: - actions.resultUnsupported(req, res); + default: + actions.resultUnsupported(req, res); + } + } + catch (err) { + actions.resultException(req, res, err); } } }); @@ -457,13 +467,18 @@ context : BLUEPRINT_CONTEXT, callback : function (req, res) { - switch (req.requestType) { - case (actions.GET) : - getVertices(req, res); - break; + try { + switch (req.requestType) { + case (actions.GET) : + getVertices(req, res); + break; - default: - actions.resultUnsupported(req, res); + default: + actions.resultUnsupported(req, res); + } + } + catch (err) { + actions.resultException(req, res, err); } } }); @@ -672,25 +687,30 @@ context : BLUEPRINT_CONTEXT, callback : function (req, res) { - switch (req.requestType) { - case (actions.POST) : - postEdge(req, res); - break; + try { + switch (req.requestType) { + case (actions.POST) : + postEdge(req, res); + break; - case (actions.GET) : - getEdge(req, res); - break; + case (actions.GET) : + getEdge(req, res); + break; - case (actions.DELETE) : - deleteEdge(req, res); - break; + case (actions.DELETE) : + deleteEdge(req, res); + break; - case (actions.PUT) : - putEdge(req, res); - break; - - default: - actions.resultUnsupported(req, res); + case (actions.PUT) : + putEdge(req, res); + break; + + default: + actions.resultUnsupported(req, res); + } + } + catch (err) { + actions.resultException(req, res, err); } } }); @@ -786,13 +806,18 @@ context : BLUEPRINT_CONTEXT, callback : function (req, res) { - switch (req.requestType) { - case (actions.GET) : - getEdges(req, res); - break; + try { + switch (req.requestType) { + case (actions.GET) : + getEdges(req, res); + break; - default: - actions.resultUnsupported(req, res); + default: + actions.resultUnsupported(req, res); + } + } + catch (err) { + actions.resultException(req, res, err); } } }); diff --git a/js/actions/system/api-collection.js b/js/actions/system/api-collection.js index 058a148489..635e421530 100644 --- a/js/actions/system/api-collection.js +++ b/js/actions/system/api-collection.js @@ -730,20 +730,25 @@ context : "api", callback : function (req, res) { - if (req.requestType === actions.GET) { - GET_api_collection(req, res); + try { + if (req.requestType === actions.GET) { + GET_api_collection(req, res); + } + else if (req.requestType === actions.DELETE) { + DELETE_api_collection(req, res); + } + else if (req.requestType === actions.POST) { + POST_api_collection(req, res); + } + else if (req.requestType === actions.PUT) { + PUT_api_collection(req, res); + } + else { + actions.resultUnsupported(req, res); + } } - else if (req.requestType === actions.DELETE) { - DELETE_api_collection(req, res); - } - else if (req.requestType === actions.POST) { - POST_api_collection(req, res); - } - else if (req.requestType === actions.PUT) { - PUT_api_collection(req, res); - } - else { - actions.resultUnsupported(req, res); + catch (err) { + actions.resultException(req, res, err); } } }); diff --git a/js/actions/system/api-cursor.js b/js/actions/system/api-cursor.js index 8d65143526..bca631ed52 100644 --- a/js/actions/system/api-cursor.js +++ b/js/actions/system/api-cursor.js @@ -133,33 +133,28 @@ function POST_api_cursor(req, res) { return; } - try { - var cursor; + var cursor; - if (json.query != undefined) { - cursor = AHUACATL_RUN(json.query, - json.bindVars, - (json.count != undefined ? json.count : false), - json.batchSize, - (json.batchSize == undefined)); - } - else { - actions.resultBad(req, res, actions.ERROR_QUERY_EMPTY); - return; - } + if (json.query != undefined) { + cursor = AHUACATL_RUN(json.query, + json.bindVars, + (json.count != undefined ? json.count : false), + json.batchSize, + (json.batchSize == undefined)); + } + else { + actions.resultBad(req, res, actions.ERROR_QUERY_EMPTY); + return; + } - // error occurred - if (cursor instanceof ArangoError) { - actions.resultBad(req, res, cursor.errorNum, cursor.errorMessage); - return; - } + // error occurred + if (cursor instanceof ArangoError) { + actions.resultBad(req, res, cursor.errorNum, cursor.errorMessage); + return; + } - // this might dispose or persist the cursor - actions.resultCursor(req, res, cursor, actions.HTTP_CREATED, { countRequested: json.count ? true : false }); - } - catch (err) { - actions.resultException(req, res, err); - } + // this might dispose or persist the cursor + actions.resultCursor(req, res, cursor, actions.HTTP_CREATED, { countRequested: json.count ? true : false }); } //////////////////////////////////////////////////////////////////////////////// @@ -207,29 +202,24 @@ function PUT_api_cursor(req, res) { return; } - try { - var cursorId = decodeURIComponent(req.suffix[0]); - var cursor = CURSOR(cursorId); + var cursorId = decodeURIComponent(req.suffix[0]); + var cursor = CURSOR(cursorId); - if (!(cursor instanceof ArangoCursor)) { - actions.resultBad(req, res, actions.ERROR_CURSOR_NOT_FOUND); - return; - } + if (! (cursor instanceof ArangoCursor)) { + actions.resultBad(req, res, actions.ERROR_CURSOR_NOT_FOUND); + return; + } - try { - // note: this might dispose or persist the cursor - actions.resultCursor(req, res, cursor, actions.HTTP_OK); - } - catch (e) { - } + try { + // note: this might dispose or persist the cursor + actions.resultCursor(req, res, cursor, actions.HTTP_OK); + } + catch (e) { + } - cursor.unuse(); - cursor = null; - internal.wait(0.0); - } - catch (err) { - actions.resultException(req, res, err); - } + cursor.unuse(); + cursor = null; + internal.wait(0.0); } //////////////////////////////////////////////////////////////////////////////// @@ -267,19 +257,14 @@ function DELETE_api_cursor(req, res) { return; } - try { - var cursorId = decodeURIComponent(req.suffix[0]); - if (! DELETE_CURSOR(cursorId)) { - actions.resultNotFound(req, res, actions.ERROR_CURSOR_NOT_FOUND); - return; - } + var cursorId = decodeURIComponent(req.suffix[0]); + if (! DELETE_CURSOR(cursorId)) { + actions.resultNotFound(req, res, actions.ERROR_CURSOR_NOT_FOUND); + return; + } - actions.resultOk(req, res, actions.HTTP_ACCEPTED, { "id" : cursorId }); - internal.wait(0.0); - } - catch (err) { - actions.resultException(req, res, err); - } + actions.resultOk(req, res, actions.HTTP_ACCEPTED, { "id" : cursorId }); + internal.wait(0.0); } // ----------------------------------------------------------------------------- @@ -295,21 +280,26 @@ actions.defineHttp({ context : "api", callback : function (req, res) { - switch (req.requestType) { - case (actions.POST) : - POST_api_cursor(req, res); - break; + try { + switch (req.requestType) { + case (actions.POST) : + POST_api_cursor(req, res); + break; - case (actions.PUT) : - PUT_api_cursor(req, res); - break; + case (actions.PUT) : + PUT_api_cursor(req, res); + break; - case (actions.DELETE) : - DELETE_api_cursor(req, res); - break; + case (actions.DELETE) : + DELETE_api_cursor(req, res); + break; - default: - actions.resultUnsupported(req, res); + default: + actions.resultUnsupported(req, res); + } + } + catch (err) { + actions.resultException(req, res, err); } } }); diff --git a/js/actions/system/api-edges.js b/js/actions/system/api-edges.js index fb4d2305e0..64ef544897 100644 --- a/js/actions/system/api-edges.js +++ b/js/actions/system/api-edges.js @@ -89,24 +89,18 @@ function GET_edges (req, res) { var direction = req.parameters['direction']; var e; - try { - if (direction === null || direction === undefined || direction === "" || direction === "any") { - e = collection.edges(vertex); - } - else if (direction === "in") { - e = collection.inEdges(vertex); - } - else if (direction === "out") { - e = collection.outEdges(vertex); - } - else { - actions.resultBad(req, res, actions.ERROR_HTTP_BAD_PARAMETER, - " must be any, in, or out, not: " + JSON.stringify(direction)); - return; - } + if (direction === null || direction === undefined || direction === "" || direction === "any") { + e = collection.edges(vertex); } - catch (err) { - actions.resultException(req, res, err); + else if (direction === "in") { + e = collection.inEdges(vertex); + } + else if (direction === "out") { + e = collection.outEdges(vertex); + } + else { + actions.resultBad(req, res, actions.ERROR_HTTP_BAD_PARAMETER, + " must be any, in, or out, not: " + JSON.stringify(direction)); return; } @@ -124,11 +118,16 @@ actions.defineHttp({ context : "api", callback : function (req, res) { - if (req.requestType === actions.GET) { - GET_edges(req, res); + try { + if (req.requestType === actions.GET) { + GET_edges(req, res); + } + else { + actions.resultUnsupported(req, res); + } } - else { - actions.resultUnsupported(req, res); + catch (err) { + actions.resultException(req, res, err); } } }); diff --git a/js/actions/system/api-explain.js b/js/actions/system/api-explain.js index 91fd3bed19..9b5fbe7f34 100644 --- a/js/actions/system/api-explain.js +++ b/js/actions/system/api-explain.js @@ -130,21 +130,15 @@ function POST_api_explain (req, res) { return; } - try { - var result = AHUACATL_EXPLAIN(json.query, json.bindVars); + var result = AHUACATL_EXPLAIN(json.query, json.bindVars); - if (result instanceof ArangoError) { - actions.resultBad(req, res, result.errorNum, result.errorMessage); - return; - } - - result = { "plan" : result }; - - actions.resultOk(req, res, actions.HTTP_OK, result); - } - catch (err) { - actions.resultException(req, res, err); + if (result instanceof ArangoError) { + actions.resultBad(req, res, result.errorNum, result.errorMessage); + return; } + + result = { "plan" : result }; + actions.resultOk(req, res, actions.HTTP_OK, result); } // ----------------------------------------------------------------------------- @@ -160,13 +154,18 @@ actions.defineHttp({ context : "api", callback : function (req, res) { - switch (req.requestType) { - case (actions.POST) : - POST_api_explain(req, res); - break; + try { + switch (req.requestType) { + case (actions.POST) : + POST_api_explain(req, res); + break; - default: - actions.resultUnsupported(req, res); + default: + actions.resultUnsupported(req, res); + } + } + catch (err) { + actions.resultException(req, res, err); } } }); diff --git a/js/actions/system/api-index.js b/js/actions/system/api-index.js index 089e29c074..8794d7aa1f 100644 --- a/js/actions/system/api-index.js +++ b/js/actions/system/api-index.js @@ -190,19 +190,14 @@ function POST_api_index_cap (req, res, collection, body) { "expecting a size"); } - try { - var size = body.size; - var index = collection.ensureCapConstraint(size); + var size = body.size; + var index = collection.ensureCapConstraint(size); - if (index.isNewlyCreated) { - actions.resultOk(req, res, actions.HTTP_CREATED, index); - } - else { - actions.resultOk(req, res, actions.HTTP_OK, index); - } + if (index.isNewlyCreated) { + actions.resultOk(req, res, actions.HTTP_CREATED, index); } - catch (err) { - actions.resultException(req, res, err); + else { + actions.resultOk(req, res, actions.HTTP_OK, index); } } @@ -273,73 +268,68 @@ function POST_api_index_geo (req, res, collection, body) { "fields must be a list of attribute paths: " + fields); } - try { - var index; + var index; - if (fields.length === 1) { + if (fields.length === 1) { - // attribute list and geoJson - if (body.hasOwnProperty("geoJson")) { - if (body.hasOwnProperty("constraint") && body.constraint) { - if (body.hasOwnProperty("ignoreNull")) { - index = collection.ensureGeoConstraint(fields[0], body.geoJson, body.ignoreNull); - } - else { - index = collection.ensureGeoConstraint(fields[0], body.geoJson, false); - } - } - else { - index = collection.ensureGeoIndex(fields[0], body.geoJson); - } - } - - // attribute list - else { - if (body.hasOwnProperty("constraint") && body.constraint) { - if (body.hasOwnProperty("ignoreNull")) { - index = collection.ensureGeoConstraint(fields[0], body.ignoreNull); - } - else { - index = collection.ensureGeoConstraint(fields[0], false); - } - } - else { - index = collection.ensureGeoIndex(fields[0]); - } - } - } - - // attributes - else if (fields.length === 2) { + // attribute list and geoJson + if (body.hasOwnProperty("geoJson")) { if (body.hasOwnProperty("constraint") && body.constraint) { if (body.hasOwnProperty("ignoreNull")) { - index = collection.ensureGeoConstraint(fields[0], fields[1], body.ignoreNull); + index = collection.ensureGeoConstraint(fields[0], body.geoJson, body.ignoreNull); } else { - index = collection.ensureGeoConstraint(fields[0], fields[1], false); + index = collection.ensureGeoConstraint(fields[0], body.geoJson, false); } } else { - index = collection.ensureGeoIndex(fields[0], fields[1]); + index = collection.ensureGeoIndex(fields[0], body.geoJson); } } - // something is wrong + // attribute list else { - actions.resultBad(req, res, actions.ERROR_HTTP_BAD_PARAMETER, - "fields must be a list of attribute paths of length 1 or 2: " + fields); - return; - } - - if (index.isNewlyCreated) { - actions.resultOk(req, res, actions.HTTP_CREATED, index); - } - else { - actions.resultOk(req, res, actions.HTTP_OK, index); + if (body.hasOwnProperty("constraint") && body.constraint) { + if (body.hasOwnProperty("ignoreNull")) { + index = collection.ensureGeoConstraint(fields[0], body.ignoreNull); + } + else { + index = collection.ensureGeoConstraint(fields[0], false); + } + } + else { + index = collection.ensureGeoIndex(fields[0]); + } } } - catch (err) { - actions.resultException(req, res, err); + + // attributes + else if (fields.length === 2) { + if (body.hasOwnProperty("constraint") && body.constraint) { + if (body.hasOwnProperty("ignoreNull")) { + index = collection.ensureGeoConstraint(fields[0], fields[1], body.ignoreNull); + } + else { + index = collection.ensureGeoConstraint(fields[0], fields[1], false); + } + } + else { + index = collection.ensureGeoIndex(fields[0], fields[1]); + } + } + + // something is wrong + else { + actions.resultBad(req, res, actions.ERROR_HTTP_BAD_PARAMETER, + "fields must be a list of attribute paths of length 1 or 2: " + fields); + return; + } + + if (index.isNewlyCreated) { + actions.resultOk(req, res, actions.HTTP_CREATED, index); + } + else { + actions.resultOk(req, res, actions.HTTP_OK, index); } } @@ -390,25 +380,20 @@ function POST_api_index_hash (req, res, collection, body) { "fields must be a list of attribute paths: " + fields); } - try { - var index; + var index; - if (body.unique) { - index = collection.ensureUniqueConstraint.apply(collection, fields); - } - else { - index = collection.ensureHashIndex.apply(collection, fields); - } - - if (index.isNewlyCreated) { - actions.resultOk(req, res, actions.HTTP_CREATED, index); - } - else { - actions.resultOk(req, res, actions.HTTP_OK, index); - } + if (body.unique) { + index = collection.ensureUniqueConstraint.apply(collection, fields); } - catch (err) { - actions.resultException(req, res, err); + else { + index = collection.ensureHashIndex.apply(collection, fields); + } + + if (index.isNewlyCreated) { + actions.resultOk(req, res, actions.HTTP_CREATED, index); + } + else { + actions.resultOk(req, res, actions.HTTP_OK, index); } } @@ -455,25 +440,20 @@ function POST_api_index_skiplist (req, res, collection, body) { "fields must be a list of attribute paths: " + fields); } - try { - var index; + var index; - if (body.unique) { - index = collection.ensureUniqueSkiplist.apply(collection, fields); - } - else { - index = collection.ensureSkiplist.apply(collection, fields); - } - - if (index.isNewlyCreated) { - actions.resultOk(req, res, actions.HTTP_CREATED, index); - } - else { - actions.resultOk(req, res, actions.HTTP_OK, index); - } + if (body.unique) { + index = collection.ensureUniqueSkiplist.apply(collection, fields); } - catch (err) { - actions.resultException(req, res, err); + else { + index = collection.ensureSkiplist.apply(collection, fields); + } + + if (index.isNewlyCreated) { + actions.resultOk(req, res, actions.HTTP_CREATED, index); + } + else { + actions.resultOk(req, res, actions.HTTP_OK, index); } } @@ -521,25 +501,20 @@ function POST_api_index_bitarray (req, res, collection, body) { "fields must be a list of attribute paths: " + fields); } - try { - var index; + var index; - if (body.unique) { - throw "Bitarray indexes can not be unique"; - } - else { - index = collection.ensureBitarray.apply(collection, fields); - } - - if (index.isNewlyCreated) { - actions.resultOk(req, res, actions.HTTP_CREATED, index); - } - else { - actions.resultOk(req, res, actions.HTTP_OK, index); - } + if (body.unique) { + throw "Bitarray indexes can not be unique"; } - catch (err) { - actions.resultException(req, res, err); + else { + index = collection.ensureBitarray.apply(collection, fields); + } + + if (index.isNewlyCreated) { + actions.resultOk(req, res, actions.HTTP_CREATED, index); + } + else { + actions.resultOk(req, res, actions.HTTP_OK, index); } } @@ -659,19 +634,14 @@ function DELETE_api_index (req, res) { return; } - try { - var iid = parseInt(decodeURIComponent(req.suffix[1])); - var droped = collection.dropIndex(collection._id + "/" + iid); + var iid = parseInt(decodeURIComponent(req.suffix[1])); + var dropped = collection.dropIndex(collection._id + "/" + iid); - if (droped) { - actions.resultOk(req, res, actions.HTTP_OK, { id : collection._id + "/" + iid }); - } - else { - actions.indexNotFound(req, res, collection, collection._id + "/" + iid); - } + if (dropped) { + actions.resultOk(req, res, actions.HTTP_OK, { id : collection._id + "/" + iid }); } - catch (err) { - actions.resultException(req, res, err); + else { + actions.indexNotFound(req, res, collection, collection._id + "/" + iid); } } @@ -684,17 +654,22 @@ actions.defineHttp({ context : "api", callback : function (req, res) { - if (req.requestType === actions.GET) { - GET_api_index(req, res); + try { + if (req.requestType === actions.GET) { + GET_api_index(req, res); + } + else if (req.requestType === actions.DELETE) { + DELETE_api_index(req, res); + } + else if (req.requestType === actions.POST) { + POST_api_index(req, res); + } + else { + actions.resultUnsupported(req, res); + } } - else if (req.requestType === actions.DELETE) { - DELETE_api_index(req, res); - } - else if (req.requestType === actions.POST) { - POST_api_index(req, res); - } - else { - actions.resultUnsupported(req, res); + catch (err) { + actions.resultException(req, res, err); } } }); diff --git a/js/actions/system/api-query.js b/js/actions/system/api-query.js index 580929eba9..e1f39f2dd7 100644 --- a/js/actions/system/api-query.js +++ b/js/actions/system/api-query.js @@ -80,21 +80,16 @@ function POST_api_query (req, res) { return; } - try { - var result = AHUACATL_PARSE(json.query); + var result = AHUACATL_PARSE(json.query); - if (result instanceof ArangoError) { - actions.resultBad(req, res, result.errorNum, result.errorMessage); - return; - } - - result = { "bindVars" : result.parameters, "collections" : result.collections }; - - actions.resultOk(req, res, actions.HTTP_OK, result); - } - catch (err) { - actions.resultException(req, res, err); + if (result instanceof ArangoError) { + actions.resultBad(req, res, result.errorNum, result.errorMessage); + return; } + + result = { "bindVars" : result.parameters, "collections" : result.collections }; + + actions.resultOk(req, res, actions.HTTP_OK, result); } // ----------------------------------------------------------------------------- @@ -110,13 +105,18 @@ actions.defineHttp({ context : "api", callback : function (req, res) { - switch (req.requestType) { - case (actions.POST) : - POST_api_query(req, res); - break; + try { + switch (req.requestType) { + case (actions.POST) : + POST_api_query(req, res); + break; - default: - actions.resultUnsupported(req, res); + default: + actions.resultUnsupported(req, res); + } + } + catch (err) { + actions.resultException(req, res, err); } } }); diff --git a/js/actions/system/api-simple.js b/js/actions/system/api-simple.js index 41d1c41e58..0829ed019b 100644 --- a/js/actions/system/api-simple.js +++ b/js/actions/system/api-simple.js @@ -74,28 +74,28 @@ actions.defineHttp({ context : "api", callback : function (req, res) { - var body = actions.getJsonBody(req, res); + try { + var body = actions.getJsonBody(req, res); - if (body === undefined) { - return; - } + if (body === undefined) { + return; + } - if (req.requestType != actions.PUT) { - actions.resultUnsupported(req, res); - } - else { - var limit = body.limit; - var skip = body.skip; - - var name = body.collection; - var id = parseInt(name) || name; - var collection = internal.db._collection(id); - - if (collection === null) { - actions.collectionNotFound(req, res, name); + if (req.requestType != actions.PUT) { + actions.resultUnsupported(req, res); } else { - try { + var limit = body.limit; + var skip = body.skip; + + var name = body.collection; + var id = parseInt(name) || name; + var collection = internal.db._collection(id); + + if (collection === null) { + actions.collectionNotFound(req, res, name); + } + else { var result = collection.all(); if (skip !== null && skip !== undefined) { @@ -108,11 +108,11 @@ actions.defineHttp({ actions.resultCursor(req, res, CREATE_CURSOR(result.toArray(), true, body.batchSize)); } - catch (err) { - actions.resultException(req, res, err); - } } } + catch (err) { + actions.resultException(req, res, err); + } } }); @@ -170,39 +170,39 @@ actions.defineHttp({ context : "api", callback : function (req, res) { - var body = actions.getJsonBody(req, res); + try { + var body = actions.getJsonBody(req, res); - if (body === undefined) { - return; - } - - if (req.requestType != actions.PUT) { - actions.resultUnsupported(req, res); - } - else { - var limit = body.limit; - var skip = body.skip; - var latitude = body.latitude; - var longitude = body.longitude; - var distance = body.distance; - var name = body.collection; - var geo = body.geo; - - var name = body.collection; - var id = parseInt(name) || name; - var collection = internal.db._collection(id); - - if (collection === null) { - actions.collectionNotFound(req, res, name); + if (body === undefined) { + return; } - else if (latitude === null || latitude === undefined) { - actions.badParameter(req, res, "latitude"); - } - else if (longitude === null || longitude === undefined) { - actions.badParameter(req, res, "longitude"); + + if (req.requestType != actions.PUT) { + actions.resultUnsupported(req, res); } else { - try { + var limit = body.limit; + var skip = body.skip; + var latitude = body.latitude; + var longitude = body.longitude; + var distance = body.distance; + var name = body.collection; + var geo = body.geo; + + var name = body.collection; + var id = parseInt(name) || name; + var collection = internal.db._collection(id); + + if (collection === null) { + actions.collectionNotFound(req, res, name); + } + else if (latitude === null || latitude === undefined) { + actions.badParameter(req, res, "latitude"); + } + else if (longitude === null || longitude === undefined) { + actions.badParameter(req, res, "longitude"); + } + else { var result; if (geo === null || geo === undefined) { @@ -226,11 +226,11 @@ actions.defineHttp({ actions.resultCursor(req, res, CREATE_CURSOR(result.toArray(), true, body.batchSize)); } - catch (err) { - actions.resultException(req, res, err); - } } } + catch (err) { + actions.resultException(req, res, err); + } } }); @@ -287,39 +287,39 @@ actions.defineHttp({ context : "api", callback : function (req, res) { - var body = actions.getJsonBody(req, res); + try { + var body = actions.getJsonBody(req, res); - if (body === undefined) { - return; - } - - if (req.requestType != actions.PUT) { - actions.resultUnsupported(req, res); - } - else { - var limit = body.limit; - var skip = body.skip; - var latitude = body.latitude; - var longitude = body.longitude; - var distance = body.distance; - var radius = body.radius; - var geo = body.geo; - - var name = body.collection; - var id = parseInt(name) || name; - var collection = internal.db._collection(id); - - if (collection === null) { - actions.collectionNotFound(req, res, name); + if (body === undefined) { + return; } - else if (latitude === null || latitude === undefined) { - actions.badParameter(req, res, "latitude"); - } - else if (longitude === null || longitude === undefined) { - actions.badParameter(req, res, "longitude"); + + if (req.requestType != actions.PUT) { + actions.resultUnsupported(req, res); } else { - try { + var limit = body.limit; + var skip = body.skip; + var latitude = body.latitude; + var longitude = body.longitude; + var distance = body.distance; + var radius = body.radius; + var geo = body.geo; + + var name = body.collection; + var id = parseInt(name) || name; + var collection = internal.db._collection(id); + + if (collection === null) { + actions.collectionNotFound(req, res, name); + } + else if (latitude === null || latitude === undefined) { + actions.badParameter(req, res, "latitude"); + } + else if (longitude === null || longitude === undefined) { + actions.badParameter(req, res, "longitude"); + } + else { var result; if (geo === null || geo === undefined) { @@ -343,11 +343,11 @@ actions.defineHttp({ actions.resultCursor(req, res, CREATE_CURSOR(result.toArray(), true, body.batchSize)); } - catch (err) { - actions.resultException(req, res, err); - } } } + catch (err) { + actions.resultException(req, res, err); + } } }); @@ -393,33 +393,33 @@ actions.defineHttp({ context : "api", callback : function (req, res) { - var body = actions.getJsonBody(req, res); + try { + var body = actions.getJsonBody(req, res); - if (body === undefined) { - return; - } - - if (req.requestType != actions.PUT) { - actions.resultUnsupported(req, res); - } - else { - var limit = body.limit; - var skip = body.skip; - var name = body.collection; - var example = body.example; - - var name = body.collection; - var id = parseInt(name) || name; - var collection = internal.db._collection(id); - - if (collection === null) { - actions.collectionNotFound(req, res, name); + if (body === undefined) { + return; } - else if (typeof example !== "object") { - actions.badParameter(req, res, "example"); + + if (req.requestType != actions.PUT) { + actions.resultUnsupported(req, res); } else { - try { + var limit = body.limit; + var skip = body.skip; + var name = body.collection; + var example = body.example; + + var name = body.collection; + var id = parseInt(name) || name; + var collection = internal.db._collection(id); + + if (collection === null) { + actions.collectionNotFound(req, res, name); + } + else if (typeof example !== "object") { + actions.badParameter(req, res, "example"); + } + else { var result = collection.byExample(example); if (skip !== null && skip !== undefined) { @@ -432,11 +432,11 @@ actions.defineHttp({ actions.resultCursor(req, res, CREATE_CURSOR(result.toArray(), true, body.batchSize)); } - catch (err) { - actions.resultException(req, res, err); - } } } + catch (err) { + actions.resultException(req, res, err); + } } }); @@ -475,39 +475,44 @@ actions.defineHttp({ context : "api", callback : function (req, res) { - var body = actions.getJsonBody(req, res); - - if (body === undefined) { - return; - } - - if (req.requestType != actions.PUT) { - actions.resultUnsupported(req, res); - } - else { - var example = body.example; - - var name = body.collection; - var id = parseInt(name) || name; - var collection = internal.db._collection(id); - - if (collection === null) { - actions.collectionNotFound(req, res, name); + try { + var body = actions.getJsonBody(req, res); + + if (body === undefined) { + return; } - else if (typeof example !== "object") { - actions.badParameter(req, res, "example"); + + if (req.requestType != actions.PUT) { + actions.resultUnsupported(req, res); } else { - var result = collection.byExample(example).limit(1); + var example = body.example; - if (result.hasNext()) { - actions.resultOk(req, res, actions.HTTP_OK, { document : result.next() }); + var name = body.collection; + var id = parseInt(name) || name; + var collection = internal.db._collection(id); + + if (collection === null) { + actions.collectionNotFound(req, res, name); + } + else if (typeof example !== "object") { + actions.badParameter(req, res, "example"); } else { - actions.resultNotFound(req, res, "no match"); + var result = collection.byExample(example).limit(1); + + if (result.hasNext()) { + actions.resultOk(req, res, actions.HTTP_OK, { document : result.next() }); + } + else { + actions.resultNotFound(req, res, "no match"); + } } } } + catch (err) { + actions.resultException(req, res, err); + } } }); @@ -520,43 +525,43 @@ actions.defineHttp({ context : "api", callback : function (req, res) { - var body = actions.getJsonBody(req, res); + try { + var body = actions.getJsonBody(req, res); - if (body === undefined) { - return; - } - - if (req.requestType != actions.PUT) { - actions.resultUnsupported(req, res); - } - else { - var limit = body.limit; - var skip = body.skip; - var name = body.collection; - var example = body.example; - var index = body.index; - - var name = body.collection; - var id = parseInt(name) || name; - var collection = internal.db._collection(id); - - if (collection === null) { - actions.collectionNotFound(req, res, name); + if (body === undefined) { + return; } - else if (typeof example !== "object") { - actions.badParameter(req, res, "example"); + + if (req.requestType != actions.PUT) { + actions.resultUnsupported(req, res); } else { - try { + var limit = body.limit; + var skip = body.skip; + var name = body.collection; + var example = body.example; + var index = body.index; + + var name = body.collection; + var id = parseInt(name) || name; + var collection = internal.db._collection(id); + + if (collection === null) { + actions.collectionNotFound(req, res, name); + } + else if (typeof example !== "object") { + actions.badParameter(req, res, "example"); + } + else { var result = collection.BY_EXAMPLE_HASH(index, example, skip, limit); actions.resultOk(req, res, actions.HTTP_OK, result); } - catch (err) { - actions.resultException(req, res, err); - } } } + catch (err) { + actions.resultException(req, res, err); + } } }); @@ -600,33 +605,33 @@ actions.defineHttp({ context : "api", callback : function (req, res) { - var body = actions.getJsonBody(req, res); + try { + var body = actions.getJsonBody(req, res); - if (body === undefined) { - return; - } + if (body === undefined) { + return; + } - if (req.requestType != actions.PUT) { - actions.resultUnsupported(req, res); - } - else { - var limit = body.limit; - var skip = body.skip; - var name = body.collection; - var attribute = body.attribute; - var left = body.left; - var right = body.right; - var closed = body.closed; - - var name = body.collection; - var id = parseInt(name) || name; - var collection = internal.db._collection(id); - - if (collection === null) { - actions.collectionNotFound(req, res, name); + if (req.requestType != actions.PUT) { + actions.resultUnsupported(req, res); } else { - try { + var limit = body.limit; + var skip = body.skip; + var name = body.collection; + var attribute = body.attribute; + var left = body.left; + var right = body.right; + var closed = body.closed; + + var name = body.collection; + var id = parseInt(name) || name; + var collection = internal.db._collection(id); + + if (collection === null) { + actions.collectionNotFound(req, res, name); + } + else { var result; if (closed) { @@ -646,11 +651,11 @@ actions.defineHttp({ actions.resultCursor(req, res, CREATE_CURSOR(result.toArray(), true, body.batchSize)); } - catch (err) { - actions.resultException(req, res, err); - } } } + catch (err) { + actions.resultException(req, res, err); + } } }); diff --git a/js/actions/system/api-system.js b/js/actions/system/api-system.js index 558647f2c7..fef420e4f9 100644 --- a/js/actions/system/api-system.js +++ b/js/actions/system/api-system.js @@ -80,7 +80,14 @@ actions.defineHttp({ url : "", prefix : true, context : "admin", - callback : Routing + callback : function (req, res) { + try { + Routing(req, res); + } + catch (err) { + actions.resultException(req, res, err); + } + } }); actions.defineHttp({ @@ -88,8 +95,13 @@ actions.defineHttp({ context : "admin", prefix : false, callback : function (req, res) { - internal.executeGlobalContextFunction("require(\"org/arangodb/actions\").reloadRouting()"); - actions.resultOk(req, res, actions.HTTP_OK); + try { + internal.executeGlobalContextFunction("require(\"org/arangodb/actions\").reloadRouting()"); + actions.resultOk(req, res, actions.HTTP_OK); + } + catch (err) { + actions.resultException(req, res, err); + } } }); @@ -235,7 +247,7 @@ actions.defineHttp({ actions.resultOk(req, res, actions.HTTP_OK, result); } catch (err) { - actions.resultError(req, res, err); + actions.resultException(req, res, err); } } }); @@ -301,20 +313,25 @@ actions.defineHttp({ context : "admin", prefix : false, callback : function (req, res) { - if (req.requestType === actions.GET) { - GET_admin_session(req, res); + try { + if (req.requestType === actions.GET) { + GET_admin_session(req, res); + } + else if (req.requestType === actions.DELETE) { + DELETE_admin_session(req, res); + } + else if (req.requestType === actions.POST) { + POST_admin_session(req, res); + } + else if (req.requestType === actions.PUT) { + PUT_admin_session(req, res); + } + else { + actions.resultUnsupported(req, res); + } } - else if (req.requestType === actions.DELETE) { - DELETE_admin_session(req, res); - } - else if (req.requestType === actions.POST) { - POST_admin_session(req, res); - } - else if (req.requestType === actions.PUT) { - PUT_admin_session(req, res); - } - else { - actions.resultUnsupported(req, res); + catch (err) { + actions.resultException(req, res, err); } } }); diff --git a/js/actions/system/key-value.js b/js/actions/system/key-value.js index 938f1e260b..397ad129db 100644 --- a/js/actions/system/key-value.js +++ b/js/actions/system/key-value.js @@ -122,37 +122,32 @@ function postKeyValue(req, res) { return; } - try { - var collection = req.suffix[0]; + var collection = req.suffix[0]; - if (internal.db._collection(collection) === null) { - actions.collectionNotFound(req, res, collection); - return; - } - - if (req.requestBody == "") { - actions.resultError(req, res, actions.HTTP_BAD, actions.ERROR_KEYVALUE_NO_VALUE, actions.getErrorMessage(actions.ERROR_KEYVALUE_NO_VALUE)); - return; - } - - var doc = buildDocumentFromReq(req); - - var oldDoc = internal.db._collection(collection).firstExample("$key", doc["$key"]); - - if (oldDoc != undefined) { - actions.resultError(req, res, actions.HTTP_BAD, actions.ERROR_KEYVALUE_KEY_EXISTS, actions.getErrorMessage(actions.ERROR_KEYVALUE_KEY_EXISTS)); - } - else { - var id = db[collection].save(doc); - var result = { - "saved" : true, - "_id" : id - } - actions.resultOk(req, res, actions.HTTP_CREATED, result); - } + if (internal.db._collection(collection) === null) { + actions.collectionNotFound(req, res, collection); + return; } - catch (err) { - actions.resultException(req, res, err); + + if (req.requestBody == "") { + actions.resultError(req, res, actions.HTTP_BAD, actions.ERROR_KEYVALUE_NO_VALUE, actions.getErrorMessage(actions.ERROR_KEYVALUE_NO_VALUE)); + return; + } + + var doc = buildDocumentFromReq(req); + + var oldDoc = internal.db._collection(collection).firstExample("$key", doc["$key"]); + + if (oldDoc != undefined) { + actions.resultError(req, res, actions.HTTP_BAD, actions.ERROR_KEYVALUE_KEY_EXISTS, actions.getErrorMessage(actions.ERROR_KEYVALUE_KEY_EXISTS)); + } + else { + var id = db[collection].save(doc); + var result = { + "saved" : true, + "_id" : id + } + actions.resultOk(req, res, actions.HTTP_CREATED, result); } } @@ -170,52 +165,47 @@ function putKeyValue(req, res) { return; } - try { - var collection = req.suffix[0]; + var collection = req.suffix[0]; - if (internal.db._collection(collection) === null) { - actions.collectionNotFound(req, res); - return; - } + if (internal.db._collection(collection) === null) { + actions.collectionNotFound(req, res); + return; + } - var doc = buildDocumentFromReq(req); + var doc = buildDocumentFromReq(req); - var oldDoc = internal.db._collection(collection).firstExample("$key", doc["$key"]); + var oldDoc = internal.db._collection(collection).firstExample("$key", doc["$key"]); - if (oldDoc == undefined) { - if (req.parameters["create"] == 1) { - var id = db[collection].save(doc); - var result = { - "saved" : true, - "_id" : id - } - actions.resultOk(req, res, actions.HTTP_CREATED, result); - return; + if (oldDoc == undefined) { + if (req.parameters["create"] == 1) { + var id = db[collection].save(doc); + var result = { + "saved" : true, + "_id" : id } - actions.resultError(req, res, actions.HTTP_NOT_FOUND, actions.ERROR_KEYVALUE_KEY_NOT_FOUND, actions.getErrorMessage(actions.ERROR_KEYVALUE_KEY_NOT_FOUND)); + actions.resultOk(req, res, actions.HTTP_CREATED, result); + return; + } + actions.resultError(req, res, actions.HTTP_NOT_FOUND, actions.ERROR_KEYVALUE_KEY_NOT_FOUND, actions.getErrorMessage(actions.ERROR_KEYVALUE_KEY_NOT_FOUND)); + } + else { + // get _id + var id = oldDoc._id; + + // save x-voc-created + var created = oldDoc["$created"]; + if (created != undefined) { + doc["x-voc-created"] = created; + } + + // replace the document + if (db[collection].replace(id, doc)) { + actions.resultOk(req, res, actions.HTTP_ACCEPTED, {"changed" : true}); } else { - // get _id - var id = oldDoc._id; - - // save x-voc-created - var created = oldDoc["$created"]; - if (created != undefined) { - doc["x-voc-created"] = created; - } - - // replace the document - if (db[collection].replace(id, doc)) { - actions.resultOk(req, res, actions.HTTP_ACCEPTED, {"changed" : true}); - } - else { - actions.resultError(req, res, actions.HTTP_BAD, actions.ERROR_KEYVALUE_KEY_NOT_CHANGED, actions.getErrorMessage(actions.ERROR_KEYVALUE_KEY_NOT_CHANGED)); - } + actions.resultError(req, res, actions.HTTP_BAD, actions.ERROR_KEYVALUE_KEY_NOT_CHANGED, actions.getErrorMessage(actions.ERROR_KEYVALUE_KEY_NOT_CHANGED)); } } - catch (err) { - actions.resultException(req, res, err); - } } //////////////////////////////////////////////////////////////////////////////// @@ -231,38 +221,33 @@ function deleteKeyValue(req, res) { return; } - try { - var collection = req.suffix[0]; + var collection = req.suffix[0]; - if (internal.db._collection(collection) === null) { - actions.collectionNotFound(req, res); - return; - } + if (internal.db._collection(collection) === null) { + actions.collectionNotFound(req, res); + return; + } - var key = req.suffix[1]; + var key = req.suffix[1]; - for (var i = 2; i < req.suffix.length; ++i) { - key += "/" + req.suffix[i]; - } + for (var i = 2; i < req.suffix.length; ++i) { + key += "/" + req.suffix[i]; + } - var doc = internal.db._collection(collection).firstExample("$key", key); + var doc = internal.db._collection(collection).firstExample("$key", key); - if (doc == undefined) { - actions.resultError(req, res, actions.HTTP_NOT_FOUND, actions.ERROR_KEYVALUE_KEY_NOT_FOUND, actions.getErrorMessage(actions.ERROR_KEYVALUE_KEY_NOT_FOUND)); + if (doc == undefined) { + actions.resultError(req, res, actions.HTTP_NOT_FOUND, actions.ERROR_KEYVALUE_KEY_NOT_FOUND, actions.getErrorMessage(actions.ERROR_KEYVALUE_KEY_NOT_FOUND)); + } + else { + var id = doc._id; + if (db[collection].remove(id)) { + actions.resultOk(req, res, actions.HTTP_ACCEPTED, {"removed" : true}); } else { - var id = doc._id; - if (db[collection].remove(id)) { - actions.resultOk(req, res, actions.HTTP_ACCEPTED, {"removed" : true}); - } - else { - actions.resultError(req, res, actions.HTTP_BAD, actions.ERROR_KEYVALUE_KEY_NOT_REMOVED, actions.getErrorMessage(actions.ERROR_KEYVALUE_KEY_NOT_REMOVED)); - } + actions.resultError(req, res, actions.HTTP_BAD, actions.ERROR_KEYVALUE_KEY_NOT_REMOVED, actions.getErrorMessage(actions.ERROR_KEYVALUE_KEY_NOT_REMOVED)); } } - catch (err) { - actions.resultException(req, res, err); - } } //////////////////////////////////////////////////////////////////////////////// @@ -280,46 +265,41 @@ function getKeyValue(req, res) { return; } - try { - var collection = req.suffix[0]; + var collection = req.suffix[0]; - if (internal.db._collection(collection) === null) { - actions.collectionNotFound(req, res); - return; - } - - var key = req.suffix[1]; - - for (var i = 2; i < req.suffix.length; ++i) { - key += "/" + req.suffix[i]; - } - - var doc = internal.db._collection(collection).firstExample("$key", key); - - if (doc == undefined) { - actions.resultError(req, res, actions.HTTP_NOT_FOUND, actions.ERROR_KEYVALUE_KEY_NOT_FOUND, actions.getErrorMessage(actions.ERROR_KEYVALUE_KEY_NOT_FOUND)); - } - else { - var headers = {}; - - if (doc["$expires"] != undefined) { - // format timestamp - headers["x-voc-expires"] = formatTimeStamp(doc["$expires"]); - } - if (doc["$extended"] != undefined) { - // serialize header value - headers["x-voc-extended"] = JSON.stringify(doc["$extended"]); - } - if (doc["$created"] != undefined) { - // format timestamp - headers["x-voc-created"] = formatTimeStamp(doc["$created"]); - } - - actions.resultOk(req, res, actions.HTTP_OK, doc["$value"], headers); - } + if (internal.db._collection(collection) === null) { + actions.collectionNotFound(req, res); + return; } - catch (err) { - actions.resultException(req, res, err); + + var key = req.suffix[1]; + + for (var i = 2; i < req.suffix.length; ++i) { + key += "/" + req.suffix[i]; + } + + var doc = internal.db._collection(collection).firstExample("$key", key); + + if (doc == undefined) { + actions.resultError(req, res, actions.HTTP_NOT_FOUND, actions.ERROR_KEYVALUE_KEY_NOT_FOUND, actions.getErrorMessage(actions.ERROR_KEYVALUE_KEY_NOT_FOUND)); + } + else { + var headers = {}; + + if (doc["$expires"] != undefined) { + // format timestamp + headers["x-voc-expires"] = formatTimeStamp(doc["$expires"]); + } + if (doc["$extended"] != undefined) { + // serialize header value + headers["x-voc-extended"] = JSON.stringify(doc["$extended"]); + } + if (doc["$created"] != undefined) { + // format timestamp + headers["x-voc-created"] = formatTimeStamp(doc["$created"]); + } + + actions.resultOk(req, res, actions.HTTP_OK, doc["$value"], headers); } } @@ -336,25 +316,30 @@ actions.defineHttp({ context : "api", callback : function (req, res) { - switch (req.requestType) { - case (actions.POST) : - postKeyValue(req, res); - break; + try { + switch (req.requestType) { + case (actions.POST) : + postKeyValue(req, res); + break; - case (actions.GET) : - getKeyValue(req, res); - break; + case (actions.GET) : + getKeyValue(req, res); + break; - case (actions.PUT) : - putKeyValue(req, res); - break; + case (actions.PUT) : + putKeyValue(req, res); + break; - case (actions.DELETE) : - deleteKeyValue(req, res); - break; + case (actions.DELETE) : + deleteKeyValue(req, res); + break; - default: - actions.resultUnsupported(req, res); + default: + actions.resultUnsupported(req, res); + } + } + catch (err) { + actions.resultException(req, res, err); } } }); @@ -386,40 +371,35 @@ function searchKeyValue(req, res) { return; } - try { - var collection = req.suffix[0]; + var collection = req.suffix[0]; - if (internal.db._collection(collection) === null) { - actions.collectionNotFound(req, res); - return; - } - - var prefix = req.suffix[1]; - - for (var i = 2; i < req.suffix.length; ++i) { - prefix += "/" + req.suffix[i]; - } - - // - // TODO: build a query which selects the keys - // - - var cursor = internal.db._collection(collection).all(); - - result = []; - - while (cursor.hasNext() ) { - var doc = cursor.next(); - if (doc["$key"] != undefined && doc["$key"].indexOf(prefix) === 0) { - result.push(doc["$key"]); - } + if (internal.db._collection(collection) === null) { + actions.collectionNotFound(req, res); + return; + } + + var prefix = req.suffix[1]; + + for (var i = 2; i < req.suffix.length; ++i) { + prefix += "/" + req.suffix[i]; + } + + // + // TODO: build a query which selects the keys + // + + var cursor = internal.db._collection(collection).all(); + + result = []; + + while (cursor.hasNext() ) { + var doc = cursor.next(); + if (doc["$key"] != undefined && doc["$key"].indexOf(prefix) === 0) { + result.push(doc["$key"]); } + } - actions.resultOk(req, res, actions.HTTP_OK, result); - } - catch (err) { - actions.resultException(req, res, err); - } + actions.resultOk(req, res, actions.HTTP_OK, result); } // ----------------------------------------------------------------------------- @@ -435,13 +415,18 @@ actions.defineHttp({ context : "api", callback : function (req, res) { - switch (req.requestType) { - case (actions.GET) : - searchKeyValue(req, res); - break; + try { + switch (req.requestType) { + case (actions.GET) : + searchKeyValue(req, res); + break; - default: - actions.resultUnsupported(req, res); + default: + actions.resultUnsupported(req, res); + } + } + catch (err) { + actions.resultException(req, res, err); } } });