diff --git a/dom/base/DOMIntersectionObserver.cpp b/dom/base/DOMIntersectionObserver.cpp index 59e9a6d6ea53..b11ca68f1942 100644 --- a/dom/base/DOMIntersectionObserver.cpp +++ b/dom/base/DOMIntersectionObserver.cpp @@ -118,7 +118,7 @@ already_AddRefed DOMIntersectionObserver::Constructor( observer->mThresholds.SetCapacity(thresholds.Length()); for (const auto& thresh : thresholds) { if (thresh < 0.0 || thresh > 1.0) { - aRv.ThrowTypeError(); + aRv.ThrowRangeError(); return nullptr; } observer->mThresholds.AppendElement(thresh); @@ -127,7 +127,7 @@ already_AddRefed DOMIntersectionObserver::Constructor( } else { double thresh = aOptions.mThreshold.GetAsDouble(); if (thresh < 0.0 || thresh > 1.0) { - aRv.ThrowTypeError(); + aRv.ThrowRangeError(); return nullptr; } observer->mThresholds.AppendElement(thresh); diff --git a/dom/bindings/ErrorResult.h b/dom/bindings/ErrorResult.h index 58c0c59d3ce7..2540f83c86c7 100644 --- a/dom/bindings/ErrorResult.h +++ b/dom/bindings/ErrorResult.h @@ -31,6 +31,7 @@ #include #include "js/GCAnnotations.h" +#include "js/ErrorReport.h" #include "js/Value.h" #include "mozilla/Assertions.h" #include "mozilla/Attributes.h" @@ -76,6 +77,13 @@ bool constexpr ErrorFormatHasContext[] = { #undef MSG_DEF }; +// Table of the kinds of exceptions error messages will produce. +JSExnType constexpr ErrorExceptionType[] = { +#define MSG_DEF(_name, _argc, _has_context, _exn, _str) _exn, +#include "mozilla/dom/Errors.msg" +#undef MSG_DEF +}; + uint16_t GetErrorArgCount(const ErrNum aErrorNumber); namespace binding_detail { @@ -289,6 +297,8 @@ class TErrorResult { template void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG ThrowTypeError(Ts&&... messageArgs) { + static_assert(dom::ErrorExceptionType[errorNumber] == JSEXN_TYPEERR, + "Throwing a non-TypeError via ThrowTypeError"); ThrowErrorWithMessage(NS_ERROR_INTERNAL_ERRORRESULT_TYPEERROR, std::forward(messageArgs)...); } @@ -311,6 +321,8 @@ class TErrorResult { template void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG ThrowRangeError(Ts&&... messageArgs) { + static_assert(dom::ErrorExceptionType[errorNumber] == JSEXN_RANGEERR, + "Throwing a non-RangeError via ThrowRangeError"); ThrowErrorWithMessage(NS_ERROR_INTERNAL_ERRORRESULT_RANGEERROR, std::forward(messageArgs)...); }