示例#1
0
static void V8ErrorMessageCallback(v8::Handle<v8::Message> message,
v8::Handle<v8::Value> data)
{
  v8::HandleScope handle_scope;
  std::string error =
          + " Javascript error on line " + OSS::string_from_number(message->GetLineNumber())
          + " : " + toString(message->GetSourceLine());
  OSS::log_error(error);
}
示例#2
0
static void AppendExceptionLine(Environment* env, v8::Handle<v8::Value> er, v8::Handle<v8::Message> message)
{
    if (message.IsEmpty())
        return;

    v8::HandleScope scope(env->isolate());
    v8::Local<v8::Object> err_obj;
    if (!er.IsEmpty() && er->IsObject()) {
        err_obj = er.As<v8::Object>();

        // Do it only once per message
        if (!err_obj->GetHiddenValue(env->processed_string()).IsEmpty())
            return;
        err_obj->SetHiddenValue(env->processed_string(), True(env->isolate()));
    }

    static char arrow[1024];

    // Print (filename):(line number): (message).
    v8::String::Utf8Value filename(message->GetScriptResourceName());
    const char* filename_string = *filename;
    int linenum = message->GetLineNumber();
    // Print line of source code.
    v8::String::Utf8Value sourceline(message->GetSourceLine());
    const char* sourceline_string = *sourceline;

    // Because of how node modules work, all scripts are wrapped with a
    // "function (module, 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. There used to be a hack here to
    // truncate off the first 62 characters, but it caused numerous other
    // problems when vm.runIn*Context() methods were used for non-module
    // code.
    //
    // If we ever decide to re-instate such a hack, the following steps
    // must be taken:
    //
    // 1. Pass a flag around to say "this code was wrapped"
    // 2. Update the stack frame output so that it is also correct.
    //
    // It would probably be simpler to add a line rather than add some
    // number of characters to the first line, since V8 truncates the
    // sourceline to 78 characters, and we end up not providing very much
    // useful debugging info to the user if we remove 62 characters.

    int start = message->GetStartColumn();
    int end = message->GetEndColumn();

    int off = snprintf(arrow, sizeof(arrow), "%s:%i\n%s\n", filename_string, linenum, sourceline_string);
    assert(off >= 0);

    // Print wavy underline (GetUnderline is deprecated).
    for (int i = 0; i < start; i++) {
        if (sourceline_string[i] == '\0' || static_cast<size_t>(off) >= sizeof(arrow)) {
            break;
        }
        assert(static_cast<size_t>(off) < sizeof(arrow));
        arrow[off++] = (sourceline_string[i] == '\t') ? '\t' : ' ';
    }
    for (int i = start; i < end; i++) {
        if (sourceline_string[i] == '\0' || static_cast<size_t>(off) >= sizeof(arrow)) {
            break;
        }
        assert(static_cast<size_t>(off) < sizeof(arrow));
        arrow[off++] = '^';
    }
    assert(static_cast<size_t>(off - 1) <= sizeof(arrow) - 1);
    arrow[off++] = '\n';
    arrow[off] = '\0';

    v8::Local<v8::String> arrow_str = v8::String::NewFromUtf8(env->isolate(), arrow);
    v8::Local<v8::Value> msg;
    v8::Local<v8::Value> stack;

    // Allocation failed, just print it out
    if (arrow_str.IsEmpty() || err_obj.IsEmpty() || !err_obj->IsNativeError())
        goto print;

    msg = err_obj->Get(env->message_string());
    stack = err_obj->Get(env->stack_string());

    if (msg.IsEmpty() || stack.IsEmpty())
        goto print;

    err_obj->Set(env->message_string(), v8::String::Concat(arrow_str, msg->ToString()));
    err_obj->Set(env->stack_string(), v8::String::Concat(arrow_str, stack->ToString()));
    return;

    print: if (env->printed_error())
        return;
    env->set_printed_error(true);
//  uv_tty_reset_mode();
    fprintf(stderr, "\n%s", arrow);
}