184 lines
7.3 KiB
JavaScript
184 lines
7.3 KiB
JavaScript
'use client';
|
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
import React from 'react';
|
|
import { useUntrackedPathname } from './navigation-untracked';
|
|
import { isNextRouterError } from './is-next-router-error';
|
|
import { handleHardNavError } from './nav-failure-handler';
|
|
const workAsyncStorage = typeof window === 'undefined' ? require('../../server/app-render/work-async-storage.external').workAsyncStorage : undefined;
|
|
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;
|
|
if (workAsyncStorage) {
|
|
const store = workAsyncStorage.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) {
|
|
const { error } = state;
|
|
// if we encounter an error while
|
|
// a navigation is pending we shouldn't render
|
|
// the error boundary and instead should fallback
|
|
// to a hard navigation to attempt recovering
|
|
if (process.env.__NEXT_APP_NAV_FAIL_HANDLING) {
|
|
if (error && handleHardNavError(error)) {
|
|
// clear error so we don't render anything
|
|
return {
|
|
error: null,
|
|
previousPathname: props.pathname
|
|
};
|
|
}
|
|
}
|
|
/**
|
|
* 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 to 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__*/ _jsxs("h2", {
|
|
style: styles.text,
|
|
children: [
|
|
"Application error: a ",
|
|
digest ? 'server' : 'client',
|
|
"-side exception has occurred while loading ",
|
|
window.location.hostname,
|
|
" (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;
|
|
// When we're rendering the missing params shell, this will return null. This
|
|
// is because we won't be rendering any not found boundaries or error
|
|
// boundaries for the missing params shell. When this runs on the client
|
|
// (where these errors can occur), we will get the correct pathname.
|
|
const pathname = useUntrackedPathname();
|
|
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
|