41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
/**
|
|
* 에러 메시지를 안전하게 문자열로 변환하는 유틸리티
|
|
*/
|
|
|
|
/**
|
|
* API 에러 객체를 안전한 문자열로 변환
|
|
* @param error - API 응답 에러 (string | object | undefined)
|
|
* @param defaultMessage - 기본 에러 메시지
|
|
* @returns 표시 가능한 에러 메시지 문자열
|
|
*/
|
|
export function formatErrorMessage(error: any, defaultMessage: string = "오류가 발생했습니다."): string {
|
|
// undefined/null 체크
|
|
if (!error) {
|
|
return defaultMessage;
|
|
}
|
|
|
|
// 이미 문자열인 경우
|
|
if (typeof error === "string") {
|
|
return error;
|
|
}
|
|
|
|
// 객체인 경우
|
|
if (typeof error === "object") {
|
|
// { code, details } 형태
|
|
if (error.details) {
|
|
return typeof error.details === "string" ? error.details : defaultMessage;
|
|
}
|
|
|
|
// { message } 형태
|
|
if (error.message) {
|
|
return typeof error.message === "string" ? error.message : defaultMessage;
|
|
}
|
|
|
|
// 기타 객체는 기본 메시지 반환
|
|
return defaultMessage;
|
|
}
|
|
|
|
// 그 외의 경우
|
|
return defaultMessage;
|
|
}
|