Exception.cpp 6.76 KB
Newer Older
1
2
3
4
5
6
7
/*
 * @project        The CERN Tape Archive (CTA)
 * @copyright      Copyright(C) 2021 CERN
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 This program is distributed in the hope that it will be useful,
 *                 but WITHOUT ANY WARRANTY; without even the implied warranty of
 *                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                 GNU General Public License for more details.
13
 *
14
15
16
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17
18
19

#include "Exception.hpp" 

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//------------------------------------------------------------------------------
// ExceptionLauncher
//------------------------------------------------------------------------------
void castor::tape::SCSI::ExceptionLauncher(const SCSI::Structures::LinuxSGIO_t &
  sgio, std::string context) {
  std::stringstream contextWithStatuses;
  contextWithStatuses << context
                      << std::hex << std::nouppercase << std::showbase
                      << " status=" << static_cast<unsigned int>(sgio.status)
                      << " host_status=" << sgio.host_status
                      << " driver_status=" << sgio.driver_status
                      << ":";

  checkAndThrowSgStatus(sgio, contextWithStatuses.str());
  checkAndThrowSgHostStatus(sgio, contextWithStatuses.str());
  checkAndThrowSgDriverStatus(sgio, contextWithStatuses.str());
}

//------------------------------------------------------------------------------
// checkAndThrowSgStatus
//------------------------------------------------------------------------------
void castor::tape::SCSI::checkAndThrowSgStatus(
  const SCSI::Structures::LinuxSGIO_t & sgio, const std::string context) {
43
  if (SCSI::Status::GOOD != sgio.status) {
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    if (SCSI::Status::CHECK_CONDITION == sgio.status) {  
      unsigned char senseKey;   
      castor::tape::SCSI::Structures::senseData_t<255> * sense = 
        (SCSI::Structures::senseData_t<255> *)sgio.sbp;   
      try {
        senseKey = sense->getSenseKey();
      } catch (...) {
        throw Exception(sgio.status,
          (SCSI::Structures::senseData_t<255> *)sgio.sbp, context);
      }
      switch (senseKey) {
        case castor::tape::SCSI::senseKeys::notReady :
          throw NotReadyException(sgio.status,
            (SCSI::Structures::senseData_t<255> *)sgio.sbp, context); 
        case castor::tape::SCSI::senseKeys::unitAttention :
          throw UnitAttentionException(sgio.status,
            (SCSI::Structures::senseData_t<255> *)sgio.sbp, context);    
        default:
          throw Exception(sgio.status,
            (SCSI::Structures::senseData_t<255> *)sgio.sbp, context); 
      }
    } else {
      throw Exception(sgio.status,
        (SCSI::Structures::senseData_t<255> *)sgio.sbp, context);
    }
69
70
  }
}
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

//------------------------------------------------------------------------------
// checkAndThrowSgHostStatus
//------------------------------------------------------------------------------
void castor::tape::SCSI::checkAndThrowSgHostStatus(
  const SCSI::Structures::LinuxSGIO_t & sgio, const std::string context) {
  if (SCSI::HostStatus::OK != sgio.host_status) {
    throw HostException(sgio.host_status, context);
  }
}

//------------------------------------------------------------------------------
// checkAndThrowSgDriverStatus
//------------------------------------------------------------------------------
void castor::tape::SCSI::checkAndThrowSgDriverStatus(
  const SCSI::Structures::LinuxSGIO_t & sgio, const std::string context) {
  if (SCSI::DriverStatus::OK != sgio.driver_status) {
    throw DriverException(sgio.driver_status,
      (SCSI::Structures::senseData_t<255> *)sgio.sbp, context);
  }
}

//------------------------------------------------------------------------------
// Exception
//------------------------------------------------------------------------------
castor::tape::SCSI::Exception::Exception(
  unsigned char status, castor::tape::SCSI::Structures::senseData_t<255>* sense,
  const std::string& context): cta::exception::Exception("") {
  std::stringstream w;
  w << context << (context.size()?" ":"")
          << "SCSI command failed with status "
          << SCSI::statusToString(status);
  if (SCSI::Status::CHECK_CONDITION == status) {
    w << ": Sense Information";
    try {
      w << ": " << sense->getSenseKeyString();
    } catch (Exception &ex) {
      w << ": In addition, failed to get Sense Key string: "
109
              << ex.getMessage().str();
110
111
112
113
114
    }
    try {
      w << ": " << sense->getACSString();
    } catch (Exception &ex) {
      w << ": In addition, failed to get ACS string: "
115
              << ex.getMessage().str();
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    }
  }
  setWhat(w.str());
}

//------------------------------------------------------------------------------
// HostException
//------------------------------------------------------------------------------
castor::tape::SCSI::HostException::HostException(
  const unsigned short int host_status, const std::string& context):
  cta::exception::Exception("") {
  std::stringstream w;
  w << context << (context.size()?" ":"")
          << "SCSI command failed with host_status: "
          << SCSI::hostStatusToString(host_status);
  setWhat(w.str());
}

//------------------------------------------------------------------------------
// DriverException
//------------------------------------------------------------------------------
castor::tape::SCSI::DriverException::DriverException(
  const unsigned short int driver_status,
  castor::tape::SCSI::Structures::senseData_t<255>* sense,
  const std::string& context) : cta::exception::Exception("") {
  std::stringstream w;
  const std::string driverSuggestions =
          SCSI::driverStatusSuggestionsToString(driver_status);
  w << context << (context.size() ? " " : "")
          << "SCSI command failed with driver_status: "
          << SCSI::driverStatusToString(driver_status)
          << (driverSuggestions.size() ? ": Driver suggestions:" : "")
          << driverSuggestions;

  if (SCSI::DriverStatus::SENSE & driver_status) {
    w << ": Sense Information";
    try {
      w << ": " << sense->getSenseKeyString();
    } catch (Exception &ex) {
      w << ": In addition, failed to get Sense Key string: "
156
              << ex.getMessage().str();
157
158
159
160
161
    }
    try {
      w << ": " << sense->getACSString();
    } catch (Exception &ex) {
      w << ": In addition, failed to get ACS string: "
162
              << ex.getMessage().str();
163
164
165
166
167
168
    }
  }

  setWhat(w.str());
}