bes  Updated for version 3.17.0
BESExceptionManager.cc
1 // BESExceptionManager.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include "BESExceptionManager.h"
34 
35 #include "BESError.h"
36 #include "TheBESKeys.h"
37 #include "BESInfoList.h"
38 
39 #define DEFAULT_ADMINISTRATOR "support@opendap.org"
40 
41 BESExceptionManager *BESExceptionManager::_instance = 0;
42 
43 BESExceptionManager::BESExceptionManager()
44 {
45 }
46 
47 BESExceptionManager::~BESExceptionManager()
48 {
49 }
50 
65 {
66  _ehm_list.push_back(ehm);
67 }
68 
90 {
91  // Let's see if any of these exception callbacks can handle the
92  // exception. The first callback that can handle the exception wins
93  ehm_iter i = _ehm_list.begin();
94  for (; i != _ehm_list.end(); i++) {
95  p_bes_ehm p = *i;
96  int handled = p(e, dhi);
97  if (handled) {
98  return handled;
99  }
100  }
101 
102  dhi.error_info = BESInfoList::TheList()->build_info();
103  string action_name = dhi.action_name;
104  if (action_name == "") action_name = "BES";
105  dhi.error_info->begin_response(action_name, dhi);
106 
107  string administrator = "";
108  try {
109  bool found = false;
110  vector<string> vals;
111  string key = "BES.ServerAdministrator";
112  TheBESKeys::TheKeys()->get_value(key, administrator, found);
113  }
114  catch (...) {
115  administrator = DEFAULT_ADMINISTRATOR;
116  }
117  if (administrator.empty()) {
118  administrator = DEFAULT_ADMINISTRATOR;
119  }
120  dhi.error_info->add_exception(e, administrator);
121  dhi.error_info->end_response();
122  return e.get_error_type();
123 }
124 
134 void BESExceptionManager::dump(ostream &strm) const
135 {
136  strm << BESIndent::LMarg << "BESExceptionManager::dump - (" << (void *) this << ")" << endl;
137  BESIndent::Indent();
138  strm << BESIndent::LMarg << "# registered callbacks: " << _ehm_list.size() << endl;
139  BESIndent::UnIndent();
140 }
141 
143 BESExceptionManager::TheEHM()
144 {
145  if (_instance == 0) {
146  _instance = new BESExceptionManager();
147  }
148  return _instance;
149 }
150 
virtual void add_ehm_callback(p_bes_ehm ehm)
Register an exception handler with the manager.
virtual int get_error_type()
Return the return code for this error class.
Definition: BESError.h:138
Abstract exception class for the BES with basic string message.
Definition: BESError.h:56
virtual void add_exception(BESError &e, const string &admin)
add exception information to this informational object
Definition: BESInfo.cc:222
manages exception handling code and default exceptions
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: BESKeys.cc:481
virtual void dump(ostream &strm) const
dumps information about this object
Structure storing information used by the BES to handle the request.
virtual void begin_response(const string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESInfo.cc:112
BESInfo * error_info
error information object
virtual int handle_exception(BESError &e, BESDataHandlerInterface &dhi)
Manage any exceptions thrown during the handling of a request.
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:43