1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Improve error handling and reporting in setup-lens-proxy

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-01-13 11:07:16 -05:00
parent de1e3919eb
commit 73469c4833

View File

@ -12,68 +12,80 @@ import { beforeApplicationIsLoadingInjectionToken } from "../runnable-tokens/bef
import buildVersionInjectable from "../../vars/build-version/build-version.injectable"; import buildVersionInjectable from "../../vars/build-version/build-version.injectable";
import initializeBuildVersionInjectable from "../../vars/build-version/init.injectable"; import initializeBuildVersionInjectable from "../../vars/build-version/init.injectable";
import lensFetchInjectable from "../../../common/fetch/lens-fetch.injectable"; import lensFetchInjectable from "../../../common/fetch/lens-fetch.injectable";
import initAuthHeaderStateInjectable from "../../../features/auth-header/main/init-state.injectable";
import { hasTypedProperty, isObject, isString, json } from "../../../common/utils";
const setupLensProxyInjectable = getInjectable({ const setupLensProxyInjectable = getInjectable({
id: "setup-lens-proxy", id: "setup-lens-proxy",
instantiate: (di) => { instantiate: (di) => ({
const lensProxy = di.inject(lensProxyInjectable); id: "setup-lens-proxy",
const exitApp = di.inject(exitAppInjectable); run: async () => {
const logger = di.inject(loggerInjectable); const lensProxy = di.inject(lensProxyInjectable);
const isWindows = di.inject(isWindowsInjectable); const exitApp = di.inject(exitAppInjectable);
const showErrorPopup = di.inject(showErrorPopupInjectable); const logger = di.inject(loggerInjectable);
const buildVersion = di.inject(buildVersionInjectable); const isWindows = di.inject(isWindowsInjectable);
const lensFetch = di.inject(lensFetchInjectable); const showErrorPopup = di.inject(showErrorPopupInjectable);
const buildVersion = di.inject(buildVersionInjectable);
const lensFetch = di.inject(lensFetchInjectable);
return { const showProxyError = (error: string) => {
id: "setup-lens-proxy", const hostsPath = isWindows
run: async () => { ? "C:\\windows\\system32\\drivers\\etc\\hosts"
try { : "/etc/hosts";
logger.info("🔌 Starting LensProxy"); const message = [
await lensProxy.listen(); // lensProxy.port available `Failed connection test: ${error}`,
} catch (error) { "Check to make sure that no other versions of Lens are running",
const message = error instanceof Error ? error.message : "unknown error"; `Check ${hostsPath} to make sure that it is clean and that the localhost loopback is at the top and set to 127.0.0.1`,
"If you have HTTP_PROXY or http_proxy set in your environment, make sure that the localhost and the ipv4 loopback address 127.0.0.1 are added to the NO_PROXY environment variable.",
].join("\n\n");
showErrorPopup("Lens Error", `Could not start proxy: ${message}`); logger.error(`🛑 LensProxy: failed connection test: ${error}`);
showErrorPopup("Lens Proxy Error", message);
exitApp();
};
return exitApp(); try {
} logger.info("🔌 Starting LensProxy");
await lensProxy.listen(); // lensProxy.port available
} catch (error) {
const message = error instanceof Error ? error.message : "unknown error";
// test proxy connection showErrorPopup("Lens Error", `Could not start proxy: ${message}`);
try { exitApp();
logger.info("🔎 Testing LensProxy connection ...");
const versionResponse = await lensFetch("/version");
const { version: versionFromProxy } = await versionResponse.json() as { version: string }; return;
}
if (buildVersion.get() !== versionFromProxy) { logger.info("🔎 Testing LensProxy connection ...");
logger.error("Proxy server responded with invalid response"); const versionResponse = await lensFetch("/version");
return exitApp(); if (versionResponse.status !== 200) {
} return showProxyError(`failed to GET /version: ${versionResponse.statusText}`);
}
logger.info("⚡ LensProxy connection OK"); const body = await versionResponse.text();
} catch (error) { const { isOk, value, error } = json.parse(body);
logger.error(`🛑 LensProxy: failed connection test: ${error}`);
const hostsPath = isWindows if (!isOk) {
? "C:\\windows\\system32\\drivers\\etc\\hosts" return showProxyError(`failed to parse response body ${error.cause} for "${error.text}"`);
: "/etc/hosts"; }
const message = [
`Failed connection test: ${error}`,
"Check to make sure that no other versions of Lens are running",
`Check ${hostsPath} to make sure that it is clean and that the localhost loopback is at the top and set to 127.0.0.1`,
"If you have HTTP_PROXY or http_proxy set in your environment, make sure that the localhost and the ipv4 loopback address 127.0.0.1 are added to the NO_PROXY environment variable.",
];
showErrorPopup("Lens Proxy Error", message.join("\n\n")); if (!isObject(value) || !hasTypedProperty(value, "version", isString)) {
return showProxyError(`invalid data returned: "${body}"`);
}
return exitApp(); if (buildVersion.get() !== value.version) {
} return showProxyError("Proxy server response with unexpeced version");
}, }
runAfter: di.inject(initializeBuildVersionInjectable),
}; logger.info("⚡ LensProxy connection OK");
}, },
runAfter: [
di.inject(initializeBuildVersionInjectable),
di.inject(initAuthHeaderStateInjectable),
],
}),
injectionToken: beforeApplicationIsLoadingInjectionToken, injectionToken: beforeApplicationIsLoadingInjectionToken,
}); });