1
0
Fork 0

better error handling

This commit is contained in:
Frank Celler 2012-03-05 10:06:41 +01:00
parent 05b7fb35b7
commit 3611799e06
12 changed files with 272 additions and 99 deletions

View File

@ -97,12 +97,12 @@ string ActionDisptacherThread::_startupModules;
/// @brief constructs a new dispatcher thread
////////////////////////////////////////////////////////////////////////////////
ActionDisptacherThread::ActionDisptacherThread (DispatcherQueue* queue, string const& actionContext)
ActionDisptacherThread::ActionDisptacherThread (DispatcherQueue* queue, string const& actionQeue)
: DispatcherThread(queue),
_report(false),
_isolate(0),
_context(),
_actionContext(actionContext) {
_actionQeue(actionQeue) {
}
////////////////////////////////////////////////////////////////////////////////
@ -254,7 +254,7 @@ void ActionDisptacherThread::initialise () {
_context->Enter();
TRI_InitV8VocBridge(_context, _vocbase);
TRI_InitV8Actions(_context, _actionContext.c_str());
TRI_InitV8Actions(_context, _actionQeue.c_str());
TRI_InitV8Conversions(_context);
TRI_InitV8Utils(_context, _startupModules);
TRI_InitV8Shell(_context);
@ -283,10 +283,6 @@ void ActionDisptacherThread::initialise () {
if (! ok) {
LOGGER_FATAL << "cannot load actions from directory '" << loader->getDirectory() << "'";
cerr << "cannot load actions from directory '" << loader->getDirectory() << "'\n";
_context->Exit();
_isolate->Exit();
exit(EXIT_FAILURE);
}
}

View File

@ -113,7 +113,7 @@ namespace triagens {
/// @brief constructs a new dispatcher thread
////////////////////////////////////////////////////////////////////////////////
ActionDisptacherThread (rest::DispatcherQueue* queue, string const& actionContext);
ActionDisptacherThread (rest::DispatcherQueue* queue, string const& actionQeue);
////////////////////////////////////////////////////////////////////////////////
/// @brief destructs a dispatcher thread
@ -248,7 +248,7 @@ namespace triagens {
/// @brief action context
////////////////////////////////////////////////////////////////////////////////
std::string _actionContext;
std::string _actionQeue;
};
}
}

View File

@ -544,7 +544,7 @@ HttpResponse* TRI_ExecuteActionVocBase (TRI_vocbase_t* vocbase,
/// @brief stores the V8 actions function inside the global variable
////////////////////////////////////////////////////////////////////////////////
void TRI_InitV8Actions (v8::Handle<v8::Context> context, char const* actionContext) {
void TRI_InitV8Actions (v8::Handle<v8::Context> context, char const* actionQeue) {
v8::HandleScope scope;
// check the isolate
@ -560,8 +560,8 @@ void TRI_InitV8Actions (v8::Handle<v8::Context> context, char const* actionConte
// create the global constants
// .............................................................................
context->Global()->Set(v8::String::New("SYS_ACTION_CONTEXT"),
v8::String::New(actionContext),
context->Global()->Set(v8::String::New("SYS_ACTION_QUEUE"),
v8::String::New(actionQeue),
v8::ReadOnly);
// .............................................................................

View File

@ -243,7 +243,7 @@ triagens::rest::HttpResponse* TRI_ExecuteActionVocBase (TRI_vocbase_t* vocbase,
/// @brief stores the V8 actions function inside the global variable
////////////////////////////////////////////////////////////////////////////////
void TRI_InitV8Actions (v8::Handle<v8::Context> context, char const* actionContext);
void TRI_InitV8Actions (v8::Handle<v8::Context> context, char const* actionQeue);
////////////////////////////////////////////////////////////////////////////////
/// @}

View File

@ -638,6 +638,8 @@ static v8::Handle<v8::Value> JS_Execute (v8::Arguments const& argv) {
if (script.IsEmpty()) {
assert(tryCatch.HasCaught());
TRI_PrintV8Exception(&tryCatch);
if (useSandbox) {
context->DetachGlobal();
context->Exit();
@ -652,6 +654,8 @@ static v8::Handle<v8::Value> JS_Execute (v8::Arguments const& argv) {
if (result.IsEmpty()) {
assert(tryCatch.HasCaught());
TRI_PrintV8Exception(&tryCatch);
if (useSandbox) {
context->DetachGlobal();
context->Exit();
@ -1312,6 +1316,7 @@ bool TRI_LoadJavaScriptFile (v8::Handle<v8::Context> context, char const* filena
// compilation failed, print errors that happened during compilation
if (script.IsEmpty()) {
LOG_ERROR("cannot compile java script file '%s'", filename);
TRI_ReportV8Exception(&tryCatch);
return false;
}
@ -1323,6 +1328,7 @@ bool TRI_LoadJavaScriptFile (v8::Handle<v8::Context> context, char const* filena
assert(tryCatch.HasCaught());
// print errors that happened during execution
LOG_ERROR("cannot execute java script file '%s'", filename);
TRI_ReportV8Exception(&tryCatch);
return false;

View File

@ -25,7 +25,7 @@
/// @author Copyright 2012, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
var actions = require("actions");
// var actions = require("actions");
////////////////////////////////////////////////////////////////////////////////
/// @brief geo "near" query

View File

@ -1,82 +1,4 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief returns an error
///
/// @FUN{actionResultError(@FA{req}, @FA{res}, @FA{httpReturnCode}, @FA{errorNum}, @FA{errorMessage}, @FA{headers})}
///
/// The functions returns an error json object. The returned object is an array
/// with an attribute @LIT{errorMessage} containing the error message
/// @FA{errorMessage}.
////////////////////////////////////////////////////////////////////////////////
function actionResultError (req, res, httpReturnCode, errorNum, errorMessage, headers) {
res.responseCode = httpReturnCode;
res.contentType = "application/json";
var result = {
"error" : true,
"code" : httpReturnCode,
"errorNum" : errorNum,
"errorMessage" : errorMessage
}
res.body = JSON.stringify(result);
if (headers != undefined) {
res.headers = headers;
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief returns an error for unsupported methods
///
/// @FUN{actionResultUnsupported(@FA{req}, @FA{res}, @FA{headers})}
///
/// The functions returns an error json object. The returned object is an array
/// with an attribute @LIT{errorMessage} containing the error message
/// @FA{errorMessage}.
////////////////////////////////////////////////////////////////////////////////
function actionResultUnsupported (req, res, headers) {
actionResultError(req, res, 405, 405, "Unsupported method", headers);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief returns a result
///
/// @FUN{actionResultOK(@FA{req}, @FA{res}, @FA{httpReturnCode}, @FA{result}, @FA{headers}})}
///
////////////////////////////////////////////////////////////////////////////////
function actionResultOK (req, res, httpReturnCode, result, headers) {
res.responseCode = httpReturnCode;
res.contentType = "application/json";
// add some default attributes to result:
result.error = false;
result.code = httpReturnCode;
res.body = JSON.stringify(result);
if (headers != undefined) {
res.headers = headers;
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief error codes
////////////////////////////////////////////////////////////////////////////////
var queryNotFound = 10404;
var queryNotModified = 10304;
var collectionNotFound = 20404;
var documentNotFound = 30404;
var documentNotModified = 30304;
var cursorNotFound = 40404;
var cursorNotModified = 40304;
var myApiRequests = {};

View File

@ -102,7 +102,14 @@ static string JS_bootstrap_modules =
" ModuleCache[path] = module = new Module(path);\n"
"\n"
" content = \"(function (module, exports, require, print) {\" + content + \"\\n});\";\n"
" f = SYS_EXECUTE(content, undefined, path);\n"
"\n"
" try {\n"
" f = SYS_EXECUTE(content, undefined, path);\n"
" }\n"
" catch (err) {\n"
" CONSOLE_ERROR(\"in file %s: %o\", path, err.stack);\n"
" throw err;\n"
" }\n"
"\n"
" if (f == undefined) {\n"
" throw \"cannot create context function\";\n"

View File

@ -101,7 +101,14 @@ Module.prototype.require = function (path) {
ModuleCache[path] = module = new Module(path);
content = "(function (module, exports, require, print) {" + content + "\n});";
f = SYS_EXECUTE(content, undefined, path);
try {
f = SYS_EXECUTE(content, undefined, path);
}
catch (err) {
CONSOLE_ERROR("in file %s: %o", path, err.stack);
throw err;
}
if (f == undefined) {
throw "cannot create context function";

View File

@ -33,6 +33,11 @@ var console = require("console");
///
/// <ol>
/// <li>@ref JSModuleActionsDefineHttp "defineHttp"</li>
/// <li>@ref JSModuleActionsActionResult "actionResult"</li>
/// <li>@ref JSModuleActionsActionResultOK "actionResultOK"</li>
/// <li>@ref JSModuleActionsActionResultError "actionResultError"</li>
/// <li>@ref JSModuleActionsActionResultUnsupported "actionResultUnsupported"</li>
/// <li>@ref JSModuleActionsActionError "actionError"</li>
/// </ol>
////////////////////////////////////////////////////////////////////////////////
@ -53,16 +58,55 @@ var console = require("console");
/// @copydetails JSF_actionResult
/// <hr>
///
/// @anchor JSModuleActionsActionResultOK
/// @copydetails JSF_actionResultOK
/// <hr>
///
/// @anchor JSModuleActionsActionResultError
/// @copydetails JSF_actionResultError
/// <hr>
///
/// @anchor JSModuleActionsActionResultUnsupported
/// @copydetails JSF_actionResultUnsupported
/// <hr>
///
/// @anchor JSModuleActionsActionError
/// @copydetails JSF_actionError
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- public constants
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup AvocadoActions
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief error codes
////////////////////////////////////////////////////////////////////////////////
exports.queryNotFound = 10404;
exports.queryNotModified = 10304;
exports.collectionNotFound = 20404;
exports.documentNotFound = 30404;
exports.documentNotModified = 30304;
exports.cursorNotFound = 40404;
exports.cursorNotModified = 40304;
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- public functions
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup V8Json V8 JSON
/// @addtogroup AvocadoActions
/// @{
////////////////////////////////////////////////////////////////////////////////
@ -177,7 +221,7 @@ function defineHttp (options) {
try {
internal.defineAction(url, queue, callback, parameter);
console.debug("defining action '" + url + "' in context " + context " using queue " + queue);
console.debug("defining action '" + url + "' in context " + context + " using queue " + queue);
}
catch (err) {
console.error("action '" + url + "' encountered error: " + err);
@ -224,6 +268,76 @@ function actionError (req, res, err) {
res.body = JSON.stringify({ 'error' : "" + err });
}
////////////////////////////////////////////////////////////////////////////////
/// @brief returns a result
///
/// @FUN{actionResultOK(@FA{req}, @FA{res}, @FA{code}, @FA{result}, @FA{headers}})}
///
/// Works like @FN{actionResult} but adds the attribute @LIT{error} with
/// value @LIT{false} and @LIT{code} with value @FA{code} to the @FA{result}.
////////////////////////////////////////////////////////////////////////////////
function actionResultOK (req, res, httpReturnCode, result, headers) {
res.responseCode = httpReturnCode;
res.contentType = "application/json";
// add some default attributes to result
if (result == undefined) {
result = {};
}
result.error = false;
result.code = httpReturnCode;
res.body = JSON.stringify(result);
if (headers != undefined) {
res.headers = headers;
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief returns an error
///
/// @FUN{actionResultError(@FA{req}, @FA{res}, @FA{code}, @FA{errorNum}, @FA{errorMessage}, @FA{headers})}
///
/// The functions generates an error response. The response body is an array
/// with an attribute @LIT{errorMessage} containing the error message
/// @FA{errorMessage}, @LIT{error} containing @LIT{true}, @LIT{code}
/// containing @FA{code}, @LIT{errorNum} containing @FA{errorNum}, and
/// $LIT{errorMessage} containing the error message @FA{errorMessage}.
////////////////////////////////////////////////////////////////////////////////
function actionResultError (req, res, httpReturnCode, errorNum, errorMessage, headers) {
res.responseCode = httpReturnCode;
res.contentType = "application/json";
var result = {
"error" : true,
"code" : httpReturnCode,
"errorNum" : errorNum,
"errorMessage" : errorMessage
}
res.body = JSON.stringify(result);
if (headers != undefined) {
res.headers = headers;
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief returns an error for unsupported methods
///
/// @FUN{actionResultUnsupported(@FA{req}, @FA{res}, @FA{headers})}
///
/// The functions generates an error response.
////////////////////////////////////////////////////////////////////////////////
function actionResultUnsupported (req, res, headers) {
actionResultError(req, res, 405, 405, "Unsupported method", headers);
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
@ -233,11 +347,16 @@ function actionError (req, res, err) {
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup AvocadoGraph
/// @addtogroup AvocadoActions
/// @{
////////////////////////////////////////////////////////////////////////////////
exports.defineHttp = defineHttp;
exports.actionResult = actionResult;
exports.actionResultOK = actionResultOK;
exports.actionResultError = actionResultError;
exports.actionResultUnsupported = actionResultUnsupported;
exports.actionError = actionError;
////////////////////////////////////////////////////////////////////////////////
/// @}

View File

@ -1,11 +1,11 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief Avocado Query Language
/// @brief Avocado Simple Query Language
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2010-2012 triagens GmbH, Cologne, Germany
/// Copyright 2012 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.

116
js/system/api-help.js Normal file
View File

@ -0,0 +1,116 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief JavaScript actions modules
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2012 triagens GmbH, Cologne, Germany
///
/// Licensed 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.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Dr. Frank Celler
/// @author Copyright 2012, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
var actions = require("actions");
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup AvocadoAPI
/// @{
////////////////////////////////////////////////////////////////////////////////
var API = "_api";
var ApiRequests = {};
ApiRequests.cursor = {
"POST /" + API + "/cursor" : "create and execute query. (creates a cursor)",
"PUT /" + API + "/cursor/<cursor-id>" : "get next results",
"DELETE /" + API + "/cursor/<cursor-id>" : "delete cursor"
}
ApiRequests.collection = {
"GET /" + API + "/collections" : "get list of collections",
"GET /" + API + "/collection/<collection-id>" : "get all elements of collection"
}
ApiRequests.document = {
"POST /" + API + "/document/<collection-id>" : "create new document",
"PUT /" + API + "/document/<collection-id>/<document-id>" : "update document",
"GET /" + API + "/document/<collection-id>/<document-id>" : "get a document",
"DELETE /" + API + "/document/<collection-id>/<document-id>" : "delete a document"
}
ApiRequests.query = {
"POST /" + API + "/query" : "create a query",
"GET /" + API + "/query/<query-id>" : "get query",
"PUT /" + API + "/query/<query-id>" : "change query",
"DELETE /" + API + "/query/<query-id>" : "delete query"
}
// -----------------------------------------------------------------------------
// --SECTION-- public functions
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup AvocadoAPI
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief returns a help
////////////////////////////////////////////////////////////////////////////////
actions.defineHttp({
url : API + "help",
context : "api",
callback : function (req, res) {
var result = {
requests : ApiRequests
}
actionResultOK(req, res, 200, result);
}
});
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- MODULE EXPORTS
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup AvocadoAPI
/// @{
////////////////////////////////////////////////////////////////////////////////
exports.apiPrefix = API;
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// @addtogroup\\|// --SECTION--\\|/// @page\\|/// @}\\)"
// End: