Bug 1623778 - Improve error reporting for AddResource. r=emilio

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Zibi Braniecki 2020-03-21 01:13:28 +00:00
parent 1aa5b504ca
commit 7f4b927d9e
2 changed files with 51 additions and 26 deletions

View File

@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "FluentBundle.h"
#include "nsContentUtils.h"
#include "mozilla/dom/UnionTypes.h"
#include "unicode/numberformatter.h"
#include "unicode/datefmt.h"
@ -102,7 +103,16 @@ void FluentBundle::AddResource(
FluentResource& aResource,
const dom::FluentBundleAddResourceOptions& aOptions) {
bool allowOverrides = aOptions.mAllowOverrides;
fluent_bundle_add_resource(mRaw.get(), aResource.Raw(), allowOverrides);
nsTArray<nsCString> errors;
fluent_bundle_add_resource(mRaw.get(), aResource.Raw(), allowOverrides,
&errors);
for (auto& err : errors) {
nsContentUtils::LogSimpleConsoleError(NS_ConvertUTF8toUTF16(err), "L10n",
false, true,
nsIScriptError::warningFlag);
}
}
bool FluentBundle::HasMessage(const nsACString& aId) {

View File

@ -213,29 +213,7 @@ pub unsafe extern "C" fn fluent_bundle_format_pattern(
let mut errors = vec![];
let value = bundle.format_pattern(pattern, args.as_ref(), &mut errors);
ret_val.assign(value.as_bytes());
for error in errors {
match error {
FluentError::ResolverError(ref err) => match err {
ResolverError::Reference(ref s) => {
let error = format!("ReferenceError: {}", s);
ret_errors.push((&error).into());
}
ResolverError::MissingDefault => {
let error = "RangeError: No default";
ret_errors.push(error.into());
}
ResolverError::Cyclic => {
let error = "RangeError: Cyclic reference";
ret_errors.push(error.into());
}
ResolverError::TooManyPlaceables => {
let error = "RangeError: Too many placeables";
ret_errors.push(error.into());
}
},
_ => panic!("Unknown error!"),
}
}
append_fluent_errors_to_ret_errors(ret_errors, &errors);
true
}
@ -244,14 +222,15 @@ pub unsafe extern "C" fn fluent_bundle_add_resource(
bundle: &mut FluentBundleRc,
r: &FluentResource,
allow_overrides: bool,
ret_errors: &mut ThinVec<nsCString>,
) {
// we don't own the resource
let r = mem::ManuallyDrop::new(Rc::from_raw(r));
if allow_overrides {
bundle.add_resource_overriding(Rc::clone(&r));
} else if bundle.add_resource(Rc::clone(&r)).is_err() {
eprintln!("Error while adding a resource");
} else if let Err(errors) = bundle.add_resource(Rc::clone(&r)) {
append_fluent_errors_to_ret_errors(ret_errors, &errors);
}
}
@ -272,3 +251,39 @@ fn convert_args<'a>(ids: &'a [String], arg_vals: &'a [FluentArgument]) -> Option
}
Some(args)
}
fn append_fluent_errors_to_ret_errors(ret_errors: &mut ThinVec<nsCString>, errors: &[FluentError]) {
for error in errors {
match error {
FluentError::ResolverError(ref err) => match err {
ResolverError::Reference(ref s) => {
let error = format!("[fluent] ReferenceError: {}.", s);
ret_errors.push(error.into());
}
ResolverError::MissingDefault => {
let error = "[fluent] RangeError: No default value for selector.";
ret_errors.push(error.into());
}
ResolverError::Cyclic => {
let error = "[fluent] RangeError: Cyclic reference encountered while resolving a message.";
ret_errors.push(error.into());
}
ResolverError::TooManyPlaceables => {
let error = "[fluent] RangeError: Too many placeables in a message.";
ret_errors.push(error.into());
}
},
FluentError::Overriding { kind, id } => {
let error = format!("[fluent] OverrideError: An entry {} of type {} is already defined in this bundle.", id, kind);
ret_errors.push(error.into());
}
FluentError::ParserError(pe) => {
let error = format!(
"[fluent] ParserError: Error of kind {:#?} in position: ({}, {})",
pe.kind, pe.pos.0, pe.pos.1
);
ret_errors.push(error.into());
}
}
}
}