Bug 1739143 - Capture DOM L10n initial translation and mutations rejections and report to console. r=nordzilla

Differential Revision: https://phabricator.services.mozilla.com/D130797
This commit is contained in:
Zibi Braniecki 2021-11-10 23:40:43 +00:00
Родитель 539896f064
Коммит 1aa25b7d0b
5 изменённых файлов: 118 добавлений и 48 удалений

Просмотреть файл

@ -11,6 +11,8 @@
#include "mozilla/dom/DocumentL10nBinding.h"
#include "mozilla/Telemetry.h"
using namespace mozilla;
using namespace mozilla::intl;
using namespace mozilla::dom;
NS_IMPL_CYCLE_COLLECTION_CLASS(DocumentL10n)
@ -74,7 +76,25 @@ class L10nReadyHandler final : public PromiseNativeHandler {
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
mDocumentL10n->InitialTranslationCompleted(false);
mPromise->MaybeRejectWithUndefined();
nsTArray<nsCString> errors{
"[dom/l10n] Could not complete initial document translation."_ns,
};
IgnoredErrorResult rv;
MaybeReportErrorsToGecko(errors, rv, mDocumentL10n->GetParentObject());
/**
* We resolve the mReady here even if we encountered failures, because
* there is nothing actionable for the user pending on `mReady` to do here
* and we don't want to incentivized consumers of this API to plan the
* same pending operation for resolve and reject scenario.
*
* Additionally, without it, the stderr received "uncaught promise
* rejection" warning, which is noisy and not-actionable.
*
* So instead, we just resolve and report errors.
*/
mPromise->MaybeResolveWithUndefined();
}
private:

Просмотреть файл

