diff --git a/src/renderer/components/+network-port-forwards/port-forward-menu.tsx b/src/renderer/components/+network-port-forwards/port-forward-menu.tsx index c35be5a57c..5b49c3f85c 100644 --- a/src/renderer/components/+network-port-forwards/port-forward-menu.tsx +++ b/src/renderer/components/+network-port-forwards/port-forward-menu.tsx @@ -26,6 +26,7 @@ import { MenuActions, MenuActionsProps } from "../menu/menu-actions"; import { MenuItem } from "../menu"; import { Icon } from "../icon"; import { PortForwardDialog } from "../../port-forward"; +import { Notifications } from "../notifications"; interface Props extends MenuActionsProps { portForward: PortForwardItem; @@ -35,7 +36,13 @@ interface Props extends MenuActionsProps { export class PortForwardMenu extends React.Component { @boundMethod remove() { - return removePortForward(this.props.portForward); + const { portForward } = this.props; + + try { + removePortForward(portForward); + } catch (error) { + Notifications.error(`Error occurred stopping the port-forward from port ${portForward.forwardPort}. The port-forward may still be active.`); + } } renderContent() { diff --git a/src/renderer/components/+network-services/service-port-component.tsx b/src/renderer/components/+network-services/service-port-component.tsx index 3a679e56ae..6a95846e04 100644 --- a/src/renderer/components/+network-services/service-port-component.tsx +++ b/src/renderer/components/+network-services/service-port-component.tsx @@ -30,7 +30,6 @@ import { Notifications } from "../notifications"; import { Button } from "../button"; import { addPortForward, getPortForward, openPortForward, PortForwardDialog, portForwardStore, predictProtocol, removePortForward } from "../../port-forward"; import type { ForwardedPort } from "../../port-forward"; -import logger from "../../../common/logger"; import { Spinner } from "../spinner"; interface Props { @@ -47,21 +46,15 @@ export class ServicePortComponent extends React.Component { constructor(props: Props) { super(props); makeObservable(this); - this.init(); + this.checkExistingPortForwarding(); } componentDidMount() { disposeOnUnmount(this, [ - reaction(() => [ portForwardStore.portForwards, this.props.service ], () => this.init()), + reaction(() => [portForwardStore.portForwards, this.props.service], () => this.checkExistingPortForwarding()), ]); } - init() { - this.checkExistingPortForwarding().catch(error => { - logger.error(error); - }); - } - async checkExistingPortForwarding() { const { service, port } = this.props; const portForward: ForwardedPort = { @@ -71,7 +64,16 @@ export class ServicePortComponent extends React.Component { port: port.port, forwardPort: this.forwardPort, }; - const activePort = await getPortForward(portForward) ?? 0; + + let activePort: number; + + try { + activePort = await getPortForward(portForward) ?? 0; + } catch (error) { + this.isPortForwarded = false; + + return; + } this.forwardPort = activePort; this.isPortForwarded = activePort ? true : false; @@ -89,7 +91,6 @@ export class ServicePortComponent extends React.Component { }; this.waiting = true; - this.isPortForwarded = false; try { this.forwardPort = await addPortForward(portForward); @@ -100,7 +101,8 @@ export class ServicePortComponent extends React.Component { this.isPortForwarded = true; } } catch (error) { - Notifications.error(error); + Notifications.error("Error occurred starting port-forward, the local port may not be available"); + this.checkExistingPortForwarding(); } finally { this.waiting = false; } @@ -122,7 +124,8 @@ export class ServicePortComponent extends React.Component { await removePortForward(portForward); this.isPortForwarded = false; } catch (error) { - Notifications.error(error); + Notifications.error(`Error occurred stopping the port-forward from port ${portForward.forwardPort}.`); + this.checkExistingPortForwarding(); } finally { this.waiting = false; } diff --git a/src/renderer/components/+workloads-pods/pod-container-port.tsx b/src/renderer/components/+workloads-pods/pod-container-port.tsx index 7dc5fe7618..19f0c5446b 100644 --- a/src/renderer/components/+workloads-pods/pod-container-port.tsx +++ b/src/renderer/components/+workloads-pods/pod-container-port.tsx @@ -30,7 +30,6 @@ import { Notifications } from "../notifications"; import { Button } from "../button"; import { addPortForward, getPortForward, openPortForward, PortForwardDialog, portForwardStore, predictProtocol, removePortForward } from "../../port-forward"; import type { ForwardedPort } from "../../port-forward"; -import logger from "../../../common/logger"; import { Spinner } from "../spinner"; interface Props { @@ -51,21 +50,15 @@ export class PodContainerPort extends React.Component { constructor(props: Props) { super(props); makeObservable(this); - this.init(); + this.checkExistingPortForwarding(); } componentDidMount() { disposeOnUnmount(this, [ - reaction(() => [ portForwardStore.portForwards, this.props.pod ], () => this.init()), + reaction(() => [portForwardStore.portForwards, this.props.pod], () => this.checkExistingPortForwarding()), ]); } - init() { - this.checkExistingPortForwarding().catch(error => { - logger.error(error); - }); - } - async checkExistingPortForwarding() { const { pod, port } = this.props; const portForward: ForwardedPort = { @@ -75,7 +68,16 @@ export class PodContainerPort extends React.Component { port: port.containerPort, forwardPort: this.forwardPort, }; - const activePort = await getPortForward(portForward) ?? 0; + + let activePort: number; + + try { + activePort = await getPortForward(portForward) ?? 0; + } catch (error) { + this.isPortForwarded = false; + + return; + } this.forwardPort = activePort; this.isPortForwarded = activePort ? true : false; @@ -93,7 +95,6 @@ export class PodContainerPort extends React.Component { }; this.waiting = true; - this.isPortForwarded = false; try { this.forwardPort = await addPortForward(portForward); @@ -104,7 +105,8 @@ export class PodContainerPort extends React.Component { this.isPortForwarded = true; } } catch (error) { - Notifications.error(error); + Notifications.error("Error occurred starting port-forward, the local port may not be available"); + this.checkExistingPortForwarding(); } finally { this.waiting = false; } @@ -126,7 +128,8 @@ export class PodContainerPort extends React.Component { await removePortForward(portForward); this.isPortForwarded = false; } catch (error) { - Notifications.error(error); + Notifications.error(`Error occurred stopping the port-forward from port ${portForward.forwardPort}.`); + this.checkExistingPortForwarding(); } finally { this.waiting = false; } diff --git a/src/renderer/port-forward/port-forward-dialog.tsx b/src/renderer/port-forward/port-forward-dialog.tsx index b577e99e71..3c1306fe33 100644 --- a/src/renderer/port-forward/port-forward-dialog.tsx +++ b/src/renderer/port-forward/port-forward-dialog.tsx @@ -112,7 +112,7 @@ export class PortForwardDialog extends Component { openPortForward(portForward); } } catch (err) { - Notifications.error(err); + Notifications.error("Error occurred starting port-forward, the local port may not be available"); } finally { close(); } diff --git a/src/renderer/port-forward/port-forward.store.ts b/src/renderer/port-forward/port-forward.store.ts index 5b91b2e5ae..9c90dcf6a0 100644 --- a/src/renderer/port-forward/port-forward.store.ts +++ b/src/renderer/port-forward/port-forward.store.ts @@ -113,7 +113,8 @@ export async function addPortForward(portForward: ForwardedPort): Promise(`/pods/port-forward/${portForward.namespace}/${portForward.kind}/${portForward.name}?port=${portForward.port}&forwardPort=${portForward.forwardPort}${getProtocolQuery(portForward.protocol)}`); } catch (error) { - logger.warn(error); // don't care, caller must check + logger.warn("Error getting port-forward:", error, portForward); + throw(error); } return response?.port; @@ -142,14 +144,11 @@ export async function getPortForward(portForward: ForwardedPort): Promise { let port = 0; + + await removePortForward(portForward); + portForward.forwardPort = desiredPort; + port = await addPortForward(portForward); - try { - await removePortForward(portForward); - portForward.forwardPort = desiredPort; - port = await addPortForward(portForward); - } catch (error) { - logger.warn(error); // don't care, caller must check - } portForwardStore.reset(); return port; @@ -161,7 +160,8 @@ export async function removePortForward(portForward: ForwardedPort) { await apiBase.del(`/pods/port-forward/${portForward.namespace}/${portForward.kind}/${portForward.name}?port=${portForward.port}&forwardPort=${portForward.forwardPort}`); await waitUntilFree(+portForward.forwardPort, 200, 1000); } catch (error) { - logger.warn(error); // don't care, caller must check + logger.warn("Error removing port-forward:", error, portForward); + throw(error); } portForwardStore.reset(); } @@ -172,8 +172,8 @@ export async function getPortForwards(): Promise { return response.portForwards; } catch (error) { - logger.warn(error); // don't care, caller must check - + logger.warn("Error getting all port-forwards:", error); + return []; } }