From eeb7a02634436982e87290319ca455acb4a9a2a4 Mon Sep 17 00:00:00 2001 From: Seth Foster Date: Tue, 7 Jan 2025 16:47:35 -0500 Subject: [PATCH] fix(robot-server): Better errors for bad wpa2 pass (#17203) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We pipe errors from nmcli straight to the display when we get them. NMCLI has famously awful errors. We don't want to always squash them, because they could be useful, but in the case of a mis-entered WPA2 PSK, we can be pretty sure that's the problem. This will now say "check your wifi credentials" Screenshot 2025-01-07 at 4 46 15 PM ## Testing - [x] Connect to a wifi network and purposefully enter the wrong password; you should get a less awful message. Note that this only actually works if you're disconnected from wifi when you try to connect; if you're connected to wifi and then try and connect to a network and use the wrong password, you just don't get a result because the request went out over the original wifi network which got disconnected. It's the same code on the flex, ot-2 on the desktop (the ODD has a different message). Closes RSQ-3 --- .../service/legacy/routers/networking.py | 11 ++++++++- .../service/legacy/routers/test_networking.py | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/robot-server/robot_server/service/legacy/routers/networking.py b/robot-server/robot_server/service/legacy/routers/networking.py index ea3c4543ea8..d0acb1b64be 100644 --- a/robot-server/robot_server/service/legacy/routers/networking.py +++ b/robot-server/robot_server/service/legacy/routers/networking.py @@ -1,5 +1,6 @@ import logging import os +import re import subprocess from typing import Annotated, Optional @@ -96,6 +97,13 @@ async def get_wifi_networks( return WifiNetworks(list=[WifiNetworkFull(**n) for n in networks]) +def _massage_nmcli_error(error_string: str) -> str: + """Raises a better-formatted error message from an nmcli error string.""" + if re.search("password.*802-11-wireless-security\\.psk.*not given", error_string): + return "Could not connect to network. Please double-check network credentials." + return error_string + + @router.post( path="/wifi/configure", summary="Configure the robot's Wi-Fi", @@ -129,7 +137,8 @@ async def post_wifi_configure( if not ok: raise LegacyErrorResponse( - message=message, errorCode=ErrorCodes.GENERAL_ERROR.value.code + message=_massage_nmcli_error(message), + errorCode=ErrorCodes.GENERAL_ERROR.value.code, ).as_error(status.HTTP_401_UNAUTHORIZED) return WifiConfigurationResponse(message=message, ssid=configuration.ssid) diff --git a/robot-server/tests/service/legacy/routers/test_networking.py b/robot-server/tests/service/legacy/routers/test_networking.py index 22ea2359a92..ebc42a41839 100755 --- a/robot-server/tests/service/legacy/routers/test_networking.py +++ b/robot-server/tests/service/legacy/routers/test_networking.py @@ -5,6 +5,7 @@ import pytest from opentrons.system import nmcli, wifi +from robot_server.service.legacy.routers.networking import _massage_nmcli_error from typing import Optional @@ -369,3 +370,25 @@ def check_option(opt_dict): assert "options" in opt for method_opt in opt["options"]: check_option(method_opt) + + +@pytest.mark.parametrize( + "nmcli_message,result_message", + [ + ( + "Warning: password for '802-11-wireless-security.psk' not given in 'passwd-file' and nmcli cannot ask without '--ask' option. Error: Connection activation failed: Secrets were required, but not provided Hint: use 'journalctl -xe NM_CONNECTION=05d784ec-1feb4147-be22-c07d7915ef96 + NM_DEVICE=mlan0' to get more details.", + "Could not connect to network. Please double-check network credentials.", + ), + ( + "Warning: asdasdasff for '802-11-afasda' not given in 'asdadsa'. Error: Connection activation failed: Secrets were required, but not provided", + "Warning: asdasdasff for '802-11-afasda' not given in 'asdadsa'. Error: Connection activation failed: Secrets were required, but not provided", + ), + ( + "Error: Connection activation failed: Secrets were required, but not provided", + "Error: Connection activation failed: Secrets were required, but not provided", + ), + ], +) +def test_error_rewriting(nmcli_message: str, result_message: str) -> None: + """It should rewrite known nmcli failure messages.""" + assert _massage_nmcli_error(nmcli_message) == result_message