Skip to content

Commit 01c15d5

Browse files
committed
feat: fetcher에서 실패 응답 시 HttpError로 명확하게 예외 처리
1 parent 419401e commit 01c15d5

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/api/fetcher.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { buildQueryParams } from '@/shared/utils/url';
22
import { ENV } from './env';
3+
import { HttpError } from '@/shared/errors/HttpError';
34

45
type FetcherOptions<T> = {
56
path: string;
@@ -77,9 +78,8 @@ const request = async <T>({
7778
const response = await fetch(url, config);
7879

7980
if (!response.ok) {
80-
// const errorData = await response.json().catch(() => null);
81-
// throw new HttpError(response.status, errorData);
82-
throw new Error();
81+
const errorData = await response.json().catch(() => null);
82+
throw new HttpError(response.status, errorData);
8383
}
8484

8585
if (response.status === 204 || response.headers.get('content-length') === '0') {

src/shared/errors/HttpError.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class HttpError extends Error {
2+
status: number;
3+
data: unknown;
4+
5+
constructor(status: number, data?: unknown) {
6+
super(`HTTP Error ${status}`);
7+
this.name = 'HttpError';
8+
this.status = status;
9+
this.data = data;
10+
}
11+
}

0 commit comments

Comments
 (0)