зеркало из https://github.com/mozilla/pjs.git
-- Allow the DOM script object factory to turn XPConnect return values into proper nsIException objects.
-- Make nsDOMClassInfo::ThrowJSException never throw null as an exception. -- Make XPCException::ToString grab the description of the result as well as the name. r+sr=jst
This commit is contained in:
Родитель
7ab19fd9bd
Коммит
82cf1531d4
|
@ -1433,25 +1433,46 @@ nsDOMClassInfo::WrapNative(JSContext *cx, JSObject *scope, nsISupports *native,
|
|||
nsresult
|
||||
nsDOMClassInfo::ThrowJSException(JSContext *cx, nsresult aResult)
|
||||
{
|
||||
nsCOMPtr<nsIExceptionService> xs =
|
||||
do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
|
||||
if (!xs)
|
||||
return NS_ERROR_FAILURE;
|
||||
do {
|
||||
nsCOMPtr<nsIExceptionService> xs =
|
||||
do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
|
||||
if (!xs) {
|
||||
break;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIExceptionManager> xm;
|
||||
nsresult rv = xs->GetCurrentExceptionManager(getter_AddRefs(xm));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
nsCOMPtr<nsIExceptionManager> xm;
|
||||
nsresult rv = xs->GetCurrentExceptionManager(getter_AddRefs(xm));
|
||||
if (NS_FAILED(rv)) {
|
||||
break;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIException> exception;
|
||||
rv = xm->GetExceptionFromProvider(aResult, 0, getter_AddRefs(exception));
|
||||
nsCOMPtr<nsIException> exception;
|
||||
rv = xm->GetExceptionFromProvider(aResult, 0, getter_AddRefs(exception));
|
||||
if (NS_FAILED(rv) || !exception) {
|
||||
break;
|
||||
}
|
||||
|
||||
jsval jv;
|
||||
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
|
||||
rv = WrapNative(cx, ::JS_GetGlobalObject(cx), exception,
|
||||
NS_GET_IID(nsIException), &jv, getter_AddRefs(holder));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
JS_SetPendingException(cx, jv);
|
||||
jsval jv;
|
||||
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
|
||||
rv = WrapNative(cx, ::JS_GetGlobalObject(cx), exception,
|
||||
NS_GET_IID(nsIException), &jv, getter_AddRefs(holder));
|
||||
if (NS_FAILED(rv) || JSVAL_IS_NULL(jv)) {
|
||||
break;
|
||||
}
|
||||
JS_SetPendingException(cx, jv);
|
||||
|
||||
return NS_OK;
|
||||
} while (0);
|
||||
|
||||
// XXX This probably wants to be localized, but that can fail in ways that
|
||||
// are hard to report correctly.
|
||||
JSString *str =
|
||||
JS_NewStringCopyZ(cx, "An error occured throwing an exception");
|
||||
if (!str) {
|
||||
// JS_NewStringCopyZ reported the error for us.
|
||||
return NS_OK;
|
||||
}
|
||||
JS_SetPendingException(cx, STRING_TO_JSVAL(str));
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim: set ts=2 sw=2 et tw=78:
|
||||
*
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
|
@ -51,6 +52,7 @@
|
|||
*/
|
||||
|
||||
#include "nsDOMScriptObjectFactory.h"
|
||||
#include "xpcexception.h"
|
||||
#include "nsScriptNameSpaceManager.h"
|
||||
#include "nsIObserverService.h"
|
||||
#include "nsJSEnvironment.h"
|
||||
|
@ -82,6 +84,7 @@ nsDOMScriptObjectFactory::nsDOMScriptObjectFactory()
|
|||
xs->RegisterExceptionProvider(this, NS_ERROR_MODULE_SVG);
|
||||
#endif
|
||||
xs->RegisterExceptionProvider(this, NS_ERROR_MODULE_DOM_XPATH);
|
||||
xs->RegisterExceptionProvider(this, NS_ERROR_MODULE_XPCONNECT);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -193,12 +196,29 @@ nsDOMScriptObjectFactory::Observe(nsISupports *aSubject,
|
|||
xs->UnregisterExceptionProvider(this, NS_ERROR_MODULE_SVG);
|
||||
#endif
|
||||
xs->UnregisterExceptionProvider(this, NS_ERROR_MODULE_DOM_XPATH);
|
||||
xs->UnregisterExceptionProvider(this, NS_ERROR_MODULE_XPCONNECT);
|
||||
}
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
static nsresult
|
||||
CreateXPConnectException(nsresult aResult, nsIException *aDefaultException,
|
||||
nsIException **_retval)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
nsCOMPtr<nsIXPCException> exception(
|
||||
do_CreateInstance("@mozilla.org/js/xpc/Exception;1", &rv));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
rv = exception->Initialize(nsnull, aResult, nsnull, nsnull, nsnull, nsnull);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
NS_ADDREF(*_retval = exception);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMScriptObjectFactory::GetException(nsresult result,
|
||||
nsIException *aDefaultException,
|
||||
|
@ -214,6 +234,8 @@ nsDOMScriptObjectFactory::GetException(nsresult result,
|
|||
#endif
|
||||
case NS_ERROR_MODULE_DOM_XPATH:
|
||||
return NS_NewXPathException(result, aDefaultException, _retval);
|
||||
case NS_ERROR_MODULE_XPCONNECT:
|
||||
return CreateXPConnectException(result, aDefaultException, _retval);
|
||||
default:
|
||||
return NS_NewDOMException(result, aDefaultException, _retval);
|
||||
}
|
||||
|
|
|
@ -367,12 +367,17 @@ nsXPCException::ToString(char **_retval)
|
|||
return rv;
|
||||
}
|
||||
|
||||
const char* msg = mMessage ? mMessage : defaultMsg;
|
||||
const char* msg = mMessage ? mMessage : nsnull;
|
||||
const char* location = indicatedLocation ?
|
||||
indicatedLocation : defaultLocation;
|
||||
const char* resultName = mName;
|
||||
if(!resultName && !NameAndFormatForNSResult(mResult, &resultName, nsnull))
|
||||
if(!resultName && !NameAndFormatForNSResult(mResult, &resultName,
|
||||
(!msg) ? &msg : nsnull))
|
||||
{
|
||||
if(!msg)
|
||||
msg = defaultMsg;
|
||||
resultName = "<unknown>";
|
||||
}
|
||||
const char* data = mData ? "yes" : "no";
|
||||
|
||||
char* temp = JS_smprintf(format, msg, mResult, resultName, location, data);
|
||||
|
|
Загрузка…
Ссылка в новой задаче