Skip to content

Commit

Permalink
fix(robot-server): Better errors for bad wpa2 pass (#17203)
Browse files Browse the repository at this point in the history
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" 
<img width="607" alt="Screenshot 2025-01-07 at 4 46 15 PM"
src="https://github.com/user-attachments/assets/d9006f01-37e8-4221-812a-900ffcb61960"
/>


## 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
  • Loading branch information
sfoster1 authored Jan 7, 2025
1 parent 696d2e4 commit eeb7a02
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
11 changes: 10 additions & 1 deletion robot-server/robot_server/service/legacy/routers/networking.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import re
import subprocess
from typing import Annotated, Optional

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions robot-server/tests/service/legacy/routers/test_networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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

0 comments on commit eeb7a02

Please sign in to comment.