관로 분석 재질별, 경과년도&재질별 조회 오류 해결

This commit is contained in:
kiminchan
2026-06-04 15:54:32 +09:00
parent af8db9457f
commit cc91225b01
5 changed files with 161 additions and 19 deletions
+38 -1
View File
@@ -29,6 +29,28 @@ const withSiteIdHeaders = (headers: HeadersInit = {}) => ({
const resolveApiUrl = (url: string) =>
url.startsWith('/') ? `${url}` : `${BASE_URL}/${url}`;
const buildRequestError = (
fallbackMessage: string,
status?: number,
body?: unknown,
) => {
const bodyMessage =
typeof body === "object" && body !== null
? (
(body as { message?: unknown }).message
?? (body as { error?: { message?: unknown } }).error?.message
)
: undefined;
const message =
typeof bodyMessage === "string" && bodyMessage.trim()
? bodyMessage
: fallbackMessage;
const error = new Error(message) as Error & { status?: number; body?: unknown };
error.status = status;
error.body = body;
return error;
};
const handleUnauthorizedResponse = () => {
if (typeof window === "undefined") return;
@@ -79,7 +101,22 @@ export const getData = async (url: string): Promise<any> => {
throw new Error('Invalid JSON response from server');
}
}
throw new Error('Failed to fetch data');
const text = await res.text().catch(() => "");
let body: unknown = null;
if (text.trim()) {
try {
body = JSON.parse(text);
} catch {
body = text;
}
}
throw buildRequestError(
`Failed to fetch data: ${res.status} ${res.statusText}`,
res.status,
body,
);
};
export const postData = async (url: string, data: object): Promise<any> => {