Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add error #882

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions src/app/user/join/[teamID]/page.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"use client";
import { useState, useEffect } from "react";
import Error from "@/components/dynamic/Error";
import axios from "axios";
import Button from "@/components/dynamic/Button";
import { useRouter } from "next/navigation";
import toast from "react-hot-toast";
import { useSession } from "next-auth/react";
import Fault from "@/utils/error";

export default function page({ params }) {
const [team, setTeam] = useState(null);
const [error, setError] = useState(null);
const router = useRouter();
const { update: sessionUpdate } = useSession();
const handleJoin = () => {
Expand Down Expand Up @@ -37,26 +36,23 @@ export default function page({ params }) {
.then((response) => setTeam(response.data.items))
.catch(({ response: data }) => {
if (data.data.message === "Invalid Team ID")
setError({
code: "404",
error: "Invalid Team ID",
message: "Please get a new team invite",
});
throw new Fault(
404,
"Invalid Team ID",
"Please get a new team invite"
);
else
setError({
code: "500",
error: "Internal Server Erro",
message: "Please contact web dev",
});
throw new Fault(
500,
"Internal Server Error",
"Please contact the software engineering team for assistance"
);
});
}
}, []);

return (
<div>
{error && (
<Error code={error.code} error={error.error} message={error.message} />
)}
{team && (
<div className="flex flex-col w-screen h-screen items-center justify-center font-poppins">
<p className="text-3xl">
Expand Down
35 changes: 12 additions & 23 deletions src/components/dynamic/ProtectedPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
import { useEffect, useState } from "react";
import { signIn, useSession } from "next-auth/react";
import Loading from "@/components/dynamic/Loading";
import Error from "./Error";
import { usePathname } from "next/navigation";
import RELEASES from "@/data/Releases";
import { ROUTES } from "@/data/ProtectedRoutes";
import Fault from "@/utils/error";

const ProtectedPage = ({ children }) => {
const { data: session, status } = useSession();
const [error, setError] = useState(null);
const [confirmed, setConfirmed] = useState(false);

const pathName = usePathname();
Expand All @@ -23,12 +22,11 @@ const ProtectedPage = ({ children }) => {
return;
}
if (RELEASES.DYNAMIC[pathName] > new Date()) {
setError({
code: 423,
error: "Locked Resource",
message: "This resource has not been released",
});
return;
throw new Fault(
423,
"Locked Resource",
"This resource has not been released"
);
}

if (status === "loading") return;
Expand All @@ -38,12 +36,11 @@ const ProtectedPage = ({ children }) => {
}

if (!session.user.roles && Object.keys(restrictions).length > 0) {
setError({
code: 403,
error: "Unauthorized",
message: "You do not have any assigned roles",
});
return;
throw new Fault(
403,
"Unauthorized",
"You do not have any assigned roles"
);
}

const authorized = Object.entries(restrictions).some(([key, values]) =>
Expand All @@ -53,22 +50,14 @@ const ProtectedPage = ({ children }) => {
);

if (!authorized && Object.keys(restrictions).length > 0) {
setError({
code: 403,
error: "Unauthorized",
message: "You do not have access this page",
});
return;
throw new Fault(403, "Unauthorized", "You do not have access this page");
}
setConfirmed(true);
}, [status]);

return (
<>
{status === "loading" && <Loading />}
{error && (
<Error code={error.code} error={error.error} message={error.message} />
)}
{confirmed && (
<>
<title>{title}</title>
Expand Down
9 changes: 9 additions & 0 deletions src/utils/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Fault extends Error {
constructor(code, name, message) {
super(message);
this.name = name;
this.code = code;
}
}

export default Fault;