Exception.cpp 6.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/******************************************************************************
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * 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 2
 * of the License, or (at your option) any later version.
 * 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.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * 
 *
 * @author Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/

#include "Exception.hpp" 

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//------------------------------------------------------------------------------
// 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) {
49
  if (SCSI::Status::GOOD != sgio.status) {
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    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);
    }
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
109
110
111
112
113
114

//------------------------------------------------------------------------------
// 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: "
115
              << ex.getMessage().str();
116
117
118
119
120
    }
    try {
      w << ": " << sense->getACSString();
    } catch (Exception &ex) {
      w << ": In addition, failed to get ACS string: "
121
              << ex.getMessage().str();
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
156
157
158
159
160
161
    }
  }
  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: "
162
              << ex.getMessage().str();
163
164
165
166
167
    }
    try {
      w << ": " << sense->getACSString();
    } catch (Exception &ex) {
      w << ": In addition, failed to get ACS string: "
168
              << ex.getMessage().str();
169
170
171
172
173
174
    }
  }

  setWhat(w.str());
}