From e4047b3f3c875cc995ec65dd58ec94462c6f503d Mon Sep 17 00:00:00 2001
From: Michael Davis <michael.davis@cern.ch>
Date: Mon, 27 Jan 2020 11:51:13 +0100
Subject: [PATCH] [cta-admin] Handle user errors from CTA Frontend:

- XRootD/SSI/Protobuf errors or CTA Exceptions return error code 1
  (something went wrong with communication or in the frontend)

- User errors return error code 2 (user specified an invalid drive,
  vid, etc.)

- If text output, print error message on stderr and exit with code 2

- If --json output, print error message on stderr, print empty JSON
  array [] on stdout, and exit with code 2
---
 cmdline/CtaAdminCmd.cpp | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/cmdline/CtaAdminCmd.cpp b/cmdline/CtaAdminCmd.cpp
index edbd961c0f..017c11d0e6 100644
--- a/cmdline/CtaAdminCmd.cpp
+++ b/cmdline/CtaAdminCmd.cpp
@@ -38,6 +38,16 @@ cta::admin::TextFormatter formattedText(1000);
 
 namespace XrdSsiPb {
 
+/*!
+ * User error exception
+ */
+class UserException : public std::runtime_error
+{
+public:
+  UserException(const std::string &err_msg) : std::runtime_error(err_msg) {}
+};
+
+
 /*!
  * Alert callback.
  *
@@ -283,7 +293,7 @@ void CtaAdminCmd::send() const
          isHeaderSent = true;
          break;
       case Response::RSP_ERR_PROTOBUF:                     throw XrdSsiPb::PbException(response.message_txt());
-      case Response::RSP_ERR_USER:
+      case Response::RSP_ERR_USER:                         throw XrdSsiPb::UserException(response.message_txt());
       case Response::RSP_ERR_CTA:                          throw std::runtime_error(response.message_txt());
       default:                                             throw XrdSsiPb::PbException("Invalid response type.");
    }
@@ -490,6 +500,10 @@ int main(int argc, const char **argv)
       std::cerr << "Error in Google Protocol Buffers: " << ex.what() << std::endl;
    } catch (XrdSsiPb::XrdSsiException &ex) {
       std::cerr << "Error from XRootD SSI Framework: " << ex.what() << std::endl;
+   } catch (XrdSsiPb::UserException &ex) {
+      if(CtaAdminCmd::isJson()) std::cout << CtaAdminCmd::jsonCloseDelim();
+      std::cerr << ex.what() << std::endl;
+      return 2;
    } catch (std::runtime_error &ex) {
       std::cerr << ex.what() << std::endl;
    } catch (std::exception &ex) {
-- 
GitLab