156 lines
5.8 KiB
JavaScript
156 lines
5.8 KiB
JavaScript
"use client";
|
|
|
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
import React from "react";
|
|
import { usePathname } from "./navigation";
|
|
import { isNextRouterError } from "./is-next-router-error";
|
|
import { staticGenerationAsyncStorage } from "./static-generation-async-storage.external";
|
|
const styles = {
|
|
error: {
|
|
// https://github.com/sindresorhus/modern-normalize/blob/main/modern-normalize.css#L38-L52
|
|
fontFamily: 'system-ui,"Segoe UI",Roboto,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"',
|
|
height: "100vh",
|
|
textAlign: "center",
|
|
display: "flex",
|
|
flexDirection: "column",
|
|
alignItems: "center",
|
|
justifyContent: "center"
|
|
},
|
|
text: {
|
|
fontSize: "14px",
|
|
fontWeight: 400,
|
|
lineHeight: "28px",
|
|
margin: "0 8px"
|
|
}
|
|
};
|
|
// if we are revalidating we want to re-throw the error so the
|
|
// function crashes so we can maintain our previous cache
|
|
// instead of caching the error page
|
|
function HandleISRError(param) {
|
|
let { error } = param;
|
|
const store = staticGenerationAsyncStorage.getStore();
|
|
if ((store == null ? void 0 : store.isRevalidate) || (store == null ? void 0 : store.isStaticGeneration)) {
|
|
console.error(error);
|
|
throw error;
|
|
}
|
|
return null;
|
|
}
|
|
export class ErrorBoundaryHandler extends React.Component {
|
|
static getDerivedStateFromError(error) {
|
|
if (isNextRouterError(error)) {
|
|
// Re-throw if an expected internal Next.js router error occurs
|
|
// this means it should be handled by a different boundary (such as a NotFound boundary in a parent segment)
|
|
throw error;
|
|
}
|
|
return {
|
|
error
|
|
};
|
|
}
|
|
static getDerivedStateFromProps(props, state) {
|
|
/**
|
|
* Handles reset of the error boundary when a navigation happens.
|
|
* Ensures the error boundary does not stay enabled when navigating to a new page.
|
|
* Approach of setState in render is safe as it checks the previous pathname and then overrides
|
|
* it as outlined in https://react.dev/reference/react/useState#storing-information-from-previous-renders
|
|
*/ if (props.pathname !== state.previousPathname && state.error) {
|
|
return {
|
|
error: null,
|
|
previousPathname: props.pathname
|
|
};
|
|
}
|
|
return {
|
|
error: state.error,
|
|
previousPathname: props.pathname
|
|
};
|
|
}
|
|
// Explicit type is needed to avoid the generated `.d.ts` having a wide return type that could be specific the the `@types/react` version.
|
|
render() {
|
|
if (this.state.error) {
|
|
return /*#__PURE__*/ _jsxs(_Fragment, {
|
|
children: [
|
|
/*#__PURE__*/ _jsx(HandleISRError, {
|
|
error: this.state.error
|
|
}),
|
|
this.props.errorStyles,
|
|
this.props.errorScripts,
|
|
/*#__PURE__*/ _jsx(this.props.errorComponent, {
|
|
error: this.state.error,
|
|
reset: this.reset
|
|
})
|
|
]
|
|
});
|
|
}
|
|
return this.props.children;
|
|
}
|
|
constructor(props){
|
|
super(props);
|
|
this.reset = ()=>{
|
|
this.setState({
|
|
error: null
|
|
});
|
|
};
|
|
this.state = {
|
|
error: null,
|
|
previousPathname: this.props.pathname
|
|
};
|
|
}
|
|
}
|
|
export function GlobalError(param) {
|
|
let { error } = param;
|
|
const digest = error == null ? void 0 : error.digest;
|
|
return /*#__PURE__*/ _jsxs("html", {
|
|
id: "__next_error__",
|
|
children: [
|
|
/*#__PURE__*/ _jsx("head", {}),
|
|
/*#__PURE__*/ _jsxs("body", {
|
|
children: [
|
|
/*#__PURE__*/ _jsx(HandleISRError, {
|
|
error: error
|
|
}),
|
|
/*#__PURE__*/ _jsx("div", {
|
|
style: styles.error,
|
|
children: /*#__PURE__*/ _jsxs("div", {
|
|
children: [
|
|
/*#__PURE__*/ _jsx("h2", {
|
|
style: styles.text,
|
|
children: "Application error: a " + (digest ? "server" : "client") + "-side exception has occurred (see the " + (digest ? "server logs" : "browser console") + " for more information)."
|
|
}),
|
|
digest ? /*#__PURE__*/ _jsx("p", {
|
|
style: styles.text,
|
|
children: "Digest: " + digest
|
|
}) : null
|
|
]
|
|
})
|
|
})
|
|
]
|
|
})
|
|
]
|
|
});
|
|
}
|
|
// Exported so that the import signature in the loaders can be identical to user
|
|
// supplied custom global error signatures.
|
|
export default GlobalError;
|
|
/**
|
|
* Handles errors through `getDerivedStateFromError`.
|
|
* Renders the provided error component and provides a way to `reset` the error boundary state.
|
|
*/ /**
|
|
* Renders error boundary with the provided "errorComponent" property as the fallback.
|
|
* If no "errorComponent" property is provided it renders the children without an error boundary.
|
|
*/ export function ErrorBoundary(param) {
|
|
let { errorComponent, errorStyles, errorScripts, children } = param;
|
|
const pathname = usePathname();
|
|
if (errorComponent) {
|
|
return /*#__PURE__*/ _jsx(ErrorBoundaryHandler, {
|
|
pathname: pathname,
|
|
errorComponent: errorComponent,
|
|
errorStyles: errorStyles,
|
|
errorScripts: errorScripts,
|
|
children: children
|
|
});
|
|
}
|
|
return /*#__PURE__*/ _jsx(_Fragment, {
|
|
children: children
|
|
});
|
|
}
|
|
|
|
//# sourceMappingURL=error-boundary.js.map
|