refactor: refine the error message

This commit is contained in:
Kevin Yue 2023-06-25 01:37:34 +08:00
parent e5fd2ba280
commit e8db014336
2 changed files with 10 additions and 8 deletions

View File

@ -45,7 +45,7 @@ class GatewayService {
}); });
if (!response.ok) { if (!response.ok) {
throw new Error("Login failed"); throw new Error(`Gateway login failed: ${response.status}`);
} }
return this.parseLoginResponse(response.data); return this.parseLoginResponse(response.data);

View File

@ -36,8 +36,9 @@ export type PortalCredential = {
class PortalService { class PortalService {
async prelogin(portal: string): Promise<Prelogin> { async prelogin(portal: string): Promise<Prelogin> {
const preloginUrl = `https://${portal}/global-protect/prelogin.esp`; const preloginUrl = `https://${portal}/global-protect/prelogin.esp`;
let response;
try { try {
const response = await fetch<string>(preloginUrl, { response = await fetch<string>(preloginUrl, {
method: "POST", method: "POST",
headers: { headers: {
"User-Agent": "PAN GlobalProtect", "User-Agent": "PAN GlobalProtect",
@ -57,14 +58,15 @@ class PortalService {
// "host-id": "TODO, mac address?", // "host-id": "TODO, mac address?",
}), }),
}); });
} catch (err) {
console.error("Failed to prelogin: Network error", err);
throw new Error("Failed to prelogin: Network error");
}
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to prelogin: ${response.status}`); throw new Error(`Failed to prelogin: ${response.status}`);
} }
return this.parsePrelogin(response.data); return this.parsePrelogin(response.data);
} catch (err) {
throw new Error(`Failed to prelogin: Network error`);
}
} }
private parsePrelogin(response: string): Prelogin { private parsePrelogin(response: string): Prelogin {