인프런 커뮤니티 질문&답변
try catch 문에서 error handling 시에 'as' 대신 instance of 로 핸들링 할 수 있을까요?
해결된 질문
작성
·
711
0
안녕하세요. 제로초님
try catch 문에서 error handling 시에 typescript의 as로 타입의 assertion 대신 아래와 같이 instanceof로 잡는 것은 좋을 방법인지 질문드립니다.
try {
setLoading(true);
const response = await axios.post('/user', {
email,
name,
password,
});
Alert.alert('알림', '회원가입 완료');
} catch (error) {
if (error instanceof AxiosError) {
Alert.alert(
'네트워크 에러',
error.response?.data.message ?? '알수없는 에러가 발생했습니다',
);
}
} finally {
setLoading(false);
}





감사합니다 axios.isAxiosError 도 알아가겠습니다~