easy-jsx-html-engine
Version:
Dead simple HTML engine using JSX syntax.
25 lines • 1.08 kB
JavaScript
import { createElement, Fragment } from "./create-element";
import { isPromise, normalizeChildren } from "./util";
function isFunctionChildren(children) {
return children?.length === 1 && typeof children[0] === "function";
}
export function ErrorBoundary(props) {
const children = normalizeChildren(isFunctionChildren(props.children)
? Promise.resolve().then(props.children[0])
: props.children);
if (!isPromise(children)) {
// error boundary can only catch errors in promises or functions
return createElement(Fragment, {}, children);
}
let result = Promise.resolve(createElement(Fragment, {}, children));
if (props.catch) {
const c = props.catch;
result = result.catch(typeof c === "function" ? c : () => createElement(Fragment, {}, c));
}
return result.catch(defaultCatch);
}
export function defaultCatch(error) {
console.error(error);
return createElement("pre", {}, error?.stack || error?.message || error || new Error("Unknown error").stack);
}
//# sourceMappingURL=error-boundary.js.map