]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
suricatasc: add line numbers in error messages
authorShivani Bhardwaj <shivani@oisf.net>
Fri, 23 Jun 2023 06:27:21 +0000 (11:57 +0530)
committerVictor Julien <vjulien@oisf.net>
Tue, 27 Jun 2023 07:33:49 +0000 (09:33 +0200)
python/suricata/sc/suricatasc.py

index b1a8d7f41eaf44b536f960a42b9d15ee4da18ab6..d5f6f98f0e070b55cad2c9f2bba2a887275e988c 100644 (file)
@@ -22,6 +22,7 @@ import readline
 import select
 import sys
 from socket import AF_UNIX, error, socket
+from inspect import currentframe
 
 from suricata.sc.specs import argsd
 
@@ -30,6 +31,11 @@ VERSION = "0.2"
 INC_SIZE = 1024
 
 
+def get_linenumber():
+    cf = currentframe()
+    return cf.f_back.f_lineno
+
+
 class SuricataException(Exception):
     """
     Generic class for suricatasc exception
@@ -136,7 +142,7 @@ class SuricataSC:
 
     def send_command(self, command, arguments=None):
         if command not in self.cmd_list and command != 'command-list':
-            raise SuricataCommandException("Command not found: {}".format(command))
+            raise SuricataCommandException("L{}: Command not found: {}".format(get_linenumber(), command))
 
         cmdmsg = {}
         cmdmsg['command'] = command
@@ -156,7 +162,7 @@ class SuricataSC:
         else:
             cmdret = None
         if not cmdret:
-            raise SuricataReturnException("Unable to get message from server")
+            raise SuricataReturnException("L{}: Unable to get message from server".format(get_linenumber))
 
         if self.verbose:
             print("RCV: "+ json.dumps(cmdret))
@@ -169,7 +175,7 @@ class SuricataSC:
                 self.socket = socket(AF_UNIX)
             self.socket.connect(self.sck_path)
         except error as err:
-            raise SuricataNetException(err)
+            raise SuricataNetException("L{}: {}".format(get_linenumber(), err))
 
         self.socket.settimeout(10)
         #send version
@@ -187,13 +193,13 @@ class SuricataSC:
             cmdret = None
 
         if not cmdret:
-            raise SuricataReturnException("Unable to get message from server")
+            raise SuricataReturnException("L{}: Unable to get message from server".format(get_linenumber()))
 
         if self.verbose:
             print("RCV: "+ json.dumps(cmdret))
 
         if cmdret["return"] == "NOK":
-            raise SuricataReturnException("Error: %s" % (cmdret["message"]))
+            raise SuricataReturnException("L{}: Error: {}".format(get_linenumber(), cmdret["message"]))
 
         cmdret = self.send_command("command-list")
 
@@ -223,9 +229,9 @@ class SuricataSC:
                 except IndexError:
                     phrase = " at least" if required_args_count != len(cmd_specs) else ""
                     msg = "Missing arguments: expected{} {}".format(phrase, required_args_count)
-                    raise SuricataCommandException(msg)
+                    raise SuricataCommandException("L{}: {}".format(get_linenumber(), msg))
                 except ValueError as ve:
-                    raise SuricataCommandException("Erroneous arguments: {}".format(ve))
+                    raise SuricataCommandException("L{}: Erroneous arguments: {}".format(get_linenumber(), ve))
             elif c < len(full_cmd):
                 arguments[spec["name"]] = spec_type(full_cmd[c])
         return cmd, arguments
@@ -237,7 +243,7 @@ class SuricataSC:
             if cmd in self.fn_commands:
                 cmd, arguments = getattr(self, "execute")(command=command)
         else:
-            raise SuricataCommandException("Unknown command: {}".format(command))
+            raise SuricataCommandException("L{}: Unknown command: {}".format(get_linenumber(), command))
         return cmd, arguments
 
     def interactive(self):