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

[ πŸ”§ ErrorHandling ] axios μ—λŸ¬ λ°œμƒ μ‹œ, isError κ°€ true κ°€ λ˜μ§€ μ•ŠμŒ #245

Open
nuuco opened this issue Oct 17, 2022 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@nuuco
Copy link
Collaborator

nuuco commented Oct 17, 2022

- μ–΄λ–€ λΆ€λΆ„μ—μ„œ λ¬Έμ œκ°€ λ°œμƒν–ˆλ‚˜μš”?

axios μ—λŸ¬ λ°œμƒ μ‹œ React Query의 isError ν”„λ‘œνΌν‹°κ°€ true κ°€ λ˜μ–΄μ•Ό ν•˜λŠ”λ°, 계속 false κ°€ λ‚˜μ˜¨λ‹€.

- μ—λŸ¬ 메세지

if(isError) {
   console.log("μ—λŸ¬ λ°œμƒ"); //톡신 μ—λŸ¬κ°€ λ°œμƒν•΄λ„ "μ—λŸ¬ λ°œμƒ"이 μ•ˆ 찍힘
}

- μ—λŸ¬ν•Έλ“€λ§ 방법

  • onError 와 isError λ₯Ό 같이 μ¨μ„œ λ°œμƒν•œ μ—λŸ¬μ˜€μŠ΅λ‹ˆλ‹€.
  • onErrorμ—μ„œ μ—λŸ¬ 처리 λ‘œμ§μ„ μ²˜λ¦¬ν•˜λ©΄ μ—λŸ¬κ°€ μ’…λ£Œλ©λ‹ˆλ‹€. 즉 isError κ°€ falseκ°€ λ©λ‹ˆλ‹€.
  • onError 둜 μ—λŸ¬ 처리λ₯Ό ν•  경우, isErrorλ₯Ό μ‚¬μš©ν•˜μ§€ 말고 onError μ•ˆμ—μ„œ state λ₯Ό μ„€μ •ν•΄ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  const { data, isLoading } = useQuery(
    ['roobits', roomId],
    () =>
      axios
        .get(`${process.env.REACT_APP_API_URL}/rooms/${roomId}`)
        .then((res) => res.data),
    {
      onError: (err) => {
        console.log('μš”μ²­ err', err);
        setErrPageOpen(true);
      }, //state 처리
    }
  );

- 문제 해결에 도움을 받은 레퍼런슀 링크

@nuuco nuuco added the bug Something isn't working label Oct 17, 2022
@nuuco nuuco self-assigned this Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant