Handle exceptions when there are connection errors

Changes:
    - Added try except handling in a key location to
      suppress noisy traceback python dumping on the
      console.
    - Add code to mask password in the console log

Tested:
    - When failed to connect to the server when it is not
      responsive.
    - Wrong password causing the connection to fail.
    - Wrong HTTPS port to cause connection failure

Change-Id: I27bb4e2fe4489ad742b7d9ebacd09a8f7ced4fa0
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/lib/bmc_redfish.py b/lib/bmc_redfish.py
index a7a70c4..1fe66c3 100644
--- a/lib/bmc_redfish.py
+++ b/lib/bmc_redfish.py
@@ -56,6 +56,14 @@
                 gp.lprint_var(except_type)
                 gp.lprint_varx("except_value", str(except_value))
                 raise (get_exception)
+        except AttributeError as e:
+            BuiltIn().log_to_console(
+                "AttributeError: Error response from server"
+            )
+        except Exception as e:
+            error_response = type(e).__name__ + " from bmc_redfish class"
+            BuiltIn().log_to_console(error_response)
+
         BuiltIn().set_global_variable("${REDFISH_SUPPORTED}", self.__inited__)
         BuiltIn().set_global_variable("${REDFISH_REST_SUPPORTED}", True)
 
diff --git a/lib/bmc_redfish_utils.py b/lib/bmc_redfish_utils.py
index dbd5da7..099345a 100644
--- a/lib/bmc_redfish_utils.py
+++ b/lib/bmc_redfish_utils.py
@@ -23,7 +23,14 @@
         """
         # Obtain a reference to the global redfish object.
         self.__inited__ = False
-        self._redfish_ = BuiltIn().get_library_instance("redfish")
+        try:
+            self._redfish_ = BuiltIn().get_library_instance("redfish")
+        except RuntimeError as e:
+            BuiltIn().log_to_console(
+                "get_library_instance: No active redfish instance found."
+            )
+            # Handling init exception at worse to raise on error.
+            raise e
 
         if MTLS_ENABLED == "True":
             self.__inited__ = True
diff --git a/lib/gen_print.py b/lib/gen_print.py
index 3f1a1fd..c81e505 100755
--- a/lib/gen_print.py
+++ b/lib/gen_print.py
@@ -1684,7 +1684,22 @@
     if type(cmd_buf) is list:
         # Assume this is a robot command in the form of a list.
         cmd_buf = "  ".join([str(element) for element in cmd_buf])
-    buffer += "Issuing: " + cmd_buf + "\n"
+
+    try:
+        passwd_value = os.environ.get(
+            "OPENBMC_PASSWORD", ""
+        ) or BuiltIn().get_variable_value("${OPENBMC_PASSWORD}", default="")
+    except Exception as e:
+        passwd_value = ""
+        pass
+
+    # adds overhead checking for password masking.
+    if passwd_value != "" and passwd_value in cmd_buf:
+        buffer += (
+            "Issuing: " + cmd_buf.replace(passwd_value, "**********") + "\n"
+        )
+    else:
+        buffer += "Issuing: " + cmd_buf + "\n"
 
     return buffer
 
diff --git a/lib/redfish_plus.py b/lib/redfish_plus.py
index c33895a..3630cc1 100755
--- a/lib/redfish_plus.py
+++ b/lib/redfish_plus.py
@@ -120,7 +120,14 @@
         max_retry = kwargs.pop("max_retry", 10)
         self._max_retry = max_retry
         valid_status_codes = kwargs.pop("valid_status_codes", [200])
-        response = func(*args, **kwargs)
+
+        try:
+            response = func(*args, **kwargs)
+        except Exception as e:
+            error_response = type(e).__name__ + " from redfish_plus class"
+            BuiltIn().log_to_console(error_response)
+            return
+
         valid_http_status_code(response.status, valid_status_codes)
         return response