-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: server action의 에러 status code를 클라이언트에 전달
- Loading branch information
Showing
7 changed files
with
46 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ on: | |
push: | ||
branches: | ||
- main | ||
- qa | ||
- qa2 | ||
|
||
jobs: | ||
build: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// https://github.com/vercel/next.js/discussions/49506 | ||
|
||
'use server'; | ||
|
||
import { isDynamicUsageError } from 'next/dist/export/helpers/is-dynamic-usage-error'; | ||
|
||
import { CustomError } from '@/utils/serverActionError'; | ||
|
||
export const withErrorHandler = <T extends Array<unknown>, U>(fn: (...args: T) => Promise<U>) => { | ||
return async (...args: T): Promise<U | CustomError> => { | ||
try { | ||
return await fn(...args); | ||
} catch (e) { | ||
if (isDynamicUsageError(e)) throw e; | ||
|
||
return { error: { message: (e as Error).message } }; | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,7 @@ | ||
export const BASE_URL = 'https://cse-dev-waffle.bacchus.io/api/v1'; | ||
|
||
export class NetworkError extends Error { | ||
statusCode: number; | ||
constructor(statusCode: number) { | ||
super(`${statusCode} 에러`); | ||
this.statusCode = statusCode; | ||
} | ||
} | ||
|
||
export const checkError = (response: Response) => { | ||
if (response.ok) return; | ||
throw new NetworkError(response.status); | ||
// server action 에러 처리를 위해 status code만 깔끔하게 담음 | ||
throw new Error(response.status.toString()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export type CustomError = { error: { message: string } }; | ||
|
||
export const isApiError = <T>(response: T | CustomError): response is CustomError => { | ||
return typeof response === 'object' && response !== null && 'error' in response; | ||
}; | ||
|
||
export function throwIfError<T>(response: T): asserts response is Exclude<T, CustomError> { | ||
if (isApiError(response)) { | ||
throw new Error(response.error.message); | ||
} | ||
} | ||
|
||
export function handleServerAction<T>(response: T): Exclude<T, CustomError> { | ||
throwIfError(response); | ||
return response; | ||
} |