diff --git a/mozilla/docshell/base/nsDocShell.cpp b/mozilla/docshell/base/nsDocShell.cpp index 7fc6aad75e2..018a4b0173b 100644 --- a/mozilla/docshell/base/nsDocShell.cpp +++ b/mozilla/docshell/base/nsDocShell.cpp @@ -50,6 +50,11 @@ #include "nsIGlobalHistory.h" #include "nsIHTTPChannel.h" +// For reporting errors with the console service. +// These can go away if error reporting is propagated up past nsDocShell. +#include "nsIConsoleService.h" +#include "nsIScriptError.h" + #ifdef XXX_NS_DEBUG // XXX: we'll need a logging facility for debugging #define WEB_TRACE(_bit,_args) \ PR_BEGIN_MACRO \ @@ -1919,7 +1924,54 @@ NS_IMETHODIMP nsDocShell::GetScriptGlobalObject(nsIScriptGlobalObject** aGlobal) NS_IMETHODIMP nsDocShell::ReportScriptError(const char* aErrorString, const char* aFileName, PRInt32 aLineNo, const char* aLineBuf) { - //XXX Needs some international work. + nsresult rv; + + // Get the console service, where we're going to register the error. + nsCOMPtr consoleService + (do_GetService("mozilla.consoleservice.1")); + + // Make an nsIScriptError, populate it with information from this + // error, then log it with the console service. The UI can then + // poll the service to update the JavaScript console. + nsCOMPtr + errorObject(do_CreateInstance("mozilla.scripterror.1")); + + if (consoleService != nsnull && errorObject != nsnull) + { + // Mock up wide strings until we fix the interface. + nsAutoString message(aErrorString); + PRUnichar *msgUni = message.ToNewUnicode(); + nsAutoString filename(aFileName); + PRUnichar *fileUni = filename.ToNewUnicode(); + nsAutoString sourceline(aLineBuf); + PRUnichar *slUni = sourceline.ToNewUnicode(); + + // make category depend on xul/!xul + const char *category; + category = mItemType == typeContent + ? "XUL javascript" + : "content javascript"; + + rv = errorObject->Init(msgUni, fileUni, slUni, aLineNo, 0, 0, category); + + nsAllocator::Free(msgUni); + nsAllocator::Free(fileUni); + nsAllocator::Free(slUni); + + if (NS_SUCCEEDED(rv)) + { + rv = consoleService->LogMessage(errorObject); + if (NS_SUCCEEDED(rv)) + { +#ifndef DEBUG + return NS_OK; +#endif + } + } + } + + // If reporting via the console service fails for some reason, fall + // back to printing to stdout. nsAutoString error; error.Assign("JavaScript Error: "); error.Append(aErrorString); @@ -1946,66 +1998,11 @@ NS_IMETHODIMP nsDocShell::ReportScriptError(const char* aErrorString, error += "\n"; } - PRBool showAlert; - if(mItemType == typeContent) - { - // Include a message for the beta release suggesting remedy - error += - "\n" - "If you visit this page with another browser and don't see a similar " - "error, it may indicate that the site has not yet been updated to " - "support standards implemented by the Mozilla browser such as the " - "W3C Document Object Model (DOM). Please see " - "http://developer.netscape.com/mozilla/ for more information."; - showAlert = PR_TRUE; - } - else - { - // for non-DEBUG builds, tuck xul errors under the rug, and - // only show those originating from content. -#ifdef DEBUG - showAlert = PR_TRUE; -#else - showAlert = PR_FALSE; -#endif - } - - // Disable error alerts unless pref is set. - PRBool alertPref; - if (showAlert == PR_TRUE - && mPrefs != nsnull - && (NS_SUCCEEDED(mPrefs->GetBoolPref("javascript.error.alerts", - &alertPref))) - && (alertPref == PR_TRUE)) - { - showAlert = PR_TRUE; - } - else - { - showAlert = PR_FALSE; - } - - if(showAlert) - { - // Show an alert for the error. At some point, we may have a JavaScript - // console to show errors in. - nsCOMPtr prompt = do_GetInterface(mTreeOwner); - if(prompt) - { - prompt->Alert(error.GetUnicode()); -#ifndef DEBUG - return NS_OK; -#endif - } - } - - // else if not showing alert or failed to get prompt interface or - // this is a debug build and we want to printf ALL errors... char* errorStr = error.ToNewCString(); if(errorStr) { - printf("%s\n", errorStr); - nsCRT::free(errorStr); + fprintf(stderr, "%s\n", errorStr); + nsAllocator::Free(errorStr); } return NS_OK; diff --git a/mozilla/js/src/xpconnect/loader/mozJSComponentLoader.cpp b/mozilla/js/src/xpconnect/loader/mozJSComponentLoader.cpp index 9b0c2fbc1a2..39fd3bb6ba6 100644 --- a/mozilla/js/src/xpconnect/loader/mozJSComponentLoader.cpp +++ b/mozilla/js/src/xpconnect/loader/mozJSComponentLoader.cpp @@ -35,6 +35,10 @@ #include "nsXPIDLString.h" #include "nsIScriptSecurityManager.h" +// For reporting errors with the console service +#include "nsIScriptError.h" +#include "nsIConsoleService.h" + const char mozJSComponentLoaderProgID[] = "moz.jsloader.1"; const char jsComponentTypeName[] = "text/javascript"; @@ -187,8 +191,53 @@ mozJSComponentLoader::GetFactory(const nsIID &aCID, static void Reporter(JSContext *cx, const char *message, JSErrorReport *rep) { - fprintf(stderr, "JS Component Loader: ERROR %s:%d\n" - " %s\n", rep->filename, rep->lineno, + nsresult rv; + + /* Use the console service to register the error. */ + nsCOMPtr consoleService + (do_GetService("mozilla.consoleservice.1")); + + /* + * Make an nsIScriptError, populate it with information from this + * error, then log it with the console service. The UI can then + * poll the service to update the JavaScript console. + */ + nsCOMPtr + errorObject(do_CreateInstance("mozilla.scripterror.1")); + + if (consoleService != nsnull && errorObject != nsnull) { + /* + * Got an error object; prepare appropriate-width versions of + * various arguments to it. + */ + nsAutoString fileUni(rep->filename); + const PRUnichar *newFileUni = fileUni.ToNewUnicode(); + + PRUint32 column = rep->uctokenptr - rep->uclinebuf; + + rv = errorObject->Init(rep->ucmessage, newFileUni, rep->uclinebuf, + rep->lineno, column, rep->flags, + "component javascript"); + nsAllocator::Free((void *)newFileUni); + if (NS_SUCCEEDED(rv)) { + rv = consoleService->LogMessage(errorObject); + if (NS_SUCCEEDED(rv)) { + // We're done! +#ifndef DEBUG + return; +#endif + } + } + } + + /* + * If any of the above fails for some reason, fall back to + * printing to stderr. + */ + fprintf(stderr, "JS Component Loader: %s %s:%d\n" + " %s\n", + JSREPORT_IS_WARNING(rep->flags) ? "WARNING" : "ERROR", + rep->filename, rep->lineno, message ? message : ""); }