Skip to content

Commit

Permalink
[SWM-411] Fix : errorhandling
Browse files Browse the repository at this point in the history
  • Loading branch information
oikkoikk committed Nov 22, 2023
1 parent a62cab5 commit a772813
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/app/auth/signin/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client';
import {
API_FETCH_ERROR,
ErrorMessage,
SESSION_ERROR
} from '@/src/api/ErrorMessage';
import useAuth from '@/src/hooks/useAuth';
import setErrorToast from '@/src/util/toast/setErrorToast';
import { useRouter } from 'next/navigation';

export default function ErrorHandler({
error,
reset
}: {
error: Error;
reset: () => void;
}) {
const router = useRouter();
const { logout } = useAuth();

if (error.cause === API_FETCH_ERROR) {
setErrorToast(error);
} else if (error.cause === SESSION_ERROR) {
logout();
} else {
setErrorToast(new Error(ErrorMessage.DEFAULT));
}

return null;
}
30 changes: 30 additions & 0 deletions src/app/auth/signout/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client';
import {
API_FETCH_ERROR,
ErrorMessage,
SESSION_ERROR
} from '@/src/api/ErrorMessage';
import useAuth from '@/src/hooks/useAuth';
import setErrorToast from '@/src/util/toast/setErrorToast';
import { useRouter } from 'next/navigation';

export default function ErrorHandler({
error,
reset
}: {
error: Error;
reset: () => void;
}) {
const router = useRouter();
const { logout } = useAuth();

if (error.cause === API_FETCH_ERROR) {
setErrorToast(error);
} else if (error.cause === SESSION_ERROR) {
logout();
} else {
setErrorToast(new Error(ErrorMessage.DEFAULT));
}

return null;
}
30 changes: 30 additions & 0 deletions src/app/classroom/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client';
import {
API_FETCH_ERROR,
ErrorMessage,
SESSION_ERROR
} from '@/src/api/ErrorMessage';
import useAuth from '@/src/hooks/useAuth';
import setErrorToast from '@/src/util/toast/setErrorToast';
import { useRouter } from 'next/navigation';

export default function ErrorHandler({
error,
reset
}: {
error: Error;
reset: () => void;
}) {
const router = useRouter();
const { logout } = useAuth();

if (error.cause === API_FETCH_ERROR) {
setErrorToast(error);
} else if (error.cause === SESSION_ERROR) {
logout();
} else {
setErrorToast(new Error(ErrorMessage.DEFAULT));
}

return null;
}
30 changes: 30 additions & 0 deletions src/app/course/[course_id]/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client';
import {
API_FETCH_ERROR,
ErrorMessage,
SESSION_ERROR
} from '@/src/api/ErrorMessage';
import useAuth from '@/src/hooks/useAuth';
import setErrorToast from '@/src/util/toast/setErrorToast';
import { useRouter } from 'next/navigation';

export default function ErrorHandler({
error,
reset
}: {
error: Error;
reset: () => void;
}) {
const router = useRouter();
const { logout } = useAuth();

if (error.cause === API_FETCH_ERROR) {
setErrorToast(error);
} else if (error.cause === SESSION_ERROR) {
logout();
} else {
setErrorToast(new Error(ErrorMessage.DEFAULT));
}

return null;
}
2 changes: 2 additions & 0 deletions src/util/http/fetchErrorHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '@/src/api/ErrorMessage';
import { UNAUTHORIZED } from '@/src/constants/query/query';
import { signOut } from 'next-auth/react';
import setErrorToast from '../toast/setErrorToast';

export async function fetchErrorHandling(res: Response, errorMessage: string) {
const response = await res.json();
Expand All @@ -15,6 +16,7 @@ export async function fetchErrorHandling(res: Response, errorMessage: string) {
new Error(ErrorMessage.UNAUTHORIZED, { cause: SESSION_ERROR })
);
} else {
setErrorToast(new Error(errorMessage));
return Promise.reject(new Error(errorMessage, { cause: API_FETCH_ERROR }));
}
return null;
Expand Down

0 comments on commit a772813

Please sign in to comment.