2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCRTGlue.h"
|
2012-04-11 14:55:23 -07:00
|
|
|
#include "nsContentUtils.h"
|
2011-10-03 12:11:31 -07:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsDOMError.h"
|
|
|
|
#include "nsDOMException.h"
|
|
|
|
#include "nsIDOMDOMException.h"
|
2012-04-11 14:55:23 -07:00
|
|
|
#include "nsIDocument.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "prprf.h"
|
|
|
|
|
2012-04-11 14:55:21 -07:00
|
|
|
enum DOM4ErrorTypeCodeMap {
|
|
|
|
/* DOM4 errors from http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#domexception */
|
|
|
|
IndexSizeError = nsIDOMDOMException::INDEX_SIZE_ERR,
|
|
|
|
HierarchyRequestError = nsIDOMDOMException::HIERARCHY_REQUEST_ERR,
|
|
|
|
WrongDocumentError = nsIDOMDOMException::WRONG_DOCUMENT_ERR,
|
|
|
|
InvalidCharacterError = nsIDOMDOMException::INVALID_CHARACTER_ERR,
|
|
|
|
NoModificationAllowedError = nsIDOMDOMException::NO_MODIFICATION_ALLOWED_ERR,
|
|
|
|
NotFoundError = nsIDOMDOMException::NOT_FOUND_ERR,
|
|
|
|
NotSupportedError = nsIDOMDOMException::NOT_SUPPORTED_ERR,
|
|
|
|
// Can't remove until setNamedItem is removed
|
|
|
|
InUseAttributeError = nsIDOMDOMException::INUSE_ATTRIBUTE_ERR,
|
|
|
|
InvalidStateError = nsIDOMDOMException::INVALID_STATE_ERR,
|
|
|
|
SyntaxError = nsIDOMDOMException::SYNTAX_ERR,
|
|
|
|
InvalidModificationError = nsIDOMDOMException::INVALID_MODIFICATION_ERR,
|
|
|
|
NamespaceError = nsIDOMDOMException::NAMESPACE_ERR,
|
|
|
|
InvalidAccessError = nsIDOMDOMException::INVALID_ACCESS_ERR,
|
|
|
|
TypeMismatchError = nsIDOMDOMException::TYPE_MISMATCH_ERR,
|
|
|
|
SecurityError = nsIDOMDOMException::SECURITY_ERR,
|
|
|
|
NetworkError = nsIDOMDOMException::NETWORK_ERR,
|
|
|
|
AbortError = nsIDOMDOMException::ABORT_ERR,
|
|
|
|
URLMismatchError = nsIDOMDOMException::URL_MISMATCH_ERR,
|
|
|
|
QuotaExceededError = nsIDOMDOMException::QUOTA_EXCEEDED_ERR,
|
|
|
|
TimeoutError = nsIDOMDOMException::TIMEOUT_ERR,
|
|
|
|
InvalidNodeTypeError = nsIDOMDOMException::INVALID_NODE_TYPE_ERR,
|
|
|
|
DataCloneError = nsIDOMDOMException::DATA_CLONE_ERR,
|
|
|
|
|
2012-06-13 18:12:37 -07:00
|
|
|
/* XXX Should be JavaScript native errors */
|
2012-04-11 14:55:21 -07:00
|
|
|
TypeError = 0,
|
2012-06-13 18:12:37 -07:00
|
|
|
RangeError = 0,
|
2012-04-11 14:55:21 -07:00
|
|
|
|
|
|
|
/* IndexedDB errors http://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#exceptions */
|
|
|
|
UnknownError = 0,
|
|
|
|
ConstraintError = 0,
|
|
|
|
DataError = 0,
|
|
|
|
TransactionInactiveError = 0,
|
|
|
|
ReadOnlyError = 0,
|
|
|
|
VersionError = 0,
|
|
|
|
|
|
|
|
/* File API errors http://dev.w3.org/2006/webapi/FileAPI/#ErrorAndException */
|
|
|
|
NotReadableError = 0,
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
/* FileHandle API errors */
|
|
|
|
LockedFileInactiveError = 0,
|
2012-04-11 14:55:21 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#define DOM4_MSG_DEF(name, message, nsresult) {(nsresult), name, #name, message},
|
|
|
|
#define DOM_MSG_DEF(val, message) {(val), NS_ERROR_GET_CODE(val), #val, message},
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static struct ResultStruct
|
|
|
|
{
|
|
|
|
nsresult mNSResult;
|
2012-04-11 14:55:21 -07:00
|
|
|
PRUint16 mCode;
|
2007-03-22 10:30:00 -07:00
|
|
|
const char* mName;
|
|
|
|
const char* mMessage;
|
|
|
|
} gDOMErrorMsgMap[] = {
|
|
|
|
#include "domerr.msg"
|
2012-04-11 14:55:21 -07:00
|
|
|
{0, 0, nsnull, nsnull} // sentinel to mark end of array
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2012-04-11 14:55:21 -07:00
|
|
|
#undef DOM4_MSG_DEF
|
2007-03-22 10:30:00 -07:00
|
|
|
#undef DOM_MSG_DEF
|
|
|
|
|
|
|
|
static void
|
|
|
|
NSResultToNameAndMessage(nsresult aNSResult,
|
|
|
|
const char** aName,
|
2012-04-11 14:55:21 -07:00
|
|
|
const char** aMessage,
|
|
|
|
PRUint16* aCode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-18 12:02:27 -07:00
|
|
|
*aName = nsnull;
|
|
|
|
*aMessage = nsnull;
|
2012-04-11 14:55:21 -07:00
|
|
|
*aCode = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
ResultStruct* result_struct = gDOMErrorMsgMap;
|
|
|
|
|
|
|
|
while (result_struct->mName) {
|
|
|
|
if (aNSResult == result_struct->mNSResult) {
|
|
|
|
*aName = result_struct->mName;
|
|
|
|
*aMessage = result_struct->mMessage;
|
2012-04-11 14:55:21 -07:00
|
|
|
*aCode = result_struct->mCode;
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
++result_struct;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("Huh, someone is throwing non-DOM errors using the DOM module!");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
|
|
|
NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, const char** aName,
|
2012-04-24 16:50:00 -07:00
|
|
|
const char** aMessage, PRUint16* aCode)
|
2010-11-10 15:25:44 -08:00
|
|
|
{
|
|
|
|
const char* name = nsnull;
|
|
|
|
const char* message = nsnull;
|
2012-04-11 14:55:21 -07:00
|
|
|
PRUint16 code = 0;
|
|
|
|
NSResultToNameAndMessage(aNSResult, &name, &message, &code);
|
2010-11-10 15:25:44 -08:00
|
|
|
|
|
|
|
if (name && message) {
|
|
|
|
*aName = name;
|
|
|
|
*aMessage = message;
|
2012-04-24 16:50:00 -07:00
|
|
|
if (aCode) {
|
|
|
|
*aCode = code;
|
|
|
|
}
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2012-06-13 18:12:37 -07:00
|
|
|
|
|
|
|
class nsDOMException : public nsIException,
|
|
|
|
public nsIDOMDOMException
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsDOMException() {}
|
|
|
|
virtual ~nsDOMException() {}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIEXCEPTION
|
|
|
|
NS_IMETHOD Init(nsresult aNSResult, const char* aName,
|
|
|
|
const char* aMessage, PRUint16 aCode,
|
|
|
|
nsIException* aDefaultException);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_NSIDOMDOMEXCEPTION
|
2012-06-13 18:12:37 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
const char* mName;
|
|
|
|
const char* mMessage;
|
|
|
|
nsCOMPtr<nsIException> mInner;
|
|
|
|
nsresult mResult;
|
|
|
|
PRUint16 mCode;
|
|
|
|
};
|
|
|
|
|
|
|
|
DOMCI_DATA(DOMException, nsDOMException)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsDOMException)
|
|
|
|
NS_IMPL_RELEASE(nsDOMException)
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsDOMException)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIException)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMDOMException)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIException)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(DOMException)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewDOMException(nsresult aNSResult, nsIException* aDefaultException,
|
|
|
|
nsIException** aException)
|
|
|
|
{
|
|
|
|
const char* name;
|
|
|
|
const char* message;
|
|
|
|
PRUint16 code;
|
|
|
|
NSResultToNameAndMessage(aNSResult, &name, &message, &code);
|
|
|
|
nsDOMException* inst = new nsDOMException();
|
|
|
|
inst->Init(aNSResult, name, message, code, aDefaultException);
|
|
|
|
*aException = inst;
|
|
|
|
NS_ADDREF(*aException);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-02-22 16:34:14 -08:00
|
|
|
nsDOMException::GetCode(PRUint16* aCode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aCode);
|
2012-04-11 14:55:21 -07:00
|
|
|
*aCode = mCode;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-13 18:12:37 -07:00
|
|
|
// Warn only when the code was changed (other than DOM Core)
|
2012-04-11 14:55:23 -07:00
|
|
|
// or the code is useless (zero)
|
2012-06-13 18:12:37 -07:00
|
|
|
if (NS_ERROR_GET_MODULE(mResult) != NS_ERROR_MODULE_DOM || !mCode) {
|
2012-04-11 14:55:23 -07:00
|
|
|
nsCOMPtr<nsIDocument> doc =
|
|
|
|
do_QueryInterface(nsContentUtils::GetDocumentFromCaller());
|
|
|
|
if (doc) {
|
|
|
|
doc->WarnOnceAbout(nsIDocument::eDOMExceptionCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-13 18:12:37 -07:00
|
|
|
nsDOMException::GetMessageMoz(char **aMessage)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mMessage) {
|
|
|
|
*aMessage = NS_strdup(mMessage);
|
|
|
|
} else {
|
|
|
|
*aMessage = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-13 18:12:37 -07:00
|
|
|
nsDOMException::GetResult(PRUint32* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
|
|
|
|
|
|
|
*aResult = mResult;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-13 18:12:37 -07:00
|
|
|
nsDOMException::GetName(char **aName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aName);
|
|
|
|
|
|
|
|
if (mName) {
|
|
|
|
*aName = NS_strdup(mName);
|
|
|
|
} else {
|
|
|
|
*aName = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-13 18:12:37 -07:00
|
|
|
nsDOMException::GetFilename(char **aFilename)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mInner) {
|
|
|
|
return mInner->GetFilename(aFilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aFilename);
|
|
|
|
|
|
|
|
*aFilename = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-13 18:12:37 -07:00
|
|
|
nsDOMException::GetLineNumber(PRUint32 *aLineNumber)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mInner) {
|
|
|
|
return mInner->GetLineNumber(aLineNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aLineNumber);
|
|
|
|
|
|
|
|
*aLineNumber = 0;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-13 18:12:37 -07:00
|
|
|
nsDOMException::GetColumnNumber(PRUint32 *aColumnNumber)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mInner) {
|
|
|
|
return mInner->GetColumnNumber(aColumnNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aColumnNumber);
|
|
|
|
|
|
|
|
*aColumnNumber = 0;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-13 18:12:37 -07:00
|
|
|
nsDOMException::GetLocation(nsIStackFrame **aLocation)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mInner) {
|
|
|
|
return mInner->GetLocation(aLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aLocation);
|
|
|
|
|
|
|
|
*aLocation = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-13 18:12:37 -07:00
|
|
|
nsDOMException::GetInner(nsIException **aInner)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aInner);
|
|
|
|
|
|
|
|
*aInner = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-13 18:12:37 -07:00
|
|
|
nsDOMException::GetData(nsISupports **aData)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mInner) {
|
|
|
|
return mInner->GetData(aData);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aData);
|
|
|
|
|
|
|
|
*aData = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-13 18:12:37 -07:00
|
|
|
nsDOMException::ToString(char **aReturn)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aReturn = nsnull;
|
|
|
|
|
|
|
|
static const char defaultMsg[] = "<no message>";
|
|
|
|
static const char defaultLocation[] = "<unknown>";
|
|
|
|
static const char defaultName[] = "<unknown>";
|
|
|
|
static const char format[] =
|
|
|
|
"[Exception... \"%s\" code: \"%d\" nsresult: \"0x%x (%s)\" location: \"%s\"]";
|
|
|
|
|
|
|
|
nsCAutoString location;
|
|
|
|
|
|
|
|
if (mInner) {
|
|
|
|
nsXPIDLCString filename;
|
|
|
|
|
|
|
|
mInner->GetFilename(getter_Copies(filename));
|
|
|
|
|
|
|
|
if (!filename.IsEmpty()) {
|
|
|
|
PRUint32 line_nr = 0;
|
|
|
|
|
|
|
|
mInner->GetLineNumber(&line_nr);
|
|
|
|
|
|
|
|
char *temp = PR_smprintf("%s Line: %d", filename.get(), line_nr);
|
|
|
|
if (temp) {
|
|
|
|
location.Assign(temp);
|
|
|
|
PR_smprintf_free(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (location.IsEmpty()) {
|
|
|
|
location = defaultLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* msg = mMessage ? mMessage : defaultMsg;
|
|
|
|
const char* resultName = mName ? mName : defaultName;
|
|
|
|
|
2012-04-11 14:55:21 -07:00
|
|
|
*aReturn = PR_smprintf(format, msg, mCode, mResult, resultName,
|
2007-03-22 10:30:00 -07:00
|
|
|
location.get());
|
|
|
|
|
|
|
|
return *aReturn ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-13 18:12:37 -07:00
|
|
|
nsDOMException::Init(nsresult aNSResult, const char* aName,
|
|
|
|
const char* aMessage, PRUint16 aCode,
|
|
|
|
nsIException* aDefaultException)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mResult = aNSResult;
|
|
|
|
mName = aName;
|
|
|
|
mMessage = aMessage;
|
2012-04-11 14:55:21 -07:00
|
|
|
mCode = aCode;
|
2007-03-22 10:30:00 -07:00
|
|
|
mInner = aDefaultException;
|
|
|
|
return NS_OK;
|
|
|
|
}
|