feat: added proper error messages on load

Signed-off-by: Varun Raj <mailme@varunraj.in>
This commit is contained in:
Varun Raj committed 2024-08-29 09:08:32 +05:30
1 parent 4f6117ded2
commit 86ae90b16a
5 files changed
+66 -10

No files matched your search

+31 -5
View File
@@ -6,6 +6,8 @@ import { getMe } from "@/handlers/api/user.handler";
import UserContext from "@/contexts/CurrentUserContext"; import UserContext from "@/contexts/CurrentUserContext";
import ErrorBlock from "../shared/ErrorBlock"; import ErrorBlock from "../shared/ErrorBlock";
import Loader from "../ui/loader"; import Loader from "../ui/loader";
import { ArrowRight, TriangleAlert } from "lucide-react";
import Link from "next/link";
type RootLayoutProps = { type RootLayoutProps = {
children: ReactNode; children: ReactNode;
@@ -14,13 +16,16 @@ type RootLayoutProps = {
export default function RootLayout({ children }: RootLayoutProps) { export default function RootLayout({ children }: RootLayoutProps) {
const [user, setUser] = useState<IUser | null>(null); const [user, setUser] = useState<IUser | null>(null);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [errorMessage, setErrorMessage] = useState<string | null>(null); const [errorMessage, setErrorMessage] = useState<{
error: string;
message: string;
} | null>(null);
const fetchData = () => { const fetchData = () => {
return getMe() return getMe()
.then(setUser) .then(setUser)
.catch((error) => { .catch((error) => {
setErrorMessage(error.message); setErrorMessage(error);
}) })
.finally(() => setLoading(false)); .finally(() => setLoading(false));
}; };
@@ -29,12 +34,33 @@ export default function RootLayout({ children }: RootLayoutProps) {
fetchData(); fetchData();
}, []); }, []);
if (loading) return <Loader /> if (loading) return <Loader />;
if (errorMessage) return <ErrorBlock description={errorMessage} />; if (errorMessage)
return (
<div className="min-h-screen flex flex-col justify-center">
<ErrorBlock
icon={<TriangleAlert />}
title={errorMessage.error}
description={errorMessage.message}
action={
<Link
className="flex items-center gap-2 text-xs"
href={
"https://github.com/varun-raj/immich-power-tools?tab=readme-ov-file#-getting-started"
}
target="_blank"
>
Setup Instructions
<ArrowRight className="h-4 w-4" />
</Link>
}
/>
</div>
);
if (!user) return null; if (!user) return null;
return ( return (
<UserContext.Provider value={user}> <UserContext.Provider value={user}>
<div className="grid max-h-screen min-h-screen w-full md:grid-cols-[200px_1fr] lg:grid-cols-[240px_1fr]"> <div className="grid max-h-screen min-h-screen w-full md:grid-cols-[200px_1fr] lg:grid-cols-[240px_1fr]">
<Sidebar /> <Sidebar />
<div className="flex flex-col">{children}</div> <div className="flex flex-col">{children}</div>
+3 -1
View File
@@ -4,15 +4,17 @@ interface ErrorBlockProps {
icon?: React.ReactNode; icon?: React.ReactNode;
title?: string; title?: string;
description?: string; description?: string;
action?: React.ReactNode;
} }
export default function ErrorBlock( export default function ErrorBlock(
{ icon, title, description }: ErrorBlockProps { icon, title, description, action }: ErrorBlockProps
) { ) {
return ( return (
<div className="flex flex-col items-center justify-center w-full h-full space-y-4 py-10"> <div className="flex flex-col items-center justify-center w-full h-full space-y-4 py-10">
{icon} {icon}
{title && <h2 className="text-xl font-semibold">{title}</h2>} {title && <h2 className="text-xl font-semibold">{title}</h2>}
{description && <p className="text-gray-500">{description}</p>} {description && <p className="text-gray-500">{description}</p>}
{action || null}
</div> </div>
) )
} }
+2 -2
View File
@@ -1,4 +1,4 @@
import { GalleryVerticalEnd, Home, Image, LocateOff, User } from "lucide-react"; import { GalleryVerticalEnd, Home, Image, LocateOff, MapPinX, User } from "lucide-react";
export const sidebarNavs = [ export const sidebarNavs = [
{ {
@@ -19,6 +19,6 @@ export const sidebarNavs = [
{ {
title: "Missing Locations", title: "Missing Locations",
link: "/assets/missing-locations", link: "/assets/missing-locations",
icon: <LocateOff className="h-4 w-4" />, icon: <MapPinX className="h-4 w-4" />,
}, },
]; ];
+29 -1
View File
@@ -3,6 +3,34 @@ import axios from 'axios'
import qs from 'qs' import qs from 'qs'
const handleErrors = (error: AxiosError | any) => {
if (error.code === "ERR_BAD_RESPONSE") {
return Promise.reject({
error: "Unable to process this request, please try again later",
message: error.code
});
}
let errorObject = {}
if (error.response) {
const { status } = error.response
if (status === 422) {
errorObject = {
...error.response.data,
message: 'Unable to process this request, please try again later',
}
} else {
errorObject = {
...error.response.data,
}
}
return Promise.reject(errorObject)
}
errorObject = {
message: 'Network Error. Please check your connection and try again!',
}
return Promise.reject(errorObject)
}
export default class API { export default class API {
@@ -91,6 +119,6 @@ export default class API {
options.headers = Object.assign(API.headers(contentType), headers) options.headers = Object.assign(API.headers(contentType), headers)
return axios(options) return axios(options)
.then((responseJson) => responseJson.data) .then((responseJson) => responseJson.data)
// .catch(handleErrors) .catch(handleErrors)
} }
} }
+1 -1
View File
@@ -41,6 +41,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
res.end() res.end()
} catch (error) { } catch (error) {
console.error('Proxy error:', error) console.error('Proxy error:', error)
res.status(500).json({ error: 'Internal Server Error' }) res.status(500).json({ error })
} }
} }