Skip to content

Commit

Permalink
Merge pull request #320 from MovieReviewComment/feature/issue-268/cle…
Browse files Browse the repository at this point in the history
…anup

[#268] Remove exceptions on reviews api handlers
  • Loading branch information
2wheeh authored Apr 12, 2024
2 parents c4a49b3 + 08b74ab commit 609b55b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 77 deletions.
79 changes: 32 additions & 47 deletions ui/src/lib/apis/review/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,48 @@ import type {
UpdateReviewRequest,
UpdateReviewResponse,
} from '@/lib/definitions/review';
import dummyReviewList from '@/lib/dummy/review';

export async function createReview(data: CreateReviewRequest): Promise<CreateReviewResponse> {
try {
const response = await fetch('/api/v1/reviews', {
method: 'POST',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json' },
});

if (!response.ok) {
const { messages } = (await response.json()) as HttpErrorResponse;

// TODO: build proper error message
console.error(
`status: ${response.status}\nstatusText: ${response.statusText}\nmessages:\n${messages.join('\n')}`
);

throw new Error('네트워크 오류가 발생했습니다. 다시 시도해주세요.');
}

return (await response.json()) as CreateReviewResponse;
} catch (error) {
// TODO: DELETE THIS WHEN API IS READY
return await new Promise((resolve) =>
setTimeout(() => resolve({ review: dummyReviewList.reviews[0] }), 500)
const response = await fetch('/api/v1/reviews', {
method: 'POST',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json' },
});

if (!response.ok) {
const { messages } = (await response.json()) as HttpErrorResponse;

// TODO: build proper error message
console.error(
`status: ${response.status}\nstatusText: ${response.statusText}\nmessages:\n${messages.join('\n')}`
);

throw new Error('네트워크 오류가 발생했습니다. 다시 시도해주세요.');
}

return (await response.json()) as CreateReviewResponse;
}

export async function updateReview(
id: number,
data: UpdateReviewRequest
): Promise<UpdateReviewResponse> {
try {
const response = await fetch(`/api/v1/reviews/${id}`, {
method: 'PUT',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json' },
});

if (!response.ok) {
const { messages } = (await response.json()) as HttpErrorResponse;

// TODO: build proper error message
console.error(
`status: ${response.status}\nstatusText: ${response.statusText}\nmessages:\n${messages.join('\n')}`
);

throw new Error('네트워크 오류가 발생했습니다. 다시 시도해주세요.');
}

return (await response.json()) as UpdateReviewResponse;
} catch (error) {
// TODO: DELETE THIS WHEN API IS READY
return await new Promise((resolve) =>
setTimeout(() => resolve({ review: dummyReviewList.reviews[Number(id) - 1] }), 500)
const response = await fetch(`/api/v1/reviews/${id}`, {
method: 'PUT',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json' },
});

if (!response.ok) {
const { messages } = (await response.json()) as HttpErrorResponse;

// TODO: build proper error message
console.error(
`status: ${response.status}\nstatusText: ${response.statusText}\nmessages:\n${messages.join('\n')}`
);

throw new Error('네트워크 오류가 발생했습니다. 다시 시도해주세요.');
}

return (await response.json()) as UpdateReviewResponse;
}
43 changes: 13 additions & 30 deletions ui/src/lib/apis/review/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
ListReviewsQuery,
ListReviewsResponse,
} from '@/lib/definitions/review';
import dummyReviewList from '@/lib/dummy/review';

export async function listReviews(query: ListReviewsQuery): Promise<ListReviewsResponse> {
noStore();
Expand All @@ -20,43 +19,27 @@ export async function listReviews(query: ListReviewsQuery): Promise<ListReviewsR
return acc;
}, new URLSearchParams());

try {
const response = await fetch(`${process.env.BACKEND_URL}/api/v1/reviews?${params.toString()}`);
const response = await fetch(`${process.env.BACKEND_URL}/api/v1/reviews?${params.toString()}`);

if (!response.ok) {
throw new Error(
`Network Error: Failed to fetch reviews list from BACKEND API: ${response.statusText}`
);
}

return (await response.json()) as ListReviewsResponse;
} catch (error) {
// TODO: handle error
// console.error('Fetch Error:', error);
// throw new Error('Internal Server Error: Failed to fetch reviews list');

// TODO: DELETE ME when reviews api works
return await new Promise((resolve) => setTimeout(() => resolve(dummyReviewList), 1000));
if (!response.ok) {
throw new Error(
`Network Error: Failed to fetch reviews list from BACKEND API: ${response.statusText}`
);
}

return (await response.json()) as ListReviewsResponse;
}

export async function getReview(id: string): Promise<GetReviewResponse> {
noStore();

try {
const response = await fetch(`${process.env.BACKEND_URL}/api/v1/reviews/${id}`);

if (!response.ok) {
throw new Error(
`Network Error: Failed to fetch review from BACKEND API: ${response.statusText}`
);
}
const response = await fetch(`${process.env.BACKEND_URL}/api/v1/reviews/${id}`);

return (await response.json()) as GetReviewResponse;
} catch (error) {
// TODO: DELETE ME when reviews api works
return await new Promise((resolve) =>
setTimeout(() => resolve({ review: dummyReviewList.reviews[Number(id) - 1] }), 1000)
if (!response.ok) {
throw new Error(
`Network Error: Failed to fetch review from BACKEND API: ${response.statusText}`
);
}

return (await response.json()) as GetReviewResponse;
}

0 comments on commit 609b55b

Please sign in to comment.