axios 응답 200인데 ..? 에러같다

Jiwon·2023년 10월 28일
0

1. 상황

응답 200 인데..?
data가 이상하다.
명세서를 자세히 보니 0이어야 정상이라고 한다.
근데 아무리 해도 error: 2 가 나온다

코드

import { LoginFormValue } from "@Types/types";
import axios from "axios";
import { BASE_URL } from "utils/baseUrl";
export const axiosLoginPost = async (data: LoginFormValue) => {
  try {
    const res = await axios.post(`${BASE_URL}/api/v1/auth/login`, {
      email: data.email,
      password: data.password,
    });
    console.log(res);
    console.log(res.data);
  } catch (error) {
    alert(error);
  }
}; 

BASE_URL 도 맞고, 경로도 맞고,
똑같은 형식으로 회원가입에서는 error: 0 으로 응답이 잘만 됐었다.
뭐지..
200이면 다 성공아니었나..

2. 원인

내가 만났던 api들은 모두 200이면 성공이었는데,
이 api는 응답이 200이어도 res.data.error를 통해 구분하고 있었다.
그래서 안됐던 것.

  • res.data.error 가 0 이면 정상처리
  • res.data.error 가 2 이면 로그인 실패
    로 이해하고 로직을 수정했다.

3. 해결

코드를 아래과 같이 변경해주니 잘 처리가 되었다.

export const axiosLoginPost = async (data: LoginFormValue) => {
  try {
    const res = await axios.post(`${BASE_URL}/api/v1/auth/login`, {
      email: data.email,
      password: data.password,
    });
    if (res.data.error === 0) {
      return res.data.payload.user;
    } else if (res.data.error === 2) {
      alert(res.data.message);
    }
  } catch (error) {
    alert(error);
  }
};

콘솔 결과

profile
hi there~!

0개의 댓글