Bug 1483036 - Report meaningful Promise values from FluentDOM C++ bits r=Gijs,smaug

Differential Revision: https://phabricator.services.mozilla.com/D24113

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Zibi Braniecki 2019-03-26 19:34:27 +00:00
Родитель 4450c1692a
Коммит bb5e52fa01
3 изменённых файлов: 43 добавлений и 35 удалений

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

@ -2816,24 +2816,13 @@ class LocalizationHandler : public PromiseNativeHandler {
}
}
}
JS::RootedObject sourceScope(aCx, JS::CurrentGlobalOrNull(aCx));
AutoEntryScript aes(mReturnValuePromise->GetParentObject(),
"Promise resolution");
JSContext* cx = aes.cx();
JS::Rooted<JS::Value> result(cx, JS::ObjectValue(*untranslatedElements));
xpc::StackScopedCloneOptions options;
options.wrapReflectors = true;
StackScopedClone(cx, options, sourceScope, &result);
mReturnValuePromise->MaybeResolve(result);
JS::Rooted<JS::Value> result(aCx, JS::ObjectValue(*untranslatedElements));
mReturnValuePromise->MaybeResolveWithClone(aCx, result);
}
virtual void RejectedCallback(JSContext* aCx,
JS::Handle<JS::Value> aValue) override {
mReturnValuePromise->MaybeRejectWithUndefined();
mReturnValuePromise->MaybeRejectWithClone(aCx, aValue);
}
private:

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

@ -369,12 +369,12 @@ class L10nReadyHandler final : public PromiseNativeHandler {
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
mDocumentL10n->InitialDocumentTranslationCompleted();
mPromise->MaybeResolveWithUndefined();
mPromise->MaybeResolveWithClone(aCx, aValue);
}
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
mDocumentL10n->InitialDocumentTranslationCompleted();
mPromise->MaybeRejectWithUndefined();
mPromise->MaybeRejectWithClone(aCx, aValue);
}
private:

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

@ -191,6 +191,18 @@ function defaultGenerateBundlesSync(resourceIds) {
return L10nRegistry.generateBundlesSync(appLocales, resourceIds);
}
function maybeReportErrorToGecko(error) {
if (AppConstants.NIGHTLY_BUILD || Cu.isInAutomation) {
if (Cu.isInAutomation) {
// We throw a string, rather than Error
// to allow the C++ Promise handler
// to clone it
throw error;
}
console.warn(error);
}
}
/**
* The `Localization` class is a central high-level API for vanilla
* JavaScript use of Fluent.
@ -246,23 +258,24 @@ class Localization {
* @private
*/
async formatWithFallback(keys, method) {
const translations = [];
const translations = new Array(keys.length);
let hasAtLeastOneBundle = false;
for await (const bundle of this.bundles) {
hasAtLeastOneBundle = true;
const missingIds = keysFromBundle(method, bundle, keys, translations);
if (missingIds.size === 0) {
break;
}
if (AppConstants.NIGHTLY_BUILD || Cu.isInAutomation) {
const locale = bundle.locales[0];
const ids = Array.from(missingIds).join(", ");
if (Cu.isInAutomation) {
throw new Error(`Missing translations in ${locale}: ${ids}`);
}
console.warn(`Missing translations in ${locale}: ${ids}`);
}
const locale = bundle.locales[0];
const ids = Array.from(missingIds).join(", ");
maybeReportErrorToGecko(`[fluent] Missing translations in ${locale}: ${ids}.`);
}
if (!hasAtLeastOneBundle) {
maybeReportErrorToGecko(`[fluent] Request for keys failed because no resource bundles got generated.\n keys: ${JSON.stringify(keys)}.\n resourceIds: ${JSON.stringify(this.resourceIds)}.`);
}
return translations;
@ -416,23 +429,24 @@ class LocalizationSync extends Localization {
}
formatWithFallback(keys, method) {
const translations = [];
const translations = new Array(keys.length);
let hasAtLeastOneBundle = false;
for (const bundle of this.bundles) {
hasAtLeastOneBundle = true;
const missingIds = keysFromBundle(method, bundle, keys, translations);
if (missingIds.size === 0) {
break;
}
if (AppConstants.NIGHTLY_BUILD || Cu.isInAutomation) {
const locale = bundle.locales[0];
const ids = Array.from(missingIds).join(", ");
if (Cu.isInAutomation) {
throw new Error(`Missing translations in ${locale}: ${ids}`);
}
console.warn(`Missing translations in ${locale}: ${ids}`);
}
const locale = bundle.locales[0];
const ids = Array.from(missingIds).join(", ");
maybeReportErrorToGecko(`[fluent] Missing translations in ${locale}: ${ids}.`);
}
if (!hasAtLeastOneBundle) {
maybeReportErrorToGecko(`[fluent] Request for keys failed because no resource bundles got generated.\n keys: ${JSON.stringify(keys)}.\n resourceIds: ${JSON.stringify(this.resourceIds)}.`);
}
return translations;
@ -554,12 +568,17 @@ function keysFromBundle(method, bundle, keys, translations) {
if (bundle.hasMessage(id)) {
messageErrors.length = 0;
translations[i] = method(bundle, messageErrors, id, args);
// XXX: Report resolver errors
if (messageErrors.length > 0) {
const locale = bundle.locales[0];
const errors = messageErrors.join(", ");
maybeReportErrorToGecko(`[fluent][resolver] errors in ${locale}/${id}: ${errors}.`);
}
} else {
missingIds.add(id);
}
});
return missingIds;
}