Bug 1612007. Add convenience methods for throwing various spec-defined DOMExceptions on ErrorResult. r=smaug
authorBoris Zbarsky <bzbarsky@mit.edu>
Wed, 29 Jan 2020 09:09:11 +0000
changeset 512053 70615b052db52c0a7c4e9ff54aecb1b73d440f5f
parent 512052 1de45bb038fca66a8413838e3118f3b47559f995
child 512054 911bb8adb076ec3a56c2b8735bc4afd3fc24215f
push id37068
push usernerli@mozilla.com
push dateWed, 29 Jan 2020 15:51:04 +0000
treeherdermozilla-central@019ae805259f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1612007
milestone74.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 1612007. Add convenience methods for throwing various spec-defined DOMExceptions on ErrorResult. r=smaug Differential Revision: https://phabricator.services.mozilla.com/D61202
dom/base/DOMIntersectionObserver.cpp
dom/bindings/ErrorResult.h
dom/webidl/IntersectionObserver.webidl
--- a/dom/base/DOMIntersectionObserver.cpp
+++ b/dom/base/DOMIntersectionObserver.cpp
@@ -78,18 +78,17 @@ already_AddRefed<DOMIntersectionObserver
     return nullptr;
   }
   RefPtr<DOMIntersectionObserver> observer =
       new DOMIntersectionObserver(window.forget(), aCb);
 
   observer->mRoot = aOptions.mRoot;
 
   if (!observer->SetRootMargin(aOptions.mRootMargin)) {
-    aRv.ThrowDOMException(NS_ERROR_DOM_SYNTAX_ERR,
-                          "rootMargin must be specified in pixels or percent.");
+    aRv.ThrowSyntaxError("rootMargin must be specified in pixels or percent.");
     return nullptr;
   }
 
   if (aOptions.mThreshold.IsDoubleSequence()) {
     const Sequence<double>& thresholds =
         aOptions.mThreshold.GetAsDoubleSequence();
     observer->mThresholds.SetCapacity(thresholds.Length());
     for (const auto& thresh : thresholds) {
--- a/dom/bindings/ErrorResult.h
+++ b/dom/bindings/ErrorResult.h
@@ -328,16 +328,76 @@ class TErrorResult {
 
   // Same thing, but using a string literal.
   template <int N>
   void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
   ThrowDOMException(nsresult rv, const char (&aMessage)[N]) {
     ThrowDOMException(rv, nsLiteralCString(aMessage));
   }
 
+  // Facilities for throwing specific spec-defined DOMExceptions.
+#define DOMEXCEPTION(name, err)                                \
+  void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG Throw##name( \
+      const nsACString& aMessage) {                            \
+    ThrowDOMException(err, aMessage);                          \
+  }                                                            \
+                                                               \
+  template <int N>                                             \
+  void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG Throw##name( \
+      const char(&aMessage)[N]) {                              \
+    ThrowDOMException(err, aMessage);                          \
+  }
+
+  // XXXbz This list sort of duplicates the DOM4_MSG_DEF bits of domerr.msg,
+  // except that has various extra errors that are not in specs
+  // (e.g. InvalidPointerId) and has multiple definitions for the same error
+  // name using different messages, which we don't need because we get the
+  // message passed in.  We should try to convert all consumers of the "extra"
+  // error codes in there to these APIs, remove the extra bits, and just
+  // include domerr.msg here.
+  DOMEXCEPTION(IndexSizeError, NS_ERROR_DOM_INDEX_SIZE_ERR)
+  // We don't have a DOMStringSizeError and it's deprecated anyway.
+  DOMEXCEPTION(HierarchyRequestError, NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)
+  DOMEXCEPTION(WrongDocumentError, NS_ERROR_DOM_WRONG_DOCUMENT_ERR)
+  DOMEXCEPTION(InvalidCharacterError, NS_ERROR_DOM_INVALID_CHARACTER_ERR)
+  // We don't have a NoDataAllowedError and it's deprecated anyway.
+  DOMEXCEPTION(NoModificationAllowedError,
+               NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR)
+  DOMEXCEPTION(NotFoundError, NS_ERROR_DOM_NOT_FOUND_ERR)
+  DOMEXCEPTION(NotSupportedError, NS_ERROR_DOM_NOT_SUPPORTED_ERR)
+  DOMEXCEPTION(InUseAttributeError, NS_ERROR_DOM_INUSE_ATTRIBUTE_ERR)
+  DOMEXCEPTION(InvalidStateError, NS_ERROR_DOM_INVALID_STATE_ERR)
+  DOMEXCEPTION(SyntaxError, NS_ERROR_DOM_SYNTAX_ERR)
+  DOMEXCEPTION(InvalidModificationError, NS_ERROR_DOM_INVALID_MODIFICATION_ERR)
+  DOMEXCEPTION(NamespaceError, NS_ERROR_DOM_NAMESPACE_ERR)
+  DOMEXCEPTION(InvalidAccessError, NS_ERROR_DOM_INVALID_ACCESS_ERR)
+  // We don't have a ValidationError and it's deprecated anyway.
+  DOMEXCEPTION(TypeMismatchError, NS_ERROR_DOM_TYPE_MISMATCH_ERR)
+  DOMEXCEPTION(SecurityError, NS_ERROR_DOM_SECURITY_ERR)
+  DOMEXCEPTION(NetworkError, NS_ERROR_DOM_NETWORK_ERR)
+  DOMEXCEPTION(AbortError, NS_ERROR_DOM_ABORT_ERR)
+  DOMEXCEPTION(URLMismatchError, NS_ERROR_DOM_URL_MISMATCH_ERR)
+  DOMEXCEPTION(QuotaExceededError, NS_ERROR_DOM_QUOTA_EXCEEDED_ERR)
+  DOMEXCEPTION(TimeoutError, NS_ERROR_DOM_TIMEOUT_ERR)
+  DOMEXCEPTION(InvalidNodeTypeError, NS_ERROR_DOM_INVALID_NODE_TYPE_ERR)
+  DOMEXCEPTION(DataCloneError, NS_ERROR_DOM_DATA_CLONE_ERR)
+  DOMEXCEPTION(EncodingError, NS_ERROR_DOM_ENCODING_NOT_SUPPORTED_ERR)
+  DOMEXCEPTION(NotReadableError, NS_ERROR_DOM_FILE_NOT_READABLE_ERR)
+  DOMEXCEPTION(UnknownError, NS_ERROR_DOM_UNKNOWN_ERR)
+  DOMEXCEPTION(ConstraintError, NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR)
+  DOMEXCEPTION(DataError, NS_ERROR_DOM_DATA_ERR)
+  DOMEXCEPTION(TransactionInactiveError,
+               NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR)
+  DOMEXCEPTION(ReadOnlyError, NS_ERROR_DOM_INDEXEDDB_READ_ONLY_ERR)
+  DOMEXCEPTION(VersionError, NS_ERROR_DOM_INDEXEDDB_VERSION_ERR)
+  DOMEXCEPTION(OperationError, NS_ERROR_DOM_OPERATION_ERR)
+  DOMEXCEPTION(NotAllowedError, NS_ERROR_DOM_NOT_ALLOWED_ERR)
+
+#undef DOMEXCEPTION
+
   bool IsDOMException() const {
     return ErrorCode() == NS_ERROR_INTERNAL_ERRORRESULT_DOMEXCEPTION;
   }
 
   // Flag on the TErrorResult that whatever needs throwing has been
   // thrown on the JSContext already and we should not mess with it.
   // If nothing was thrown, this becomes an uncatchable exception.
   void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
--- a/dom/webidl/IntersectionObserver.webidl
+++ b/dom/webidl/IntersectionObserver.webidl
@@ -1,15 +1,15 @@
 /* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* 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/.
  *
  * The origin of this IDL file is
- * https://wicg.github.io/IntersectionObserver/
+ * https://w3c.github.io/IntersectionObserver/
  */
 
 [ProbablyShortLivingWrapper, Pref="dom.IntersectionObserver.enabled",
  Exposed=Window]
 interface IntersectionObserverEntry {
   [Constant]
   readonly attribute DOMHighResTimeStamp time;
   [Constant]