]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools: ynl: rework the string representation of NlError
authorJakub Kicinski <kuba@kernel.org>
Mon, 27 Oct 2025 19:29:58 +0000 (12:29 -0700)
committerJakub Kicinski <kuba@kernel.org>
Tue, 28 Oct 2025 23:35:06 +0000 (16:35 -0700)
In early days of YNL development dumping the NlMsg on errors
was quite useful, as the library itself could have been buggy.
These days increasingly the NlMsg is just taking up screen space
and means nothing to a typical user. Try to format the errors
more in line with how YNL C formats its errors strings.

Before:
  $ ynl --family ethtool  --do channels-set  --json '{}'
  Netlink error: Invalid argument
  nl_len = 44 (28) nl_flags = 0x300 nl_type = 2
error: -22
extack: {'miss-type': 'header'}

  $ ynl --family ethtool  --do channels-set  --json '{..., "tx-count": 999}'
  Netlink error: Invalid argument
  nl_len = 88 (72) nl_flags = 0x300 nl_type = 2
error: -22
extack: {'msg': 'requested channel count exceeds maximum', 'bad-attr': '.tx-count'}

After:
  $ ynl --family ethtool  --do channels-set  --json '{}'
  Netlink error: Invalid argument {'miss-type': 'header'}

  $ ynl --family ethtool  --do channels-set  --json '{..., "tx-count": 999}'
  Netlink error: requested channel count exceeds maximum: Invalid argument {'bad-attr': '.tx-count'}

Reviewed-by: Donald Hunter <donald.hunter@gmail.com>
Link: https://patch.msgid.link/20251027192958.2058340-2-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
tools/net/ynl/pyynl/lib/ynl.py

index bdcc4f031d39430f90de3253f51020c538b03f5a..225baad3c8f8beddcb65c59146cf3ee2ef78f59b 100644 (file)
@@ -105,7 +105,16 @@ class NlError(Exception):
         self.error = -nl_msg.error
 
     def __str__(self):
-        return f"Netlink error: {os.strerror(self.error)}\n{self.nl_msg}"
+        msg = "Netlink error: "
+
+        extack = self.nl_msg.extack.copy() if self.nl_msg.extack else {}
+        if 'msg' in extack:
+            msg += extack['msg'] + ': '
+            del extack['msg']
+        msg += os.strerror(self.error)
+        if extack:
+            msg += ' ' + str(extack)
+        return msg
 
 
 class ConfigError(Exception):