Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Unified Diff: Source/bindings/core/v8/V8Initializer.cpp

Issue 625943002: Catch uncaught promise rejections from V8 and log to console. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: addressed Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/bindings/core/v8/V8Initializer.cpp
diff --git a/Source/bindings/core/v8/V8Initializer.cpp b/Source/bindings/core/v8/V8Initializer.cpp
index f73978540a245000a78a8a178ab3b90a6fb93a5a..9da208577385e3f145e2a7ab242aa938e33b1089 100644
--- a/Source/bindings/core/v8/V8Initializer.cpp
+++ b/Source/bindings/core/v8/V8Initializer.cpp
@@ -30,6 +30,8 @@
#include "bindings/core/v8/ScriptCallStackFactory.h"
#include "bindings/core/v8/ScriptController.h"
#include "bindings/core/v8/ScriptProfiler.h"
+#include "bindings/core/v8/ScriptValue.h"
+#include "bindings/core/v8/ScriptValueTraits.h"
#include "bindings/core/v8/V8Binding.h"
#include "bindings/core/v8/V8DOMException.h"
#include "bindings/core/v8/V8ErrorEvent.h"
@@ -41,10 +43,12 @@
#include "bindings/core/v8/V8Window.h"
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
+#include "core/dom/Microtask.h"
#include "core/frame/ConsoleTypes.h"
#include "core/frame/LocalDOMWindow.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/csp/ContentSecurityPolicy.h"
+#include "core/inspector/ScriptArguments.h"
#include "core/inspector/ScriptCallStack.h"
#include "platform/EventDispatchForbiddenScope.h"
#include "platform/TraceEvent.h"
@@ -149,9 +153,103 @@ static void messageHandlerInMainThread(v8::Handle<v8::Message> message, v8::Hand
// other isolated worlds (which means that the error events won't fire any event listeners
// in user's scripts).
EventDispatchForbiddenScope::AllowUserAgentEvents allowUserAgentEvents;
- enteredWindow->document()->reportException(event.release(), scriptId, callStack, corsStatus);
+ enteredWindow->document()->reportException(event.release(), scriptId, callStack, nullptr, corsStatus);
} else {
- enteredWindow->document()->reportException(event.release(), scriptId, callStack, corsStatus);
+ enteredWindow->document()->reportException(event.release(), scriptId, callStack, nullptr, corsStatus);
+ }
+}
+
+namespace {
+
+class PromiseRejectMessage {
+public:
+ PromiseRejectMessage(const ScriptValue& promise)
+ : m_promise(promise)
+ {
+ }
+
+ ScriptValue m_promise;
+};
+
+} // namespace
+
+typedef WillBeHeapHashMap<ScriptValue, OwnPtr<PromiseRejectMessage>, ScriptValueHash, ScriptValueHashTraits> PromiseRejectMessageMap;
+static bool firePromiseRejectMessagesCallbackScheduled = false;
+
+static PromiseRejectMessageMap& promiseRejectMessageQueue()
+{
+ DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<PromiseRejectMessageMap>, map, (adoptPtrWillBeNoop(new PromiseRejectMessageMap())));
+ return *map;
+}
+
+static void firePromiseRejectMessages()
pfeldman 2014/10/06 13:11:59 Lets kick it from WebKit upon end of task instead.
+{
+ ASSERT(isMainThread());
+
+ firePromiseRejectMessagesCallbackScheduled = false;
+ v8::Isolate::GetCurrent()->RemoveCallCompletedCallback(&firePromiseRejectMessages);
+
+ // FIXME: Send more data from V8.
+ int scriptId = 0;
+ AccessControlStatus corsStatus = SharableCrossOrigin;
+ RefPtrWillBeRawPtr<ScriptCallStack> callStack = nullptr;
+
+ PromiseRejectMessageMap& map = promiseRejectMessageQueue();
+ while (!map.isEmpty()) {
+ PromiseRejectMessageMap::iterator it = map.begin();
+ OwnPtr<PromiseRejectMessage> message = it->value.release();
+ map.remove(it);
+
+ ScriptState* scriptState = message->m_promise.scriptState();
+ if (scriptState->contextIsValid())
+ continue;
+ ScriptState::Scope scope(scriptState);
+
+ v8::Isolate* isolate = scriptState->isolate();
+ RefPtrWillBeRawPtr<ErrorEvent> event = ErrorEvent::createSanitizedError(&scriptState->world());
+ event->setUnsanitizedMessage("Unhandled promise rejection");
+
+ Vector<ScriptValue> args;
+ args.append(ScriptValue(scriptState, v8String(isolate, event->messageForConsole())));
+ args.append(message->m_promise);
+ RefPtrWillBeRawPtr<ScriptArguments> arguments = ScriptArguments::create(scriptState, args);
+
+ scriptState->executionContext()->reportException(event.release(), scriptId, callStack, arguments.release(), corsStatus, true);
pfeldman 2014/10/06 13:11:59 Accessing frame console for main thread and captur
+ }
+}
+
+static void promiseRejectHandlerInMainThread(v8::Handle<v8::Promise> promise, v8::Handle<v8::Value> value, v8::PromiseRejectEvent event)
+{
+ ASSERT(isMainThread());
+
+ // It's possible that promiseRejectHandlerInMainThread() is invoked while we're initializing a window.
+ // In that half-baked situation, we don't have a valid context nor a valid world,
+ // so just return immediately.
+ if (DOMWrapperWorld::windowIsBeingInitialized())
+ return;
+
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
+ ScriptState* scriptState = ScriptState::current(isolate);
+ ScriptValue promiseValue(scriptState, promise);
+ PromiseRejectMessageMap& map = promiseRejectMessageQueue();
+
+ if (event == v8::kPromiseRejectWithNoHandler) {
+ ASSERT(!map.contains(promiseValue));
+ map.set(promiseValue, adoptPtr(new PromiseRejectMessage(promiseValue)));
+ } else if (event == v8::kPromiseHandlerAddedAfterReject) {
+ PromiseRejectMessageMap::iterator it = map.find(promiseValue);
+ if (it != map.end()) {
+ map.remove(it);
+ } else {
+ // FIXME: Report revoke message to console.
+ }
+ } else {
+ ASSERT_NOT_REACHED();
+ }
+
+ if (!firePromiseRejectMessagesCallbackScheduled && !map.isEmpty()) {
+ firePromiseRejectMessagesCallbackScheduled = true;
+ isolate->AddCallCompletedCallback(&firePromiseRejectMessages);
}
}
@@ -218,6 +316,7 @@ void V8Initializer::initializeMainThreadIfNeeded()
v8::V8::SetAllowCodeGenerationFromStringsCallback(codeGenerationCheckCallbackInMainThread);
isolate->SetEventLogger(timerTraceProfilerInMainThread);
+ isolate->SetPromiseRejectCallback(promiseRejectHandlerInMainThread);
ScriptProfiler::initialize();
}
@@ -252,7 +351,7 @@ static void messageHandlerInWorker(v8::Handle<v8::Message> message, v8::Handle<v
// the error event from the v8::Message, quietly leave.
if (!v8::V8::IsExecutionTerminating(isolate)) {
V8ErrorHandler::storeExceptionOnErrorEventWrapper(event.get(), data, scriptState->context()->Global(), isolate);
- context->reportException(event.release(), scriptId, nullptr, corsStatus);
+ context->reportException(event.release(), scriptId, nullptr, nullptr, corsStatus);
}
}

Powered by Google App Engine
This is Rietveld 408576698