mirror of
https://github.com/karl0ss/homepage.git
synced 2025-04-29 12:03:41 +01:00
place error boundaries closer to the source
This commit is contained in:
parent
88c774339d
commit
ea96999377
@ -1,4 +1,4 @@
|
|||||||
import React from 'react';
|
import React from "react";
|
||||||
|
|
||||||
export default class ErrorBoundary extends React.Component {
|
export default class ErrorBoundary extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
@ -10,8 +10,8 @@ export default class ErrorBoundary extends React.Component {
|
|||||||
// Catch errors in any components below and re-render with error message
|
// Catch errors in any components below and re-render with error message
|
||||||
this.setState({
|
this.setState({
|
||||||
error,
|
error,
|
||||||
errorInfo
|
errorInfo,
|
||||||
})
|
});
|
||||||
|
|
||||||
// You can also log error messages to an error reporting service here
|
// You can also log error messages to an error reporting service here
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
@ -23,11 +23,10 @@ export default class ErrorBoundary extends React.Component {
|
|||||||
if (errorInfo) {
|
if (errorInfo) {
|
||||||
// Error path
|
// Error path
|
||||||
return (
|
return (
|
||||||
<div>
|
<div className="inline-block text-sm bg-rose-100 text-rose-900 dark:bg-rose-900 dark:text-rose-100 rounded-md p-2 m-1">
|
||||||
<h2>Something went wrong.</h2>
|
<div className="font-medium mb-1">Something went wrong.</div>
|
||||||
<details style={{ whiteSpace: 'pre-wrap' }}>
|
<details className="text-xs font-mono whitespace-pre">
|
||||||
{error && error.toString()}
|
<summary>{error && error.toString()}</summary>
|
||||||
<br />
|
|
||||||
{errorInfo.componentStack}
|
{errorInfo.componentStack}
|
||||||
</details>
|
</details>
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
|
|
||||||
import ErrorBoundary from "components/errorboundry";
|
|
||||||
import List from "components/services/list";
|
import List from "components/services/list";
|
||||||
|
|
||||||
export default function ServicesGroup({ services, layout }) {
|
export default function ServicesGroup({ services, layout }) {
|
||||||
@ -13,7 +12,7 @@ export default function ServicesGroup({ services, layout }) {
|
|||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<h2 className="text-theme-800 dark:text-theme-300 text-xl font-medium">{services.name}</h2>
|
<h2 className="text-theme-800 dark:text-theme-300 text-xl font-medium">{services.name}</h2>
|
||||||
<ErrorBoundary><List services={services.services} layout={layout} /></ErrorBoundary>
|
<List services={services.services} layout={layout} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
|
|
||||||
import ErrorBoundary from "components/errorboundry";
|
|
||||||
import Item from "components/services/item";
|
import Item from "components/services/item";
|
||||||
|
|
||||||
const columnMap = [
|
const columnMap = [
|
||||||
@ -24,7 +23,7 @@ export default function List({ services, layout }) {
|
|||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{services.map((service) => (
|
{services.map((service) => (
|
||||||
<ErrorBoundary key={service.name}><Item key={service.name} service={service} /></ErrorBoundary>
|
<Item key={service.name} service={service} />
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
);
|
);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { useTranslation } from "next-i18next";
|
import { useTranslation } from "next-i18next";
|
||||||
|
|
||||||
|
import ErrorBoundary from "components/errorboundry";
|
||||||
import components from "widgets/components";
|
import components from "widgets/components";
|
||||||
|
|
||||||
export default function Widget({ service }) {
|
export default function Widget({ service }) {
|
||||||
@ -8,7 +9,11 @@ export default function Widget({ service }) {
|
|||||||
const ServiceWidget = components[service.widget.type];
|
const ServiceWidget = components[service.widget.type];
|
||||||
|
|
||||||
if (ServiceWidget) {
|
if (ServiceWidget) {
|
||||||
return <ServiceWidget service={service} />;
|
return (
|
||||||
|
<ErrorBoundary>
|
||||||
|
<ServiceWidget service={service} />
|
||||||
|
</ErrorBoundary>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import dynamic from "next/dynamic";
|
import dynamic from "next/dynamic";
|
||||||
|
|
||||||
|
import ErrorBoundary from "components/errorboundry";
|
||||||
|
|
||||||
const widgetMappings = {
|
const widgetMappings = {
|
||||||
weatherapi: dynamic(() => import("components/widgets/weather/weather")),
|
weatherapi: dynamic(() => import("components/widgets/weather/weather")),
|
||||||
openweathermap: dynamic(() => import("components/widgets/openweathermap/weather")),
|
openweathermap: dynamic(() => import("components/widgets/openweathermap/weather")),
|
||||||
@ -13,7 +15,11 @@ export default function Widget({ widget }) {
|
|||||||
const InfoWidget = widgetMappings[widget.type];
|
const InfoWidget = widgetMappings[widget.type];
|
||||||
|
|
||||||
if (InfoWidget) {
|
if (InfoWidget) {
|
||||||
return <InfoWidget options={widget.options} />;
|
return (
|
||||||
|
<ErrorBoundary>
|
||||||
|
<InfoWidget options={widget.options} />
|
||||||
|
</ErrorBoundary>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -8,7 +8,6 @@ import { useEffect, useContext, useState } from "react";
|
|||||||
import { BiError } from "react-icons/bi";
|
import { BiError } from "react-icons/bi";
|
||||||
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
|
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
|
||||||
|
|
||||||
import ErrorBoundary from "components/errorboundry";
|
|
||||||
import ServicesGroup from "components/services/group";
|
import ServicesGroup from "components/services/group";
|
||||||
import BookmarksGroup from "components/bookmarks/group";
|
import BookmarksGroup from "components/bookmarks/group";
|
||||||
import Widget from "components/widgets/widget";
|
import Widget from "components/widgets/widget";
|
||||||
@ -20,6 +19,7 @@ import { ColorContext } from "utils/contexts/color";
|
|||||||
import { ThemeContext } from "utils/contexts/theme";
|
import { ThemeContext } from "utils/contexts/theme";
|
||||||
import { SettingsContext } from "utils/contexts/settings";
|
import { SettingsContext } from "utils/contexts/settings";
|
||||||
import { bookmarksResponse, servicesResponse, widgetsResponse } from "utils/config/api-response";
|
import { bookmarksResponse, servicesResponse, widgetsResponse } from "utils/config/api-response";
|
||||||
|
import ErrorBoundary from "components/errorboundry";
|
||||||
|
|
||||||
const ThemeToggle = dynamic(() => import("components/toggles/theme"), {
|
const ThemeToggle = dynamic(() => import("components/toggles/theme"), {
|
||||||
ssr: false,
|
ssr: false,
|
||||||
@ -145,7 +145,9 @@ function Index({ initialSettings, fallback }) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<SWRConfig value={{ fallback, fetcher: (resource, init) => fetch(resource, init).then((res) => res.json()) }}>
|
<SWRConfig value={{ fallback, fetcher: (resource, init) => fetch(resource, init).then((res) => res.json()) }}>
|
||||||
<Home initialSettings={initialSettings} />
|
<ErrorBoundary>
|
||||||
|
<Home initialSettings={initialSettings} />
|
||||||
|
</ErrorBoundary>
|
||||||
</SWRConfig>
|
</SWRConfig>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -192,14 +194,14 @@ function Home({ initialSettings }) {
|
|||||||
{widgets
|
{widgets
|
||||||
.filter((widget) => !rightAlignedWidgets.includes(widget.type))
|
.filter((widget) => !rightAlignedWidgets.includes(widget.type))
|
||||||
.map((widget, i) => (
|
.map((widget, i) => (
|
||||||
<ErrorBoundary key={i}><Widget key={i} widget={widget} /></ErrorBoundary>
|
<Widget key={i} widget={widget} />
|
||||||
))}
|
))}
|
||||||
|
|
||||||
<div className="ml-4 flex flex-wrap basis-full grow sm:basis-auto justify-between md:justify-end mt-2 md:mt-0">
|
<div className="ml-4 flex flex-wrap basis-full grow sm:basis-auto justify-between md:justify-end mt-2 md:mt-0">
|
||||||
{widgets
|
{widgets
|
||||||
.filter((widget) => rightAlignedWidgets.includes(widget.type))
|
.filter((widget) => rightAlignedWidgets.includes(widget.type))
|
||||||
.map((widget, i) => (
|
.map((widget, i) => (
|
||||||
<ErrorBoundary key={i}><Widget key={i} widget={widget} /></ErrorBoundary>
|
<Widget key={i} widget={widget} />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
@ -209,7 +211,7 @@ function Home({ initialSettings }) {
|
|||||||
{services && (
|
{services && (
|
||||||
<div className="flex flex-wrap p-8 items-start">
|
<div className="flex flex-wrap p-8 items-start">
|
||||||
{services.map((group) => (
|
{services.map((group) => (
|
||||||
<ErrorBoundary key={group.name}><ServicesGroup key={group.name} services={group} layout={initialSettings.layout?.[group.name]} /></ErrorBoundary>
|
<ServicesGroup key={group.name} services={group} layout={initialSettings.layout?.[group.name]} />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@ -217,7 +219,7 @@ function Home({ initialSettings }) {
|
|||||||
{bookmarks && (
|
{bookmarks && (
|
||||||
<div className="grow flex flex-wrap pt-0 p-8">
|
<div className="grow flex flex-wrap pt-0 p-8">
|
||||||
{bookmarks.map((group) => (
|
{bookmarks.map((group) => (
|
||||||
<ErrorBoundary key={group.name}><BookmarksGroup key={group.name} group={group} /></ErrorBoundary>
|
<BookmarksGroup key={group.name} group={group} />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user