@ -7,7 +7,10 @@
#include "L10nMutations.h"
#include "mozilla/dom/DocumentInlines.h"
#include "nsRefreshDriver.h"
#include "mozilla/intl/Localization.h"
using namespace mozilla;
using namespace mozilla::intl;
using namespace mozilla::dom;
NS_IMPL_CYCLE_COLLECTION_CLASS(L10nMutations)
@ -124,6 +127,44 @@ void L10nMutations::WillRefresh(mozilla::TimeStamp aTime) {
FlushPendingTranslations();
}
/**
* The handler for the `TranslateElements` promise used to turn
* a potential rejection into a console warning.
**/
class L10nMutationFinalizationHandler final : public PromiseNativeHandler {
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(L10nMutationFinalizationHandler)
explicit L10nMutationFinalizationHandler(nsIGlobalObject* aGlobal)
: mGlobal(aGlobal) {}
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
}
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
nsTArray<nsCString> errors{
"[dom/l10n] Errors during l10n mutation frame."_ns,
};
IgnoredErrorResult rv;
MaybeReportErrorsToGecko(errors, rv, mGlobal);
}
private:
~L10nMutationFinalizationHandler() = default;
nsCOMPtr<nsIGlobalObject> mGlobal;
};
NS_IMPL_CYCLE_COLLECTION(L10nMutationFinalizationHandler, mGlobal)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(L10nMutationFinalizationHandler)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(L10nMutationFinalizationHandler)
NS_IMPL_CYCLE_COLLECTING_RELEASE(L10nMutationFinalizationHandler)
void L10nMutations::FlushPendingTranslations() {
if (!mDOMLocalization) {
return;
@ -147,6 +188,10 @@ void L10nMutations::FlushPendingTranslations() {
mPendingElements.Clear();
RefPtr<Promise> promise = mDOMLocalization->TranslateElements(elements, rv);
RefPtr<PromiseNativeHandler> l10nMutationFinalizationHandler =
new L10nMutationFinalizationHandler(mDOMLocalization->GetParentObject());
promise->AppendNativeHandler(l10nMutationFinalizationHandler);
}
void L10nMutations::Disconnect() {

Просмотреть файл

@ -11,11 +11,13 @@
SimpleTest.waitForExplicitFinish();
document.addEventListener("DOMContentLoaded", async function() {
/**
* Even when we fail to localize all elements, we will
* still resolve the `ready` promise to communicate that
* the initial translation phase is now completed.
*/
document.l10n.ready.then(() => {
is(1, 2, "the ready should not resolve");
SimpleTest.finish();
}, (err) => {
is(1, 1, "the ready should reject");
is(1, 1, "the ready should resolve");
SimpleTest.finish();
});
});

Просмотреть файл

@ -5,7 +5,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Localization.h"
#include "nsContentUtils.h"
#include "nsIObserverService.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/Preferences.h"
@ -21,48 +20,6 @@ using namespace mozilla::intl;
static const char* kObservedPrefs[] = {L10N_PSEUDO_PREF, nullptr};
// The state where the application contains incomplete localization resources
// is much more common than for other types of core resources.
//
// In result, we our localization is designed to handle missing resources
// gracefully, and we need a more fine-tuned way to communicate those problems
// to developers.
//
// In particular, we want developers and early adopters to be able to reason
// about missing translations, without bothering end user in production, where
// the user cannot react to that.
//
// We currently differentiate between nightly/dev-edition builds or automation
// where we report the errors, and beta/release, where we silence them.
static bool MaybeReportErrorsToGecko(const nsTArray<nsCString>& aErrors,
ErrorResult& aRv,
nsIGlobalObject* global) {
if (!aErrors.IsEmpty()) {
if (xpc::IsInAutomation()) {
aRv.ThrowInvalidStateError(aErrors.ElementAt(0));
return true;
}
#if defined(NIGHTLY_BUILD) || defined(MOZ_DEV_EDITION) || defined(DEBUG)
Document* doc = nullptr;
if (global) {
nsPIDOMWindowInner* innerWindow = global->AsInnerWindow();
if (innerWindow) {
doc = innerWindow->GetExtantDoc();
}
}
for (const auto& error : aErrors) {
nsContentUtils::ReportToConsoleNonLocalized(NS_ConvertUTF8toUTF16(error),
nsIScriptError::warningFlag,
"l10n"_ns, doc);
}
#endif
}
return false;
}
static nsTArray<ffi::L10nKey> ConvertFromL10nKeys(
const Sequence<OwningUTF8StringOrL10nIdArgs>& aKeys) {
nsTArray<ffi::L10nKey> l10nKeys(aKeys.Length());

Просмотреть файл

@ -12,6 +12,9 @@
#include "nsWeakReference.h"
#include "nsWrapperCache.h"
#include "nsWeakReference.h"
#include "nsIScriptError.h"
#include "nsContentUtils.h"
#include "nsPIDOMWindow.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/BindingDeclarations.h"
@ -22,6 +25,49 @@
namespace mozilla {
namespace intl {
// The state where the application contains incomplete localization resources
// is much more common than for other types of core resources.
//
// In result, our localization is designed to handle missing resources
// gracefully, and we need a more fine-tuned way to communicate those problems
// to developers.
//
// In particular, we want developers and early adopters to be able to reason
// about missing translations, without bothering end user in production, where
// the user cannot react to that.
//
// We currently differentiate between nightly/dev-edition builds or automation
// where we report the errors, and beta/release, where we silence them.
[[maybe_unused]] static bool MaybeReportErrorsToGecko(
const nsTArray<nsCString>& aErrors, ErrorResult& aRv,
nsIGlobalObject* aGlobal) {
if (!aErrors.IsEmpty()) {
if (xpc::IsInAutomation()) {
aRv.ThrowInvalidStateError(aErrors.ElementAt(0));
return true;
}
#if defined(NIGHTLY_BUILD) || defined(MOZ_DEV_EDITION) || defined(DEBUG)
dom::Document* doc = nullptr;
if (aGlobal) {
nsPIDOMWindowInner* innerWindow = aGlobal->AsInnerWindow();
if (innerWindow) {
doc = innerWindow->GetExtantDoc();
}
}
for (const auto& error : aErrors) {
nsContentUtils::ReportToConsoleNonLocalized(NS_ConvertUTF8toUTF16(error),
nsIScriptError::warningFlag,
"l10n"_ns, doc);
printf_stderr("%s\n", error.get());
}
#endif
}
return false;
}
class Localization : public nsIObserver,
public nsWrapperCache,
public nsSupportsWeakReference {