Skip to content

Commit

Permalink
feat: adds reason param for server errors
Browse files Browse the repository at this point in the history
  • Loading branch information
malcodeman committed Nov 28, 2024
1 parent bee9ca7 commit 7a35876
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
5 changes: 4 additions & 1 deletion app/_components/Posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { FiLoader } from "react-icons/fi";
import ServerError from "@/app/_components/ServerError";
import { usePosts } from "@/app/_hooks/usePosts";
import { getGalleryImages } from "@/app/_lib/utils";
import { AxiosError } from "axios";

type Props = {
queryKey: string[];
Expand Down Expand Up @@ -43,7 +44,9 @@ export default function Posts(props: Props) {
<div>
{isError ? (
<ServerError
statusCode={error instanceof Error ? error.message : "Internal"}
reason={
error instanceof AxiosError ? error.response?.data.reason : ""
}
/>
) : (
<main
Expand Down
17 changes: 7 additions & 10 deletions app/_components/ServerError.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import { Button } from "@/components/Button";

type Props = {
statusCode: string;
reason: string;
};

export default function ServerError(props: Props) {
const { statusCode } = props;
const { reason } = props;

const getMessage = (code: string) => {
switch (code) {
case "Not Found":
return "This account has been banned or the username is incorrect.";
case "Forbidden":
return "This account has been suspended.";
case "Internal":
const getMessage = () => {
switch (reason) {
case "private":
return "This is a private community. Only approved members can view and contribute.";
default:
return "Internal server error";
}
};

return (
<div className="flex h-[calc(100vh_-_49px)] flex-col items-center justify-center p-4">
<h2 className="mb-4 text-lg">{getMessage(statusCode)}</h2>
<h2 className="mb-4 text-lg">{getMessage()}</h2>
<Button as="link" href="/">
Go home
</Button>
Expand Down

0 comments on commit 7a35876

Please sign in to comment.