티스토리 뷰
개요
프론트엔드 개발을 하면서 정상적으로 동작하는 UI 외에 에러가 발생했을 때 적절한 UI 및 페이지 이동을 통해 더 안정적이고 사용성을 고려한 서비스 개발이 필요 했습니다.
가장먼저, Error Boundary를 도입하고자 하였습니다.
https://ko.legacy.reactjs.org/docs/error-boundaries.html
다음 링크를 통해 간략한 에러발생 시나리오를 파악해보면
- 하위 컴포넌트에서 에러발생
- getDerivedFromError에서 fallback UI가 보이도록 설정해주고
- componentDidCatch를 통해 logging을 지원합니다.
다음은 링크를 참고하여 간략하게 작성한 APIErrorBoundary 코드 입니다.
// ApiErrorBoundary.tsx
import React, { PropsWithChildren } from 'react';
import { NetworkError, UnknownError } from '@/components';
type AuthErrorBoundaryState = {
hasError: boolean;
};
export default class ApiErrorBoundary extends React.Component<
PropsWithChildren<ApiErrorBoundaryProps>,
AuthErrorBoundaryState
> {
constructor(props: ApiErrorBoundaryProps) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError() {
return { hasError: true };
}
// Todo:connect monitoring api
componentDidCatch(): void {}
render() {
if (!this.state.hasError) {
return this.props.children;
}
if (this.props.error?.type === 'network') {
return <NetworkError />;
}
return <UnknownError />;
}
}
// UI.tsx
export default function TodayChallenge({ }: Props) {
return (
<ApiErrorBoundary> // ErrorBoundary 설정
<Fetcher>
<Container />
</Fetcher>
</ApiErrorBoundary>
);
}
그러나…
ErrorBoundary에 포착되지 않는 에러들이 있습니다.
- 이벤트 핸들러 (더 알아보기)
- 비동기적 코드 (예: setTimeout 혹은 requestAnimationFrame 콜백)
- 서버 사이드 렌더링
- 자식에서가 아닌 에러 경계 자체에서 발생하는 에러
Next.js를 사용하기 때문에 서버사이드에서의 에러처리를 따로 진행을 해주어야 했습니다.
저의 경우 yceffort님의 정리해주신 자료를 참고하여 코드를 작성하였습니다.
https://yceffort.kr/2021/10/api-error-handling-nextjs
next.js의 redirect관련해선 다음 링크를 참고 해주세요.
https://nextjs.org/docs/app/api-reference/functions/redirect.
import { GetServerSideProps, GetServerSidePropsContext } from 'next';
import { isInstanceOfAPIError } from '@/error';
export default function withGetServerSideProps(getServerSideProps: GetServerSideProps): GetServerSideProps {
return async (context: GetServerSidePropsContext) => {
try {
const result = await getServerSideProps(context);
return result;
} catch (error) {
if (isInstanceOfAPIError(error)) {
const { redirectUrl, notFound } = error;
if (notFound) {
return {
notFound: true,
};
}
return {
redirect: {
destination: redirectUrl,
permanent: false,
},
};
}
// handle on the client
return {
props: {},
},
};
}
};
}
- redirect나 notFound의 값을 갖고 있는 Error 타입의 경우 특정 주소로 redirect할 수 있게 설정했습니다.
- 클라이언트에서 처리하려는 경우 서버에서 에러가 나지않게 빈 객체를 보내주었습니다.
만약 getInitialProps를 사용하는 경우에는 다음 링크를 확인해주세요
https://stackoverflow.com/questions/59746476/redirecting-from-getinitialprops-in-error-js-in-nextjs
마지막으로..
// Container.tsx
const { error, isError } = useFetch();
if (isError) {
throw error;
}
container component에서 에러가 발생하면 throw 하고 ErrorBoundary에 포착되어 필요한 UI를 보여주어 대응할 수 있도록 합니다.
참고
getInitialProps에서 redirect 작성하기
https://stackoverflow.com/questions/59746476/redirecting-from-getinitialprops-in-error-js-in-nextjs
에러 핸들링 참고 아티클
https://yceffort.kr/2021/10/api-error-handling-nextjs
nextjs redirect
https://nextjs.org/docs/app/api-reference/functions/redirect
'사이드 프로젝트' 카테고리의 다른 글
코드 스플리팅과 Tree shaking (1) | 2023.12.06 |
---|---|
Nextjs error boundary 적용하기 (0) | 2023.09.06 |
[nextjs/eslint] next/babel parsing error 해결하기 (0) | 2023.07.17 |
- Total
- Today
- Yesterday
- React useMemo
- 백준 22862
- storybook react is not defiend 해결
- 가장 긴 짝수 연속한 부분 수열
- nextjs errorboundary
- storybook scss이슈
- serverless 배포
- useCallback과 useMemo 사용
- react suspense
- CSS
- suspense 장점
- 불량 사용자 자바스크립트
- 자바스크립트
- 미로탈출 명령어
- nextjs 에러핸들링
- React useCallback
- storybook scss import
- 서버사이드 error handling
- javascript
- 표현 가능한 이진트리
- node version yarn berry
- 서비스 디자인 패턴
- 백준 1600번
- node 버전 마이그레이션
- serverless nestjs
- 1600 파이썬
- 에러핸들링
- 관심사 분리하기
- 선언적 UI
- nestjs 배포하기
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |