Bug 1107592 part 1. Implement a DOMException constructor. r=peterv
authorBoris Zbarsky <bzbarsky@mit.edu>
Fri, 02 Jan 2015 17:08:33 -0500
changeset 221912 fea8c83aef600f036ee901c2a7dc9d3e5f00557b
parent 221911 b12bf3c3d05a52c58f51aeb2e4d237046bdfa6b0
child 221913 0738d2b29e8bf59f60bfdb8f273bd8f730083e74
push id28050
push userphilringnalda@gmail.com
push dateSun, 04 Jan 2015 04:02:52 +0000
treeherdermozilla-central@55f3224d7513 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspeterv
bugs1107592
milestone37.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1107592 part 1. Implement a DOMException constructor. r=peterv
dom/base/DOMException.cpp
dom/base/DOMException.h
dom/base/test/test_DOMException.html
dom/webidl/DOMException.webidl
--- a/dom/base/DOMException.cpp
+++ b/dom/base/DOMException.cpp
@@ -671,16 +671,44 @@ DOMException::GetName(nsString& retval)
 }
 
 void
 DOMException::GetMessageMoz(nsString& retval)
 {
   CopyUTF8toUTF16(mMessage, retval);
 }
 
+already_AddRefed<DOMException>
+DOMException::Constructor(GlobalObject& /* unused */,
+                          const nsAString& aMessage,
+                          const Optional<nsAString>& aName,
+                          ErrorResult& aError)
+{
+  nsresult exceptionResult = NS_OK;
+  uint16_t exceptionCode = 0;
+  nsCString name(NS_LITERAL_CSTRING("Error"));
+
+  if (aName.WasPassed()) {
+    CopyUTF16toUTF8(aName.Value(), name);
+    for (uint32_t idx = 0; idx < ArrayLength(sDOMErrorMsgMap); idx++) {
+      if (name.EqualsASCII(sDOMErrorMsgMap[idx].mName)) {
+        exceptionResult = sDOMErrorMsgMap[idx].mNSResult;
+        exceptionCode = sDOMErrorMsgMap[idx].mCode;
+      }
+    }
+  }
+
+  nsRefPtr<DOMException> retval =
+    new DOMException(exceptionResult,
+                     NS_ConvertUTF16toUTF8(aMessage),
+                     name,
+                     exceptionCode);
+  return retval.forget();
+}
+
 JSObject*
 DOMException::WrapObject(JSContext* aCx)
 {
   return DOMExceptionBinding::Wrap(aCx, this);
 }
 
 /* static */already_AddRefed<DOMException>
 DOMException::Create(nsresult aRv)
--- a/dom/base/DOMException.h
+++ b/dom/base/DOMException.h
@@ -31,16 +31,18 @@ NS_GetNameAndMessageForDOMNSResult(nsres
                                    nsACString& aMessage,
                                    uint16_t* aCode = nullptr);
 
 namespace mozilla {
 class ErrorResult;
 
 namespace dom {
 
+class GlobalObject;
+
 #define MOZILLA_EXCEPTION_IID \
 { 0x55eda557, 0xeba0, 0x4fe3, \
   { 0xae, 0x2e, 0xf3, 0x94, 0x49, 0x23, 0x62, 0xd6 } }
 
 class Exception : public nsIXPCException,
                   public nsWrapperCache
 {
 public:
@@ -131,16 +133,22 @@ public:
 
   // nsIException overrides
   NS_IMETHOD ToString(nsACString& aReturn) MOZ_OVERRIDE;
 
   // nsWrapperCache overrides
   virtual JSObject* WrapObject(JSContext* aCx)
     MOZ_OVERRIDE;
 
+  static already_AddRefed<DOMException>
+  Constructor(GlobalObject& /* unused */,
+              const nsAString& aMessage,
+              const Optional<nsAString>& aName,
+              ErrorResult& aError);
+
   uint16_t Code() const {
     return mCode;
   }
 
   // Intentionally shadow the nsXPCException version.
   void GetMessageMoz(nsString& retval);
   void GetName(nsString& retval);
 
--- a/dom/base/test/test_DOMException.html
+++ b/dom/base/test/test_DOMException.html
@@ -39,12 +39,29 @@ var constants = [
   "INVALID_NODE_TYPE_ERR",
   "DATA_CLONE_ERR"
 ];
 for (var i = 0; i < constants.length; ++i) {
   var constant = constants[i];
   if (constant)
     is(DOMException[constant], i, constant)
 }
+
+var ex = new DOMException();
+ise(ex.name, "Error",
+    "Not passing a name should end up with 'Error' as the name");
+ise(ex.message, "",
+    "Not passing a message should end up with empty string as the message");
+
+ex = new DOMException("foo");
+ise(ex.name, "Error",
+    "Not passing a name should still end up with 'Error' as the name");
+ise(ex.message, "foo", "Should be using passed-in message");
+
+ex = new DOMException("bar", "NotSupportedError");
+ise(ex.name, "NotSupportedError", "Should be using the passed-in name");
+ise(ex.message, "bar", "Should still be using passed-in message");
+ise(ex.code, DOMException.NOT_SUPPORTED_ERR,
+    "Should have the right exception code");
 </script>
 </pre>
 </body>
 </html>
--- a/dom/webidl/DOMException.webidl
+++ b/dom/webidl/DOMException.webidl
@@ -69,17 +69,18 @@ interface Exception {
   stringifier;
 };
 
 Exception implements ExceptionMembers;
 
 // XXXkhuey this is an 'exception', not an interface, but we don't have any
 // parser or codegen mechanisms for dealing with exceptions.
 [ExceptionClass,
- Exposed=(Window, Worker)]
+ Exposed=(Window, Worker),
+ Constructor(optional DOMString message = "", optional DOMString name)]
 interface DOMException {
   const unsigned short INDEX_SIZE_ERR = 1;
   const unsigned short DOMSTRING_SIZE_ERR = 2; // historical
   const unsigned short HIERARCHY_REQUEST_ERR = 3;
   const unsigned short WRONG_DOCUMENT_ERR = 4;
   const unsigned short INVALID_CHARACTER_ERR = 5;
   const unsigned short NO_DATA_ALLOWED_ERR = 6; // historical
   const unsigned short NO_MODIFICATION_ALLOWED_ERR = 7;