Skip to content

Commit c5fee25

Browse files
committed
feat(react): add switch to provider to log errors
Because: * We should allow users to control if reportErrors are reported or not. This commit: * Adds a class property to ReactLocalization to enable/disable logging in reportErrors. * Allows <LocalizationProvider> to enable/disable error reporting Closes: #411
1 parent 20ebb43 commit c5fee25

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

fluent-react/src/localization.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const reMarkup = /<|&#?\w+;/;
2929
export class ReactLocalization {
3030
public bundles: Iterable<FluentBundle>;
3131
public parseMarkup: MarkupParser | null;
32+
private logErrors: boolean = true;
3233

3334
constructor(
3435
bundles: Iterable<FluentBundle>,
@@ -228,11 +229,15 @@ export class ReactLocalization {
228229
return cloneElement(sourceElement, localizedProps, ...translatedChildren);
229230
}
230231

231-
// XXX Control this via a prop passed to the LocalizationProvider.
232-
// See https://github.com/projectfluent/fluent.js/issues/411.
233232
reportError(error: Error): void {
234-
/* global console */
235-
// eslint-disable-next-line no-console
236-
console.warn(`[@fluent/react] ${error.name}: ${error.message}`);
233+
if (this.logErrors) {
234+
/* global console */
235+
// eslint-disable-next-line no-console
236+
console.warn(`[@fluent/react] ${error.name}: ${error.message}`);
237+
}
238+
}
239+
240+
setLogErrors(logError: boolean): void {
241+
this.logErrors = logError;
237242
}
238243
}

fluent-react/src/provider.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ import { ReactLocalization } from "./localization.js";
2424
export function LocalizationProvider(props: {
2525
children: ReactNode;
2626
l10n: ReactLocalization;
27+
logErrors?: boolean;
2728
}): ReactElement {
29+
if (props.logErrors !== undefined) {
30+
props.l10n.setLogErrors(props.logErrors);
31+
}
32+
2833
return createElement(
2934
FluentContext.Provider,
3035
{ value: props.l10n },

fluent-react/test/localized_valid.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,18 @@ describe("Localized - validation", () => {
114114
]
115115
`);
116116
});
117+
118+
test("Does not have a warning when no id is provided", () => {
119+
jest.spyOn(console, "warn").mockImplementation(() => {});
120+
const renderer = TestRenderer.create(
121+
<LocalizationProvider l10n={new ReactLocalization([])} logErrors={false}>
122+
<Localized>
123+
<div />
124+
</Localized>
125+
</LocalizationProvider>
126+
);
127+
128+
expect(renderer.toJSON()).toMatchInlineSnapshot(`<div />`);
129+
expect(console.warn.mock.calls).toMatchInlineSnapshot(`[]`);
130+
});
117131
});

0 commit comments

Comments
 (0)