Forráskód Böngészése

chore(utils): 优化接口请求函数,接口异常时显示详细错误

wangcong 3 hónapja
szülő
commit
10c9256ab6
1 módosított fájl, 3 hozzáadás és 1 törlés
  1. 3 1
      src/utils/index.ts

+ 3 - 1
src/utils/index.ts

@@ -47,7 +47,9 @@ export const request = async <T>(url: string, init: RequestInit = {}, timeout?:
   }
 
   if (!res.ok || res.status !== 200) {
-    throw new Error(res.statusText);
+    const json = await res.json();
+    const errMsg: string = json.msg || json.error || '';
+    throw new Error(`HTTP Error: ${res.status} ${errMsg}`);
   }
 
   // if (res.url.includes('/Uboxlogin')) {