From 3b5f53a397bfea895b72635bc5aff2ec29ef95aa Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Mon, 15 Jul 2013 11:44:49 -0700 Subject: [PATCH] Bug 889911 - Introduce xpc::SystemErrorReporter, roughly based on mozJSComponentLoader's error reporter. r=mrbkap --- js/xpconnect/src/nsXPConnect.cpp | 54 ++++++++++++++++++++++++++++++++ js/xpconnect/src/xpcpublic.h | 6 ++++ 2 files changed, 60 insertions(+) diff --git a/js/xpconnect/src/nsXPConnect.cpp b/js/xpconnect/src/nsXPConnect.cpp index 73c79acfeb9..3a065c8cd9c 100644 --- a/js/xpconnect/src/nsXPConnect.cpp +++ b/js/xpconnect/src/nsXPConnect.cpp @@ -222,8 +222,62 @@ nsXPConnect::IsISupportsDescendant(nsIInterfaceInfo* info) return found; } +NS_EXPORT_(void) +xpc::SystemErrorReporter(JSContext *cx, const char *message, JSErrorReport *rep) +{ + // It would be nice to assert !JS_DescribeScriptedCaller here, to be sure + // that there isn't any script running that could catch the exception. But + // the JS engine invokes the error reporter directly if someone reports an + // ErrorReport that it doesn't know how to turn into an exception. Arguably + // it should just learn how to throw everything. But either way, if the + // exception is ending here, it's not going to get propagated to a caller, + // so it's up to us to make it known. + + nsresult rv; + + /* Use the console service to register the error. */ + nsCOMPtr consoleService = + do_GetService(NS_CONSOLESERVICE_CONTRACTID); + + /* + * Make an nsIScriptError, populate it with information from this + * error, then log it with the console service. + */ + nsCOMPtr errorObject = + do_CreateInstance(NS_SCRIPTERROR_CONTRACTID); + + if (consoleService && errorObject) { + uint32_t column = rep->uctokenptr - rep->uclinebuf; + + const PRUnichar* ucmessage = + static_cast(rep->ucmessage); + const PRUnichar* uclinebuf = + static_cast(rep->uclinebuf); + + rv = errorObject->Init( + ucmessage ? nsDependentString(ucmessage) : EmptyString(), + NS_ConvertASCIItoUTF16(rep->filename), + uclinebuf ? nsDependentString(uclinebuf) : EmptyString(), + rep->lineno, column, rep->flags, + "system javascript"); + if (NS_SUCCEEDED(rv)) + consoleService->LogMessage(errorObject); + } + + /* Log to stderr in debug builds. */ +#ifdef DEBUG + fprintf(stderr, "System JS : %s %s:%d\n" + " %s\n", + JSREPORT_IS_WARNING(rep->flags) ? "WARNING" : "ERROR", + rep->filename, rep->lineno, + message ? message : ""); +#endif + +} + /***************************************************************************/ + nsresult nsXPConnect::GetInfoForIID(const nsIID * aIID, nsIInterfaceInfo** info) { diff --git a/js/xpconnect/src/xpcpublic.h b/js/xpconnect/src/xpcpublic.h index c3fb2e251af..c7d40620408 100644 --- a/js/xpconnect/src/xpcpublic.h +++ b/js/xpconnect/src/xpcpublic.h @@ -413,6 +413,12 @@ GetNativeForGlobal(JSObject *global); */ JSObject * GetJunkScope(); + +// Error reporter used when there is no associated DOM window on to which to +// report errors and warnings. +NS_EXPORT_(void) +SystemErrorReporter(JSContext *cx, const char *message, JSErrorReport *rep); + } // namespace xpc namespace mozilla {