зеркало из https://github.com/mozilla/pjs.git
Modify nsIScriptGlobalObjectOwner::ReportScriptError to take an nsIScriptError interface, rather than separate message, file, line arguments.
Fix implementations and their callers, and restore error printing to stderr even when the error is successfully logged to the console service, to make life easier for people who go to the trouble to invoke Mozilla with -console.
This commit is contained in:
Родитель
576a9dec53
Коммит
4537a69b81
|
@ -41,6 +41,8 @@
|
|||
#include "nsString.h"
|
||||
#include "nsVoidArray.h"
|
||||
#include "nsXULElement.h"
|
||||
#include "nsIConsoleService.h"
|
||||
#include "nsIScriptError.h"
|
||||
|
||||
class nsXULPDGlobalObject : public nsIScriptObjectOwner,
|
||||
public nsIScriptGlobalObject,
|
||||
|
@ -369,34 +371,27 @@ nsXULPrototypeDocument::GetScriptGlobalObject(nsIScriptGlobalObject** _result)
|
|||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsXULPrototypeDocument::ReportScriptError(const char* aErrorString,
|
||||
const char* aFileName,
|
||||
PRInt32 aLineNo,
|
||||
const char* aLineBuf)
|
||||
nsXULPrototypeDocument::ReportScriptError(nsIScriptError *errorObject)
|
||||
{
|
||||
// XXX Eventually replace this with something that shows a dialog
|
||||
// box or similar fanciness.
|
||||
nsAutoString error = "JavaScript Error: ";
|
||||
error += aErrorString;
|
||||
error += "\n";
|
||||
nsresult rv;
|
||||
|
||||
if (aFileName) {
|
||||
error += "URL: ";
|
||||
error += aFileName;
|
||||
}
|
||||
if (errorObject == nsnull)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
|
||||
if (aLineNo) {
|
||||
error += ", line ";
|
||||
error += aLineNo;
|
||||
}
|
||||
// Get the console service, where we're going to register the error.
|
||||
nsCOMPtr<nsIConsoleService> consoleService
|
||||
(do_GetService("mozilla.consoleservice.1"));
|
||||
|
||||
if (aLineBuf) {
|
||||
error += "\n";
|
||||
error += aLineBuf;
|
||||
}
|
||||
|
||||
printf("%s\n", (const char*) nsCAutoString(error));
|
||||
return NS_OK;
|
||||
if (consoleService != nsnull) {
|
||||
rv = consoleService->LogMessage(errorObject);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
return NS_OK;
|
||||
} else {
|
||||
return rv;
|
||||
}
|
||||
} else {
|
||||
return NS_ERROR_NOT_AVAILABLE;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
|
|
@ -40,7 +40,7 @@
|
|||
#include "nsIWebBrowserChrome.h"
|
||||
#include "nsPoint.h"
|
||||
#include "nsGfxCIID.h"
|
||||
#include "nsIPrompt.h" // as long as ReportScriptError raises an alert box.
|
||||
#include "nsIPrompt.h"
|
||||
|
||||
// Local Includes
|
||||
#include "nsDocShell.h"
|
||||
|
@ -1954,91 +1954,33 @@ NS_IMETHODIMP nsDocShell::GetScriptGlobalObject(nsIScriptGlobalObject** aGlobal)
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsDocShell::ReportScriptError(const char* aErrorString,
|
||||
const char* aFileName, PRInt32 aLineNo, const char* aLineBuf)
|
||||
NS_IMETHODIMP nsDocShell::ReportScriptError(nsIScriptError *errorObject)
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
if (errorObject == nsnull)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
|
||||
// Get the console service, where we're going to register the error.
|
||||
nsCOMPtr<nsIConsoleService> 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<nsIScriptError>
|
||||
errorObject(do_CreateInstance("mozilla.scripterror.1"));
|
||||
|
||||
if (consoleService != nsnull && errorObject != nsnull)
|
||||
if (consoleService != 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);
|
||||
|
||||
rv = consoleService->LogMessage(errorObject);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
{
|
||||
rv = consoleService->LogMessage(errorObject);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
{
|
||||
#ifndef DEBUG
|
||||
return NS_OK;
|
||||
#endif
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
error += "\n";
|
||||
|
||||
if(aFileName)
|
||||
else
|
||||
{
|
||||
error += "URL: ";
|
||||
error += aFileName;
|
||||
error += "\n";
|
||||
return NS_ERROR_NOT_AVAILABLE;
|
||||
}
|
||||
|
||||
if(aLineNo)
|
||||
{
|
||||
error += "Line number: ";
|
||||
error.Append(aLineNo, 10);
|
||||
error += "\n";
|
||||
}
|
||||
|
||||
if(aLineBuf)
|
||||
{
|
||||
error += "Line text: ";
|
||||
error += aLineBuf;
|
||||
error += "\n";
|
||||
}
|
||||
|
||||
char* errorStr = error.ToNewCString();
|
||||
if(errorStr)
|
||||
{
|
||||
fprintf(stderr, "%s\n", errorStr);
|
||||
nsAllocator::Free(errorStr);
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
*/
|
||||
|
||||
interface nsIScriptGlobalObject;
|
||||
interface nsIScriptError;
|
||||
|
||||
[scriptable, uuid(413E8400-A87F-11d3-AFC6-00A024FFC08C)]
|
||||
interface nsIScriptGlobalObjectOwner : nsISupports
|
||||
|
@ -48,6 +49,5 @@ interface nsIScriptGlobalObjectOwner : nsISupports
|
|||
* occurred while a script was being evaluted.
|
||||
*
|
||||
*/
|
||||
void reportScriptError(in string aErrorString, in string aFileName,
|
||||
in long aLineNo, in string aLineBuf);
|
||||
void reportScriptError(in nsIScriptError aError);
|
||||
};
|
||||
|
|
|
@ -64,6 +64,8 @@ static NS_DEFINE_IID(kPrefServiceCID, NS_PREF_CID);
|
|||
static PRLogModuleInfo* gJSDiagnostics = nsnull;
|
||||
#endif
|
||||
|
||||
#include "nsIScriptError.h"
|
||||
|
||||
void PR_CALLBACK
|
||||
NS_ScriptErrorReporter(JSContext *cx,
|
||||
const char *message,
|
||||
|
@ -83,27 +85,64 @@ NS_ScriptErrorReporter(JSContext *cx,
|
|||
NS_WARN_IF_FALSE(PR_FALSE, "Failed to get a global Object Owner");
|
||||
return;
|
||||
}
|
||||
|
||||
const char* error;
|
||||
if (message) {
|
||||
error = message;
|
||||
|
||||
// Make an nsIScriptError and populate it with information from
|
||||
// this error.
|
||||
nsCOMPtr<nsIScriptError>
|
||||
errorObject(do_CreateInstance("mozilla.scripterror.1"));
|
||||
|
||||
// XXX possible here to distinguish between XUL and content js?
|
||||
// or could just expose setCategory and twiddle it later.
|
||||
const char *category = "XUL/Content JavaScript";
|
||||
|
||||
if (errorObject == nsnull) {
|
||||
return NS_ERROR_NOT_AVAILABLE;
|
||||
}
|
||||
else {
|
||||
error = "<unknown>";
|
||||
}
|
||||
|
||||
if(report) {
|
||||
owner->ReportScriptError(error,
|
||||
report->filename,
|
||||
report->lineno,
|
||||
report->linebuf);
|
||||
}
|
||||
else {
|
||||
owner->ReportScriptError(error, nsnull, 0, nsnull);
|
||||
nsresult rv = NS_ERROR_FAILURE;
|
||||
if (report) {
|
||||
nsAutoString fileUni;
|
||||
fileUni.AssignWithConversion(report->filename);
|
||||
const PRUnichar *newFileUni = fileUni.ToNewUnicode();
|
||||
PRUint32 column = report->uctokenptr - report->uclinebuf;
|
||||
rv = errorObject->Init(report->ucmessage, newFileUni,
|
||||
report->uclinebuf, report->lineno,
|
||||
column, report->flags, category);
|
||||
nsAllocator::Free((void *)newFileUni);
|
||||
} else if (message) {
|
||||
nsAutoString messageUni;
|
||||
messageUni.AssignWithConversion(message);
|
||||
const PRUnichar *newMessageUni = messageUni.ToNewUnicode();
|
||||
rv = errorObject->Init(newMessageUni, nsnull, nsnull,
|
||||
0, 0, 0, category);
|
||||
nsAllocator::Free((void *)newMessageUni);
|
||||
}
|
||||
|
||||
if (NS_SUCCEEDED(rv))
|
||||
owner->ReportScriptError(errorObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Print it to stderr as well, for the benefit of those invoking
|
||||
// mozilla with -console.
|
||||
nsAutoString error;
|
||||
error.Assign("JavaScript ");
|
||||
error.Append(JSREPORT_IS_WARNING(report->flags) ? "warning: " : "error: ");
|
||||
error += "\n";
|
||||
error.Append(report->filename);
|
||||
error.Append(" line ");
|
||||
error.Append(report->lineno, 10);
|
||||
error.Append(": ");
|
||||
error.Append(report->ucmessage);
|
||||
error += "\n";
|
||||
|
||||
char *errorStr = error.ToNewCString();
|
||||
if (errorStr) {
|
||||
fprintf(stderr, "%s\n", errorStr);
|
||||
nsAllocator::Free(errorStr);
|
||||
}
|
||||
|
||||
#ifdef PR_LOGGING
|
||||
if (report) {
|
||||
if (!gJSDiagnostics)
|
||||
|
|
|
@ -41,6 +41,8 @@
|
|||
#include "nsString.h"
|
||||
#include "nsVoidArray.h"
|
||||
#include "nsXULElement.h"
|
||||
#include "nsIConsoleService.h"
|
||||
#include "nsIScriptError.h"
|
||||
|
||||
class nsXULPDGlobalObject : public nsIScriptObjectOwner,
|
||||
public nsIScriptGlobalObject,
|
||||
|
@ -369,34 +371,27 @@ nsXULPrototypeDocument::GetScriptGlobalObject(nsIScriptGlobalObject** _result)
|
|||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsXULPrototypeDocument::ReportScriptError(const char* aErrorString,
|
||||
const char* aFileName,
|
||||
PRInt32 aLineNo,
|
||||
const char* aLineBuf)
|
||||
nsXULPrototypeDocument::ReportScriptError(nsIScriptError *errorObject)
|
||||
{
|
||||
// XXX Eventually replace this with something that shows a dialog
|
||||
// box or similar fanciness.
|
||||
nsAutoString error = "JavaScript Error: ";
|
||||
error += aErrorString;
|
||||
error += "\n";
|
||||
nsresult rv;
|
||||
|
||||
if (aFileName) {
|
||||
error += "URL: ";
|
||||
error += aFileName;
|
||||
}
|
||||
if (errorObject == nsnull)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
|
||||
if (aLineNo) {
|
||||
error += ", line ";
|
||||
error += aLineNo;
|
||||
}
|
||||
// Get the console service, where we're going to register the error.
|
||||
nsCOMPtr<nsIConsoleService> consoleService
|
||||
(do_GetService("mozilla.consoleservice.1"));
|
||||
|
||||
if (aLineBuf) {
|
||||
error += "\n";
|
||||
error += aLineBuf;
|
||||
}
|
||||
|
||||
printf("%s\n", (const char*) nsCAutoString(error));
|
||||
return NS_OK;
|
||||
if (consoleService != nsnull) {
|
||||
rv = consoleService->LogMessage(errorObject);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
return NS_OK;
|
||||
} else {
|
||||
return rv;
|
||||
}
|
||||
} else {
|
||||
return NS_ERROR_NOT_AVAILABLE;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
|
Загрузка…
Ссылка в новой задаче