From 481f410ee0a6cb6e7f03b93407d69e91c946190f Mon Sep 17 00:00:00 2001 From: Masatoshi Kimura Date: Wed, 11 Apr 2012 17:55:21 -0400 Subject: [PATCH] Bug 730161 - Implement IDBRequest.error. r=sicking --- dom/base/DOMError.cpp | 14 ++++++++++++++ dom/base/DOMError.h | 3 +++ dom/indexedDB/IDBRequest.cpp | 25 +++++++++++++++++-------- dom/indexedDB/IDBRequest.h | 10 ++-------- dom/indexedDB/OpenDatabaseHelper.cpp | 6 +++--- dom/indexedDB/nsIIDBRequest.idl | 13 +++++++------ 6 files changed, 46 insertions(+), 25 deletions(-) diff --git a/dom/base/DOMError.cpp b/dom/base/DOMError.cpp index 3af6181505e..b25ee545a83 100644 --- a/dom/base/DOMError.cpp +++ b/dom/base/DOMError.cpp @@ -8,6 +8,7 @@ #include "mozilla/Util.h" #include "nsDOMClassInfo.h" +#include "nsDOMException.h" using mozilla::ArrayLength; using mozilla::dom::DOMError; @@ -22,6 +23,19 @@ struct NameMap } // anonymous namespace +// static +already_AddRefed +DOMError::CreateForNSResult(nsresult aRv) +{ + const char* name; + const char* message; + aRv = NS_GetNameAndMessageForDOMNSResult(aRv, &name, &message); + if (NS_FAILED(aRv) || !name) { + return nsnull; + } + return CreateWithName(NS_ConvertASCIItoUTF16(name)); +} + // static already_AddRefed DOMError::CreateForDOMExceptionCode(PRUint16 aDOMExceptionCode) diff --git a/dom/base/DOMError.h b/dom/base/DOMError.h index e4058b0daee..22656bd8b85 100644 --- a/dom/base/DOMError.h +++ b/dom/base/DOMError.h @@ -23,6 +23,9 @@ public: NS_DECL_ISUPPORTS NS_DECL_NSIDOMDOMERROR + static already_AddRefed + CreateForNSResult(nsresult rv); + static already_AddRefed CreateForDOMExceptionCode(PRUint16 aDOMExceptionCode); diff --git a/dom/indexedDB/IDBRequest.cpp b/dom/indexedDB/IDBRequest.cpp index 80c02c2f111..c7a22468c7d 100644 --- a/dom/indexedDB/IDBRequest.cpp +++ b/dom/indexedDB/IDBRequest.cpp @@ -56,12 +56,12 @@ #include "AsyncConnectionHelper.h" #include "IDBEvents.h" #include "IDBTransaction.h" +#include "DOMError.h" USING_INDEXEDDB_NAMESPACE IDBRequest::IDBRequest() : mResultVal(JSVAL_VOID), - mErrorCode(0), mHaveResultOrErrorCode(false), mRooted(false) { @@ -100,7 +100,7 @@ IDBRequest::Reset() NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); mResultVal = JSVAL_VOID; mHaveResultOrErrorCode = false; - mErrorCode = 0; + mError = nsnull; UnrootResultVal(); } @@ -124,7 +124,7 @@ IDBRequest::NotifyHelperCompleted(HelperBase* aHelper) // If the request failed then set the error code and return. if (NS_FAILED(rv)) { - mErrorCode = NS_ERROR_GET_CODE(rv); + mError = DOMError::CreateForNSResult(rv); return NS_OK; } @@ -137,7 +137,7 @@ IDBRequest::NotifyHelperCompleted(HelperBase* aHelper) if (NS_FAILED(cxStack->GetSafeJSContext(&cx))) { NS_WARNING("Failed to get safe JSContext!"); rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR; - mErrorCode = NS_ERROR_GET_CODE(rv); + mError = DOMError::CreateForNSResult(rv); return rv; } } @@ -167,16 +167,25 @@ IDBRequest::NotifyHelperCompleted(HelperBase* aHelper) } if (NS_SUCCEEDED(rv)) { - mErrorCode = 0; + mError = nsnull; } else { - mErrorCode = NS_ERROR_GET_CODE(rv); + mError = DOMError::CreateForNSResult(rv); mResultVal = JSVAL_VOID; } return rv; } +void +IDBRequest::SetError(nsresult rv) +{ + NS_ASSERTION(NS_FAILED(rv), "Er, what?"); + NS_ASSERTION(!mError, "Already have an error?"); + + mError = DOMError::CreateForNSResult(rv); +} + void IDBRequest::RootResultValInternal() { @@ -239,7 +248,7 @@ IDBRequest::GetResult(jsval* aResult) } NS_IMETHODIMP -IDBRequest::GetErrorCode(PRUint16* aErrorCode) +IDBRequest::GetError(nsIDOMDOMError** aError) { NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); @@ -248,7 +257,7 @@ IDBRequest::GetErrorCode(PRUint16* aErrorCode) return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR; } - *aErrorCode = mErrorCode; + NS_IF_ADDREF(*aError = mError); return NS_OK; } diff --git a/dom/indexedDB/IDBRequest.h b/dom/indexedDB/IDBRequest.h index 5cc89608b26..55d3c92909e 100644 --- a/dom/indexedDB/IDBRequest.h +++ b/dom/indexedDB/IDBRequest.h @@ -82,13 +82,7 @@ public: nsresult NotifyHelperCompleted(HelperBase* aHelper); - void SetError(nsresult rv) - { - NS_ASSERTION(NS_FAILED(rv), "Er, what?"); - NS_ASSERTION(mErrorCode == NS_OK, "Already have an error?"); - - mErrorCode = rv; - } + void SetError(nsresult rv); protected: IDBRequest(); @@ -121,7 +115,7 @@ protected: jsval mResultVal; - PRUint16 mErrorCode; + nsCOMPtr mError; bool mHaveResultOrErrorCode; bool mRooted; }; diff --git a/dom/indexedDB/OpenDatabaseHelper.cpp b/dom/indexedDB/OpenDatabaseHelper.cpp index 155d5c7ea60..0ebbd9b26a5 100644 --- a/dom/indexedDB/OpenDatabaseHelper.cpp +++ b/dom/indexedDB/OpenDatabaseHelper.cpp @@ -2201,11 +2201,11 @@ OpenDatabaseHelper::DispatchErrorEvent() return; } - PRUint16 errorCode = 0; + nsCOMPtr error; DebugOnly rv = - mOpenDBRequest->GetErrorCode(&errorCode); + mOpenDBRequest->GetError(getter_AddRefs(error)); NS_ASSERTION(NS_SUCCEEDED(rv), "This shouldn't be failing at this point!"); - if (!errorCode) { + if (!error) { mOpenDBRequest->SetError(mResultCode); } diff --git a/dom/indexedDB/nsIIDBRequest.idl b/dom/indexedDB/nsIIDBRequest.idl index 5a3aa10f10e..243e43a1189 100644 --- a/dom/indexedDB/nsIIDBRequest.idl +++ b/dom/indexedDB/nsIIDBRequest.idl @@ -39,6 +39,7 @@ * ***** END LICENSE BLOCK ***** */ #include "nsISupports.idl" +#include "nsIDOMDOMError.idl" interface nsIDOMEventListener; interface nsIIDBTransaction; @@ -48,19 +49,19 @@ interface nsIIDBTransaction; * http://dev.w3.org/2006/webapi/WebSimpleDB/#idl-def-IDBRequest for more * information. */ -[scriptable, builtinclass, uuid(fe30ca60-bb90-4d68-af2f-4735f9228a54)] +[scriptable, builtinclass, uuid(4b9d901b-14a4-430c-b41b-5ecb238f4184)] interface nsIIDBRequest : nsISupports { - // "pending" or "done" - readonly attribute DOMString readyState; + readonly attribute jsval result; + + readonly attribute nsIDOMDOMError error; readonly attribute nsISupports source; readonly attribute nsIIDBTransaction transaction; - readonly attribute jsval result; - - readonly attribute unsigned short errorCode; + // "pending" or "done" + readonly attribute DOMString readyState; attribute nsIDOMEventListener onsuccess; attribute nsIDOMEventListener onerror;