From ef832e0fa6f2aca8566a6c5140fd7b21f8fef910 Mon Sep 17 00:00:00 2001
From: Jorge Camarero Vera <jorge.camarero@cern.ch>
Date: Fri, 14 Oct 2022 14:44:41 +0200
Subject: [PATCH] Caught the No drive found in configuration exception

---
 ReleaseNotes.md                  |  3 ++-
 tapeserver/daemon/TapeDaemon.cpp | 25 +++++++++++++++----------
 2 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/ReleaseNotes.md b/ReleaseNotes.md
index 504bd5b62e..4a54f2ccf5 100644
--- a/ReleaseNotes.md
+++ b/ReleaseNotes.md
@@ -5,7 +5,8 @@
 - cta/CTA#41 - Delete verification_status of tape when tape is reclaimed
 - cta/CTA#153 - Allow verification status to be cleared with cta-admin
 - cta/CTA#173 - Update release notes and small changes to refactoring of operation tools cmd line parsing - Compatible with operations 0.4-95 or later
-### Bug fixes 
+### Bug fixes
+- cta/CTA#48 - Catch tape server exception and log an error instead
 - cta/CTA#161 - Fix bug when using temporary tables with PostgreSQL
 
 # v4.7.12-1
diff --git a/tapeserver/daemon/TapeDaemon.cpp b/tapeserver/daemon/TapeDaemon.cpp
index 412886155f..0f1e08eda6 100644
--- a/tapeserver/daemon/TapeDaemon.cpp
+++ b/tapeserver/daemon/TapeDaemon.cpp
@@ -14,18 +14,20 @@
  *               granted to it by virtue of its status as an Intergovernmental Organization or
  *               submit itself to any jurisdiction.
  */
+#include <limits.h>
+#include <sys/prctl.h>
+
+#include <google/protobuf/service.h>
 
-#include "TapeDaemon.hpp"
 #include "common/exception/Errnum.hpp"
+#include "common/exception/NoSuchObject.hpp"
 #include "common/utils/utils.hpp"
 #include "tapeserver/daemon/CommandLineParams.hpp"
-#include "ProcessManager.hpp"
-#include "SignalHandler.hpp"
-#include "DriveHandler.hpp"
-#include "MaintenanceHandler.hpp"
-#include <google/protobuf/service.h>
-#include <limits.h>
-#include <sys/prctl.h>
+#include "tapeserver/daemon/DriveHandler.hpp"
+#include "tapeserver/daemon/MaintenanceHandler.hpp"
+#include "tapeserver/daemon/ProcessManager.hpp"
+#include "tapeserver/daemon/SignalHandler.hpp"
+#include "tapeserver/daemon/TapeDaemon.hpp"
 
 namespace cta { namespace tape { namespace daemon {
 
@@ -49,6 +51,9 @@ TapeDaemon::~TapeDaemon() {
 int TapeDaemon::main() {
   try {
     exceptionThrowingMain();
+  } catch (cta::exception::NoSuchObject &ex) {
+    m_log(log::ERR, "Aborting cta-taped. Not starting because: " + ex.getMessage().str());
+    return 2;
   } catch (cta::exception::Exception &ex) {
     // Log the error
     m_log(log::ERR, "Aborting cta-taped on uncaught exception. Stack trace follows.", {{"Message", ex.getMessage().str()}});
@@ -73,8 +78,8 @@ std::string cta::tape::daemon::TapeDaemon::getHostName() const {
 // exceptionThrowingMain
 //------------------------------------------------------------------------------
 void  cta::tape::daemon::TapeDaemon::exceptionThrowingMain()  {
-  if(m_globalConfiguration.driveConfigs.empty())
-    throw cta::exception::Exception("No drive found in configuration");
+  if (m_globalConfiguration.driveConfigs.empty())
+    throw cta::exception::NoSuchObject("No drive found in configuration");
 
   // Process must be able to change user now and should be permitted to perform
   // raw IO in the future
-- 
GitLab