Ejemplo n.º 1
0
void Executor::HandleV8Error (v8::TryCatch& tryCatch,
                              v8::Handle<v8::Value>& result) {
  ISOLATE;
  if (tryCatch.HasCaught()) {
    // caught a V8 exception
    if (! tryCatch.CanContinue()) {
      // request was cancelled
      TRI_GET_GLOBALS();
      v8g->_canceled = true;

      THROW_ARANGO_EXCEPTION(TRI_ERROR_REQUEST_CANCELED);
    }

    // request was not cancelled, but some other error occurred
    // peek into the exception
    if (tryCatch.Exception()->IsObject()) {
      // cast the exception to an object

      v8::Handle<v8::Array> objValue = v8::Handle<v8::Array>::Cast(tryCatch.Exception());
      v8::Handle<v8::String> errorNum = TRI_V8_ASCII_STRING("errorNum");
      v8::Handle<v8::String> errorMessage = TRI_V8_ASCII_STRING("errorMessage");
        
      if (objValue->HasOwnProperty(errorNum) && 
          objValue->HasOwnProperty(errorMessage)) {
        v8::Handle<v8::Value> errorNumValue = objValue->Get(errorNum);
        v8::Handle<v8::Value> errorMessageValue = objValue->Get(errorMessage);

        // found something that looks like an ArangoError
        if ((errorNumValue->IsNumber() || errorNumValue->IsNumberObject()) && 
            (errorMessageValue->IsString() || errorMessageValue->IsStringObject())) {
          int errorCode = static_cast<int>(TRI_ObjectToInt64(errorNumValue));
          std::string const errorMessage(TRI_ObjectToString(errorMessageValue));
    
          THROW_ARANGO_EXCEPTION_MESSAGE(errorCode, errorMessage);
        }
      }
    
      // exception is no ArangoError
      std::string const details(TRI_ObjectToString(tryCatch.Exception()));
      THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_QUERY_SCRIPT, details);
    }
    
    // we can't figure out what kind of error occured and throw a generic error
    THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
  }
  
  if (result.IsEmpty()) {
    THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
  }

  // if we get here, no exception has been raised
}
Ejemplo n.º 2
0
AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(const JsValueList& params, JsValuePtr thisPtr) const
{
  if (!IsFunction())
    throw new std::runtime_error("Attempting to call a non-function");

  const JsContext context(jsEngine);
  if (!thisPtr)
  {
	  v8::Local<v8::Context> localContext = v8::Local<v8::Context>::New(jsEngine->GetIsolate(), *jsEngine->context);
    thisPtr = JsValuePtr(new JsValue(jsEngine, localContext->Global()));
  }
  if (!thisPtr->IsObject())
    throw new std::runtime_error("`this` pointer has to be an object");
  v8::Local<v8::Object> thisObj = v8::Local<v8::Object>::Cast(thisPtr->UnwrapValue());

  std::vector<v8::Handle<v8::Value>> argv;
  for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it)
    argv.push_back((*it)->UnwrapValue());

  const v8::TryCatch tryCatch;
  v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue());
  v8::Local<v8::Value> result = func->Call(thisObj, argv.size(),
      argv.size() ? &argv.front() : 0);

  if (tryCatch.HasCaught())
    throw JsError(tryCatch.Exception(), tryCatch.Message());

  return JsValuePtr(new JsValue(jsEngine, result));
}
Ejemplo n.º 3
0
void InjectedScript::wrapEvaluateResult(ErrorString* errorString, v8::MaybeLocal<v8::Value> maybeResultValue, const v8::TryCatch& tryCatch, const String16& objectGroup, bool returnByValue, bool generatePreview, std::unique_ptr<protocol::Runtime::RemoteObject>* result, Maybe<bool>* wasThrown, Maybe<protocol::Runtime::ExceptionDetails>* exceptionDetails)
{
    v8::Local<v8::Value> resultValue;
    if (!tryCatch.HasCaught()) {
        if (hasInternalError(errorString, !maybeResultValue.ToLocal(&resultValue)))
            return;
        std::unique_ptr<RemoteObject> remoteObject = wrapObject(errorString, resultValue, objectGroup, returnByValue, generatePreview);
        if (!remoteObject)
            return;
        if (objectGroup == "console")
            m_lastEvaluationResult.Reset(m_context->isolate(), resultValue);
        *result = std::move(remoteObject);
        if (wasThrown)
            *wasThrown = false;
    } else {
        v8::Local<v8::Value> exception = tryCatch.Exception();
        std::unique_ptr<RemoteObject> remoteObject = wrapObject(errorString, exception, objectGroup, false, generatePreview && !exception->IsNativeError());
        if (!remoteObject)
            return;
        *result = std::move(remoteObject);
        if (exceptionDetails)
            *exceptionDetails = createExceptionDetails(tryCatch.Message());
        if (wasThrown)
            *wasThrown = true;
    }
}
Ejemplo n.º 4
0
void reportException(ScriptState* scriptState, v8::TryCatch& exceptionCatcher)
{
    String errorMessage;
    int lineNumber = 0;
    String sourceURL;

    // There can be a situation that an exception is thrown without setting a message.
    v8::Local<v8::Message> message = exceptionCatcher.Message();
    if (message.IsEmpty()) {
        v8::Local<v8::String> exceptionString = exceptionCatcher.Exception()->ToString();
        // Conversion of the exception object to string can fail if an
        // exception is thrown during conversion.
        if (!exceptionString.IsEmpty())
            errorMessage = toWebCoreString(exceptionString);
    } else {
        errorMessage = toWebCoreString(message->Get());
        lineNumber = message->GetLineNumber();
        sourceURL = toWebCoreString(message->GetScriptResourceName());
    }

    // Do not report the exception if the current execution context is Document because we do not want to lead to duplicate error messages in the console.
    // FIXME (31171): need better design to solve the duplicate error message reporting problem.
    ScriptExecutionContext* context = getScriptExecutionContext(scriptState);
    // During the frame teardown, there may not be a valid context.
    if (context && !context->isDocument())
        context->reportException(errorMessage, lineNumber, sourceURL);
    exceptionCatcher.Reset();
}
Ejemplo n.º 5
0
ScriptResult 
JavascriptEngineV8::createErrorResult( std::string prefix, const v8::TryCatch& try_catch )
{
    std::ostringstream str;

    v8::String::AsciiValue error( try_catch.Exception() );
    v8::Handle<v8::Message> message = try_catch.Message();

    str << prefix << ": ";

    if( !message.IsEmpty() ) {
      //v8::String::AsciiValue filename(message->GetScriptResourceName());
      int linenum = message->GetLineNumber();
      str << "line " << linenum << " cols [" << message->GetStartColumn() << "-" << message->GetEndColumn() << "] " << std::string( *error ) << std::endl;
      v8::String::AsciiValue sourceline( message->GetSourceLine() );
      str << std::string( *sourceline ) << std::endl;
      /*
      v8::String::Utf8Value stack_trace(try_catch.StackTrace());
      if (stack_trace.length() > 0) {
      str <<  std::string(*stack_trace) << std::endl;
      }
      */
    }
    else {
      str << std::string( *error );
    }
    return ScriptResult( EMPTY_STRING, false, str.str() );

}
static void setExceptionAsReturnValue(const v8::FunctionCallbackInfo<v8::Value>& info, v8::Local<v8::Object> returnValue, v8::TryCatch& tryCatch)
{
    v8::Isolate* isolate = info.GetIsolate();
    returnValue->Set(v8::String::NewFromUtf8(isolate, "result"), tryCatch.Exception());
    returnValue->Set(v8::String::NewFromUtf8(isolate, "exceptionDetails"), JavaScriptCallFrame::createExceptionDetails(isolate, tryCatch.Message()));
    v8SetReturnValue(info, returnValue);
}
Ejemplo n.º 7
0
void CJavascriptException::ThrowIf(v8::Isolate *isolate, v8::TryCatch& try_catch)
{
  if (try_catch.HasCaught() && try_catch.CanContinue())
  {
    v8::HandleScope handle_scope(isolate);

    PyObject *type = NULL;
    v8::Handle<v8::Value> obj = try_catch.Exception();

    if (obj->IsObject())
    {
      v8::Handle<v8::Object> exc = obj->ToObject();
      v8::Handle<v8::String> name = v8::String::NewFromUtf8(isolate, "name");

      if (exc->Has(name))
      {
        v8::String::Utf8Value s(v8::Handle<v8::String>::Cast(exc->Get(name)));

        for (size_t i=0; i<_countof(SupportErrors); i++)
        {
          if (strnicmp(SupportErrors[i].name, *s, s.length()) == 0)
          {
            type = SupportErrors[i].type;
          }
        }
      }
    }

    throw CJavascriptException(isolate, try_catch, type);
  }
}
Ejemplo n.º 8
0
void Script::translateException(const v8::TryCatch &tryCatch, bool useStack) {
  v8::HandleScope handleScope;

  if (useStack && !tryCatch.StackTrace().IsEmpty())
    throw Exception(Value(tryCatch.StackTrace()).toString());

  if (tryCatch.Exception()->IsNull()) throw Exception("Interrupted");

  string msg = Value(tryCatch.Exception()).toString();

  v8::Handle<v8::Message> message = tryCatch.Message();
  if (message.IsEmpty()) throw Exception(msg);

  string filename = Value(message->GetScriptResourceName()).toString();
  int line = message->GetLineNumber();
  int col = message->GetStartColumn();

  throw Exception(msg, FileLocation(filename, line, col));
}
Ejemplo n.º 9
0
void CompiledScript::set_last_error(bool is_error, v8::TryCatch &try_catch)
{
    if (is_error)
    {
        Handle<Value> exception = try_catch.Exception();
        last_exception.Dispose();
        last_exception = v8::Persistent<v8::Value>::New(exception);
    }
    else
    {
        last_exception.Dispose();
        last_exception.Clear();
    }
}
Ejemplo n.º 10
0
std::string V8::ReportException(const v8::TryCatch& try_catch, bool suppressBacktrace) {
	v8::HandleScope handle_scope;
	std::string output = "";

	std::string exception_string = V8::StringToStdString(try_catch.Exception().As<String>());
	v8::Handle<v8::Message> message = try_catch.Message();

	if (suppressBacktrace || message.IsEmpty()) {

		// V8 didn't provide any extra information about this error; just
		// print the exception.
		output += exception_string;
		output += "\n";

	} else {

		output += V8::StringToStdString(
			message->GetScriptResourceName().As<String>()
		) + ":" + boost::lexical_cast<std::string>(
			message->GetLineNumber()
		) + "\n";

		output += exception_string + "\n";

		// Print line of source code.
		output += V8::StringToStdString(message->GetSourceLine()) + "\n";

		// Print wavy underline (GetUnderline is deprecated).
		int start = message->GetStartColumn();
		for (int i = 0; i < start; i++) {
			output += " ";
		}
		int end = message->GetEndColumn();
		for (int i = start; i < end; i++) {
			output += "^";
		}
		output += "\n";

		std::string stackTrace = V8::StringToStdString(try_catch.StackTrace().As<String>());
		if (stackTrace.length() > 0) {
			output += stackTrace + "\n";
		}
	}

	return output;
}
Ejemplo n.º 11
0
void reportException(ScriptState* scriptState, v8::TryCatch& exceptionCatcher)
{
    String errorMessage;
    int lineNumber = 0;
    String sourceURL;

    // There can be a situation that an exception is thrown without setting a message.
    v8::Local<v8::Message> message = exceptionCatcher.Message();
    if (message.IsEmpty())
        errorMessage = toWebCoreString(exceptionCatcher.Exception()->ToString());
    else {
        errorMessage = toWebCoreString(message->Get());
        lineNumber = message->GetLineNumber();
        sourceURL = toWebCoreString(message->GetScriptResourceName());
    }

    getScriptExecutionContext(scriptState)->reportException(errorMessage, lineNumber, sourceURL);
    exceptionCatcher.Reset();
}
Ejemplo n.º 12
0
std::string v8cffi_trace_back::prettyTraceBack(
  v8::Isolate *isolate,
  const v8::TryCatch &try_catch)
{
  std::string trace_back = "";

  std::string error_message = errorMessage(isolate, try_catch);

  if (!error_message.empty())
    trace_back += error_message + "\n";

  std::string stack_trace = stackTrace(try_catch);

  if (!stack_trace.empty())
    trace_back += stack_trace;
  else
    trace_back += v8cffi_utils::toCString(
      v8::String::Utf8Value(try_catch.Exception()));

  return trace_back;
}
Ejemplo n.º 13
0
const std::string CJavascriptException::Extract(v8::Isolate *isolate, v8::TryCatch& try_catch)
{
  assert(isolate->InContext());

  v8::HandleScope handle_scope(isolate);

  std::ostringstream oss;

  v8::String::Utf8Value msg(try_catch.Exception());

  if (*msg)
    oss << std::string(*msg, msg.length());

  v8::Handle<v8::Message> message = try_catch.Message();

  if (!message.IsEmpty())
  {
    oss << " ( ";

    if (!message->GetScriptResourceName().IsEmpty() &&
        !message->GetScriptResourceName()->IsUndefined())
    {
      v8::String::Utf8Value name(message->GetScriptResourceName());

      oss << std::string(*name, name.length());
    }

    oss << " @ " << message->GetLineNumber() << " : " << message->GetStartColumn() << " ) ";

    if (!message->GetSourceLine().IsEmpty() &&
        !message->GetSourceLine()->IsUndefined())
    {
      v8::String::Utf8Value line(message->GetSourceLine());

      oss << " -> " << std::string(*line, line.length());
    }
  }

  return oss.str();
}
Ejemplo n.º 14
0
void
ReportException(const v8::TryCatch& try_catch, OutT& out) {

  v8::HandleScope handle_scope;
  
  v8::String::Utf8Value exception(try_catch.Exception());
  const char* exception_string = ToCString(exception);
  v8::Handle<v8::Message> message = try_catch.Message();
  if (message.IsEmpty()) {
    // V8 didn't provide any extra information about this error; just
    // print the exception.
    out << exception_string << std::endl;
  } else {
    // Print (filename):(line number): (message).
    v8::String::Utf8Value filename(message->GetScriptResourceName());
    const char* filename_string = ToCString(filename);
    int linenum = message->GetLineNumber();
    out
      << filename_string
      << ":" << linenum
      << ": " << exception_string
      << std::endl;
    // Print line of source code.
    v8::String::Utf8Value sourceline(message->GetSourceLine());
    const char* sourceline_string = ToCString(sourceline);
    out << sourceline_string << std::endl;
    // Print wavy underline (GetUnderline is deprecated).
    int start = message->GetStartColumn();
    for (int i = 0; i < start; i++) {
      out << (" ");
    }
    int end = message->GetEndColumn();
    for (int i = start; i < end; i++) {
      out << ("^");
    }
    out << std::endl;
  }
}
Ejemplo n.º 15
0
/// @brief checks if a V8 exception has occurred and throws an appropriate C++
/// exception from it if so
void Executor::HandleV8Error(v8::TryCatch& tryCatch,
                             v8::Handle<v8::Value>& result,
                             arangodb::basics::StringBuffer* const buffer,
                             bool duringCompile) {
  ISOLATE;

  if (tryCatch.HasCaught()) {
    // caught a V8 exception
    if (!tryCatch.CanContinue()) {
      // request was canceled
      TRI_GET_GLOBALS();
      v8g->_canceled = true;

      THROW_ARANGO_EXCEPTION(TRI_ERROR_REQUEST_CANCELED);
    }

    // request was not canceled, but some other error occurred
    // peek into the exception
    if (tryCatch.Exception()->IsObject()) {
      // cast the exception to an object

      v8::Handle<v8::Array> objValue =
          v8::Handle<v8::Array>::Cast(tryCatch.Exception());
      v8::Handle<v8::String> errorNum = TRI_V8_ASCII_STRING("errorNum");
      v8::Handle<v8::String> errorMessage = TRI_V8_ASCII_STRING("errorMessage");

      TRI_Utf8ValueNFC stacktrace(TRI_UNKNOWN_MEM_ZONE, tryCatch.StackTrace());

      if (objValue->HasOwnProperty(errorNum) &&
          objValue->HasOwnProperty(errorMessage)) {
        v8::Handle<v8::Value> errorNumValue = objValue->Get(errorNum);
        v8::Handle<v8::Value> errorMessageValue = objValue->Get(errorMessage);

        // found something that looks like an ArangoError
        if ((errorNumValue->IsNumber() || errorNumValue->IsNumberObject()) &&
            (errorMessageValue->IsString() ||
             errorMessageValue->IsStringObject())) {
          int errorCode = static_cast<int>(TRI_ObjectToInt64(errorNumValue));
          std::string errorMessage(TRI_ObjectToString(errorMessageValue));

          if (*stacktrace && stacktrace.length() > 0) {
            errorMessage += "\nstacktrace of offending AQL function: ";
            errorMessage += *stacktrace;
          }

          THROW_ARANGO_EXCEPTION_MESSAGE(errorCode, errorMessage);
        }
      }

      // exception is no ArangoError
      std::string details(TRI_ObjectToString(tryCatch.Exception()));

      if (buffer) {
        std::string script(buffer->c_str(), buffer->length());
        LOG(ERR) << details << " " << script;
        details += "\nSee log for more details";
      }
      if (*stacktrace && stacktrace.length() > 0) {
        details += "\nstacktrace of offending AQL function: ";
        details += *stacktrace;
      }

      THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_QUERY_SCRIPT, details);
    }

    std::string msg("unknown error in scripting");
    if (duringCompile) {
      msg += " (during compilation)";
    }
    if (buffer) {
      std::string script(buffer->c_str(), buffer->length());
      LOG(ERR) << msg << " " << script;
      msg += " See log for details";
    }
    // we can't figure out what kind of error occurred and throw a generic error
    THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_INTERNAL, msg);
  }

  if (result.IsEmpty()) {
    std::string msg("unknown error in scripting");
    if (duringCompile) {
      msg += " (during compilation)";
    }
    if (buffer) {
      std::string script(buffer->c_str(), buffer->length());
      LOG(ERR) << msg << " " << script;
      msg += " See log for details";
    }

    THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_INTERNAL, msg);
  }

  // if we get here, no exception has been raised
}
Ejemplo n.º 16
0
static void reportException(v8::TryCatch &try_catch, bool show_line)
{
  using namespace v8;
  
  Handle<Message> message = try_catch.Message();

  v8::String::Utf8Value error(try_catch.Exception());
  if (error.length() > 0)
  {

    std::ostringstream errorMsg;

    /*
     02:50:22.863: [CID=00000000] JS: File undefined:12
    [CID=00000000] JS: Source Line   var notheetoo = nothere.subst(0, 10);
    [CID=00000000] JS:
                              ^
    [CID=00000000] JS:
    {TypeError: Cannot call method 'subst' of undefined
        at RouteProfile.isTellMeRoutable (unknown source)
        at RouteProfile.isRoutable (unknown source)
        at handle_request (unknown source)
    }
    */

    if (show_line && !message.IsEmpty())
    {
      // Print (filename):(line number): (message).
      String::Utf8Value filename(message->GetScriptResourceName());
      const char* filename_string = toCString(filename);
      int linenum = message->GetLineNumber();
      //fprintf(stderr, "%s:%i\n", filename_string, linenum);

      errorMsg << filename_string << ":" << linenum << std::endl;
      // Print line of source code.
      String::Utf8Value sourceline(message->GetSourceLine());
      const char* sourceline_string = toCString(sourceline);

      // HACK HACK HACK
      //
      // FIXME
      //
      // Because of how CommonJS modules work, all scripts are wrapped with a
      // "function (function (exports, __filename, ...) {"
      // to provide script local variables.
      //
      // When reporting errors on the first line of a script, this wrapper
      // function is leaked to the user. This HACK is to remove it. The length
      // of the wrapper is 62. That wrapper is defined in src/node.js
      //
      // If that wrapper is ever changed, then this number also has to be
      // updated. Or - someone could clean this up so that the two peices
      // don't need to be changed.
      //
      // Even better would be to get support into V8 for wrappers that
      // shouldn't be reported to users.
      int offset = linenum == 1 ? 62 : 0;

      //fprintf(stderr, "%s\n", sourceline_string + offset);
      errorMsg << sourceline_string + offset << std::endl;
      // Print wavy underline (GetUnderline is deprecated).
      int start = message->GetStartColumn();
      for (int i = offset; i < start; i++)
      {
        errorMsg << " ";
      }
      int end = message->GetEndColumn();
      for (int i = start; i < end; i++)
      {
        errorMsg << "^";
      }
      errorMsg << std::endl;
    }

    String::Utf8Value trace(try_catch.StackTrace());

    if (trace.length() > 0)
    {
      errorMsg << *trace;
    }
    OSS_LOG_ERROR("\t[CID=00000000] JS: " << *error << std::endl << "{" << std::endl << errorMsg.str() << std::endl << "}");
  }
}
Ejemplo n.º 17
0
static void ReportException(Environment* env, const v8::TryCatch& try_catch)
{
    ReportException(env, try_catch.Exception(), try_catch.Message());
}