Skip to content

Commit

Permalink
[SWM-360] Fix : SESSION_ERROR handling
Browse files Browse the repository at this point in the history
  • Loading branch information
oikkoikk committed Oct 11, 2023
1 parent bfe8afd commit 8bc8c91
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 42 deletions.
13 changes: 7 additions & 6 deletions src/app/@modal/(.)search/[lecture_code]/error.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
'use client';
import { API_FETCH_ERROR, ErrorMessage } from '@/src/api/ErrorMessage';
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';
import { useEffect } from 'react';

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

useEffect(() => {
if (error.cause === API_FETCH_ERROR) {
setErrorToast(error);
router.back();
} else if (error.cause === SESSION_ERROR) {
setErrorToast(error);
await logout();
} else {
setErrorToast(new Error(ErrorMessage.DEFAULT));
}
return () => router.refresh();
}, [error, router]);

return null;
}
27 changes: 16 additions & 11 deletions src/app/dashboard/error.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
'use client';
import { API_FETCH_ERROR, ErrorMessage } from '@/src/api/ErrorMessage';
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';
import { useEffect } from 'react';

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

useEffect(() => {
if (error.cause === API_FETCH_ERROR) {
setErrorToast(error);
} else {
setErrorToast(new Error(ErrorMessage.DEFAULT));
}
return () => router.refresh();
}, [error, router]);
if (error.cause === API_FETCH_ERROR) {
setErrorToast(error);
} else if (error.cause === SESSION_ERROR) {
setErrorToast(error);
await logout();
} else {
setErrorToast(new Error(ErrorMessage.DEFAULT));
}

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

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

useEffect(() => {
if (error.cause === API_FETCH_ERROR) {
setErrorToast(error);
router.back();
} else {
setErrorToast(new Error(ErrorMessage.DEFAULT));
}
return () => router.refresh();
}, [error, router]);
if (error.cause === API_FETCH_ERROR) {
setErrorToast(error);
router.back();
} else if (error.cause === SESSION_ERROR) {
setErrorToast(error);
await logout();
} else {
setErrorToast(new Error(ErrorMessage.DEFAULT));
}

return null;
}
31 changes: 18 additions & 13 deletions src/app/search/[lecture_code]/error.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
'use client';
import { API_FETCH_ERROR, ErrorMessage } from '@/src/api/ErrorMessage';
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';
import { useEffect } from 'react';

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

useEffect(() => {
if (error.cause === API_FETCH_ERROR) {
setErrorToast(error);
router.replace('/');
router.refresh();
} else {
setErrorToast(new Error(ErrorMessage.DEFAULT));
}
return () => router.refresh();
}, [error, router]);
if (error.cause === API_FETCH_ERROR) {
setErrorToast(error);
router.replace('/');
router.refresh();
} else if (error.cause === SESSION_ERROR) {
setErrorToast(error);
await logout();
} else {
setErrorToast(new Error(ErrorMessage.DEFAULT));
}

return null;
}

0 comments on commit 8bc8c91

Please sign in to comment.