Skip to content

Commit 5bb07d5

Browse files
authored
feat: add error boundary for map (#109)
1 parent 920eff9 commit 5bb07d5

5 files changed

Lines changed: 64 additions & 10 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"next-themes": "^0.4.3",
5050
"react": "^19.1.0",
5151
"react-dom": "^19.1.0",
52+
"react-error-boundary": "^6.0.0",
5253
"react-icons": "^5.5.0",
5354
"react-map-gl": "^7.1.7",
5455
"react-markdown": "^10.1.0",

src/app.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
import { Box, Container, GridItem, SimpleGrid } from "@chakra-ui/react";
1+
import {
2+
Alert,
3+
Box,
4+
Container,
5+
Flex,
6+
GridItem,
7+
SimpleGrid,
8+
} from "@chakra-ui/react";
29
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
10+
import { ErrorBoundary } from "react-error-boundary";
311
import { MapProvider } from "react-map-gl/dist/esm/exports-maplibre";
412
import Header from "./components/header";
513
import Map from "./components/map";
@@ -15,7 +23,9 @@ export default function App() {
1523
<MapProvider>
1624
<StacMapProvider>
1725
<Box zIndex={0} position={"absolute"} top={0} left={0}>
18-
<Map></Map>
26+
<ErrorBoundary FallbackComponent={MapFallback}>
27+
<Map></Map>
28+
</ErrorBoundary>
1929
</Box>
2030
<Container zIndex={1} fluid h={"dvh"} py={4} pointerEvents={"none"}>
2131
<SimpleGrid columns={{ base: 1, md: 3 }} gap={4}>
@@ -36,3 +46,19 @@ export default function App() {
3646
</QueryClientProvider>
3747
);
3848
}
49+
50+
function MapFallback({ error }: { error: Error }) {
51+
return (
52+
<Flex h={"100dvh"} w={"100dvw"} alignItems="center" justifyContent="center">
53+
<Box>
54+
<Alert.Root status="error">
55+
<Alert.Indicator />
56+
<Alert.Content>
57+
<Alert.Title>Error while rendering the map</Alert.Title>
58+
<Alert.Description>{error.message}</Alert.Description>
59+
</Alert.Content>
60+
</Alert.Root>
61+
</Box>
62+
</Flex>
63+
);
64+
}

src/components/navigation-breadcrumbs.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function BreadcrumbItem({
108108
{(current && (
109109
<Breadcrumb.CurrentLink>
110110
<HStack>
111-
{getValueIcon(value)({})}
111+
{getValueIcon(value)}
112112
{text}
113113
</HStack>
114114
</Breadcrumb.CurrentLink>
@@ -124,7 +124,7 @@ function BreadcrumbItem({
124124
}
125125
}}
126126
>
127-
{getValueIcon(value)({})}
127+
{getValueIcon(value)}
128128
{text}
129129
</Breadcrumb.Link>
130130
)}
@@ -135,13 +135,13 @@ function BreadcrumbItem({
135135
function getValueIcon(value: StacValue) {
136136
switch (value.type) {
137137
case "Catalog":
138-
return LuFolder;
138+
return <LuFolder></LuFolder>;
139139
case "Collection":
140-
return LuFolderPlus;
140+
return <LuFolderPlus></LuFolderPlus>;
141141
case "Feature":
142-
return LuFile;
142+
return <LuFile></LuFile>;
143143
case "FeatureCollection":
144-
return LuFiles;
144+
return <LuFiles></LuFiles>;
145145
}
146146
}
147147

src/components/panel.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,30 @@ function ValueContent({ value }: { value: StacValue }) {
5454
return <Item item={value}></Item>;
5555
case "FeatureCollection":
5656
return <ItemCollection itemCollection={value}></ItemCollection>;
57+
case undefined:
58+
return (
59+
<Alert.Root status="error">
60+
<Alert.Indicator />
61+
<Alert.Content>
62+
<Alert.Title>Value does not have a "type" field</Alert.Title>
63+
</Alert.Content>
64+
</Alert.Root>
65+
);
5766
default:
5867
return (
59-
// @ts-expect-error Fallback for unknown types
60-
<Alert.Root status="error">Unknown STAC type: {value.type}</Alert.Root>
68+
<Alert.Root status="error">
69+
<Alert.Indicator />
70+
<Alert.Content>
71+
<Alert.Title>Unknown "type" field</Alert.Title>
72+
<Alert.Description>
73+
{
74+
// @ts-expect-error Fallback for unknown types
75+
value.type
76+
}{" "}
77+
is not a valid STAC type
78+
</Alert.Description>
79+
</Alert.Content>
80+
</Alert.Root>
6181
);
6282
}
6383
}

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5041,6 +5041,13 @@ react-dom@^19.1.0:
50415041
dependencies:
50425042
scheduler "^0.26.0"
50435043

5044+
react-error-boundary@^6.0.0:
5045+
version "6.0.0"
5046+
resolved "https://registry.yarnpkg.com/react-error-boundary/-/react-error-boundary-6.0.0.tgz#a9e552146958fa77d873b587aa6a5e97544ee954"
5047+
integrity sha512-gdlJjD7NWr0IfkPlaREN2d9uUZUlksrfOx7SX62VRerwXbMY6ftGCIZua1VG1aXFNOimhISsTq+Owp725b9SiA==
5048+
dependencies:
5049+
"@babel/runtime" "^7.12.5"
5050+
50445051
react-icons@^5.5.0:
50455052
version "5.5.0"
50465053
resolved "https://registry.yarnpkg.com/react-icons/-/react-icons-5.5.0.tgz#8aa25d3543ff84231685d3331164c00299cdfaf2"

0 commit comments

Comments
 (0)