Skip to content

Commit

Permalink
Better error handling (hooks)
Browse files Browse the repository at this point in the history
  • Loading branch information
j3lte committed May 14, 2024
1 parent ead43e6 commit e273f19
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-mustache-template-component",
"version": "2.1.4",
"version": "2.1.5",
"description": "Mustache Template Component for React",
"author": {
"name": "J.W. Lagendijk",
Expand Down
2 changes: 1 addition & 1 deletion src/TemplateComponent/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe("TemplateComponent", () => {
);

expect(el).toBeNull();
expect(mocked).toHaveBeenCalled();
expect(mocked).toHaveBeenCalledWith(expect.any(Error));

console.error = originalError;
});
Expand Down
74 changes: 44 additions & 30 deletions src/TemplateComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,44 +69,58 @@ export interface TemplateComponentProps extends HTMLAttributes<HTMLElement> {
const TemplateComponentInternal: FunctionComponent<TemplateComponentProps> =
forwardRef<ComponentType, TemplateComponentProps>(
({ template, sanitize, sanitizeOptions, data, type, ...args }, ref) => {
try {
const sanitizer = dompurify.sanitize;
const compiled = useMemo(
() =>
typeof template === "string"
? Mustache.render(template, data)
: null,
[template, data],
);
const shouldSanitize = typeof sanitize === "boolean" ? sanitize : true;
const innerType = type || "div";
const sanitizer = dompurify.sanitize;
const shouldSanitize = typeof sanitize === "boolean" ? sanitize : true;
const innerType = type || "div";

const compiled = useMemo(() => {
if (typeof template === "string") {
try {
return Mustache.render(template, data);
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
return null;
}
}
return null;
}, [template, data]);

const html = useMemo(() => {
if (compiled === null) {
return null;
}
try {
return (
shouldSanitize
? typeof sanitizeOptions !== "undefined"
? sanitizer(compiled, sanitizeOptions)
: sanitizer(compiled)
: compiled
) as string;
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
return null;
}
}, [compiled]);

const html = (
shouldSanitize
? typeof sanitizeOptions !== "undefined"
? sanitizer(compiled, sanitizeOptions)
: sanitizer(compiled)
: compiled
) as string;

const htmlOpts = useMemo(
() => ({
...args,
dangerouslySetInnerHTML: { __html: html },
}),
[html],
);
const htmlOpts = useMemo(
() =>
html
? {
...args,
dangerouslySetInnerHTML: { __html: html },
}
: args,
[html],
);

return createElement(innerType, { ...htmlOpts, ref }, null);
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
if (!html) {
return null;
}

return createElement(innerType, { ...htmlOpts, ref }, null);
},
);

Expand Down

0 comments on commit e273f19

Please sign in to comment.