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

feat: add ResponseError class #89

Merged
merged 5 commits into from
Dec 9, 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
31 changes: 28 additions & 3 deletions src/apis/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import { ErrorResponse } from '@interfaces/api';

export class ResponseError extends Error {
errorData: ErrorResponse;

constructor(errorData: ErrorResponse) {
super();
this.errorData = errorData;
}
}

class Fetch {
private baseURL: string;
private accessToken: string | undefined;
Expand All @@ -18,7 +29,15 @@ class Fetch {
return data;
}

async post<T>({ path, headers, body }: { path: string; headers?: HeadersInit; body: object }) {
async post<TData>({
path,
headers,
body,
}: {
path: string;
headers?: HeadersInit;
body: object;
}) {
const response = await fetch(`${this.baseURL}${path}`, {
method: 'POST',
headers: {
Expand All @@ -28,8 +47,14 @@ class Fetch {
},
body: JSON.stringify(body),
});
const data: T = await response.json();
return data;

const data = await response.json();

if (!response.ok) {
throw new ResponseError(data);
}

return data as TData;
}

setAccessToken(token: string) {
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useFunnel/useFunnel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const useFunnel = <Steps extends string[]>(

const FunnelComponent = Object.assign(
(props: Omit<FunnelProps<Steps>, 'steps' | 'step'>) => {
console.log('🚀 ~ file: useFunnel.tsx:21 ~ step:', step);
return <Funnel<Steps> steps={steps} step={step} {...props} />;
},
{ Step }
Expand Down
11 changes: 11 additions & 0 deletions src/interfaces/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface ErrorResponse {
abCode: string;
errorContent: ErrorContent;
}

interface ErrorContent {
message: string;
hint: string;
httpCode: number;
payload?: number;
}
18 changes: 13 additions & 5 deletions src/routes/Auth/kakao/KakaoLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { useNavigate } from 'react-router';

import { kakaoLogin } from '@apis/oauth/kakao';

import { ResponseError } from '@apis/fetch';

import { Container } from './KakaoLogin.styles';

const KakaoLogin = () => {
Expand All @@ -12,11 +14,17 @@ const KakaoLogin = () => {

const handleKakaoLogin = async () => {
if (kakaoCode) {
const response = await kakaoLogin(kakaoCode);

if (response.accessToken) {
/** TBD */
response.newMember ? navigate('/onboard') : navigate('/');
try {
const response = await kakaoLogin(kakaoCode);
if (response && response.accessToken) {
response.newMember ? navigate('/onboard') : navigate('/');
}
} catch (err) {
if (err instanceof ResponseError) {
if (err.errorData.abCode === 'ILLEGAL_JOIN_STATUS') {
navigate(`/signup`, { state: { memberId: err.errorData.errorContent.payload } });
}
}
}
} else {
throw new Error('code is invalid');
Expand Down
3 changes: 3 additions & 0 deletions src/routes/Auth/signup/Signup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from 'react';
import { useLocation } from 'react-router-dom';

import useFunnel from '@hooks/useFunnel/useFunnel';

Expand All @@ -7,6 +8,8 @@ import 약관동의 from './약관동의';
import 정보입력 from './정보입력';

const Signup = () => {
const { state } = useLocation();

const [registerData, setRegisterData] = useState();
const steps = ['정보입력', '약관동의', '가입성공'];
const [Funnel, setStep] = useFunnel(steps);
Expand Down