fix error handling for CryptHandle.get

The old code calls CryptHandle once to make sure it is valid, then
calls it again to use it.

Tested:
busctl call xyz.openbmc_project.eStoraged.mmcblk0 \
>   /xyz/openbmc_project/inventory/storage/mmcblk0 \
>   xyz.openbmc_project.Inventory.Item.Volume FormatLuks ays 3 1 2 3 \
>   xyz.openbmc_project.Inventory.Item.Volume.FilesystemType.ext4 \
>   --timeout=60

Signed-off-by: John Edward Broadbent <jebr@google.com>
Change-Id: I21c02315c365e74ead3d0b5b2578c62503376756
diff --git a/include/cryptsetupInterface.hpp b/include/cryptsetupInterface.hpp
index 49076dd..719e98b 100644
--- a/include/cryptsetupInterface.hpp
+++ b/include/cryptsetupInterface.hpp
@@ -2,11 +2,17 @@
 
 #include <libcryptsetup.h>
 
+#include <phosphor-logging/lg2.hpp>
 #include <stdplus/handle/managed.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+
+#include <string>
 
 namespace estoraged
 {
 
+using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound;
+
 /** @class CryptsetupInterface
  *  @brief Interface to the cryptsetup functions used to manage a LUKS device.
  *  @details This class is used to mock out the cryptsetup functions.
@@ -222,6 +228,14 @@
     /** @brief Get a pointer to the crypt_device struct. */
     struct crypt_device* get()
     {
+        if (*handle == nullptr)
+        {
+            lg2::error("Failed to get crypt device handle",
+                       "REDFISH_MESSAGE_ID",
+                       std::string("OpenBMC.0.1.HandleGetFail"));
+            throw ResourceNotFound();
+        }
+
         return *handle;
     }