bes  Updated for version 3.19.1
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 <time.h> /* time_t, struct tm, difftime, time, mktime */
34 
35 #include <sstream>
36 
37 #include "BESExceptionManager.h"
38 
39 #include "BESError.h"
40 #include "TheBESKeys.h"
41 #include "BESInfoList.h"
42 #include "BESLog.h"
43 
44 #define DEFAULT_ADMINISTRATOR "support@opendap.org"
45 
46 BESExceptionManager *BESExceptionManager::_instance = 0;
47 
48 BESExceptionManager::BESExceptionManager()
49 {
50 }
51 
52 BESExceptionManager::~BESExceptionManager()
53 {
54 }
55 
70 {
71  _ehm_list.push_back(ehm);
72 }
73 
78 void log_error(BESError &e)
79 {
80 #if 0
81  struct tm *ptm;
82  time_t timer = time(NULL);
83  ptm = gmtime(&timer);
84  string now(asctime(ptm));
85  now = now.substr(0, now.length() - 1); // drop \n from end of string
86 #endif
87 
88  string error_name = "";
89  // TODO This should be configurable; I'm changing the values below to always log all errors.
90  // I'm also confused about the actual intention. jhrg 11/14/17
91  bool only_log_to_verbose = false;
92  switch (e.get_error_type()) {
93  case BES_INTERNAL_FATAL_ERROR:
94  error_name = "BES Internal Fatal Error";
95  break;
96 
97  case BES_INTERNAL_ERROR:
98  error_name = "BES Internal Error";
99  break;
100 
101  case BES_SYNTAX_USER_ERROR:
102  error_name = "BES User Syntax Error";
103  only_log_to_verbose = false; // TODO Was 'true.' jhrg 11/14/17
104  break;
105 
106  case BES_FORBIDDEN_ERROR:
107  error_name = "BES Forbidden Error";
108  break;
109 
110  case BES_NOT_FOUND_ERROR:
111  error_name = "BES Not Found Error";
112  only_log_to_verbose = false; // TODO was 'true.' jhrg 11/14/17
113  break;
114 
115  default:
116  error_name = "Unrecognized BES Error";
117  break;
118  }
119 
120 #if 0
121  string m = BESLog::mark;
122  std::ostringstream msg;
123  msg << "ERROR: " << error_name << m << "type: " << e.get_error_type() << m << "file: " << e.get_file() << m
124  << "line: " << e.get_line() << m << "message: " << e.get_message();
125 #endif
126 
127  if (only_log_to_verbose) {
128  VERBOSE("ERROR: " << error_name << ", type: " << e.get_error_type() << ", file: " << e.get_file() << ":"
129  << e.get_line() << ", message: " << e.get_message() << endl);
130 #if 0
131  // This seems buggy - if you don't flush the
132  // log it won't print the time correctly.
133  BESLog::TheLog()->flush_me();
134  *(BESLog::TheLog()) << msg.str() << endl;
135  BESLog::TheLog()->flush_me();
136 #endif
137  }
138  else {
139  LOG("ERROR: " << error_name << ": " << e.get_message() << endl);
140 #if 0
141  BESLog::TheLog()->flush_me();
142  *(BESLog::TheLog()) << msg.str() << endl;
143  BESLog::TheLog()->flush_me();
144 #endif
145  }
146 }
147 
169 {
170  // Let's see if any of these exception callbacks can handle the
171  // exception. The first callback that can handle the exception wins
172 
173  for (ehm_iter i = _ehm_list.begin(), ei = _ehm_list.end(); i != ei; ++i) {
174  p_bes_ehm p = *i;
175  int handled = p(e, dhi);
176  if (handled) {
177  return handled;
178  }
179  }
180 
181  dhi.error_info = BESInfoList::TheList()->build_info();
182  string action_name = dhi.action_name;
183  if (action_name.empty()) action_name = "BES";
184  dhi.error_info->begin_response(action_name, dhi);
185 
186  string administrator = "";
187  try {
188  bool found = false;
189  vector<string> vals;
190  string key = "BES.ServerAdministrator";
191  TheBESKeys::TheKeys()->get_value(key, administrator, found);
192  }
193  catch (...) {
194  administrator = DEFAULT_ADMINISTRATOR;
195  }
196  if (administrator.empty()) {
197  administrator = DEFAULT_ADMINISTRATOR;
198  }
199  dhi.error_info->add_exception(e, administrator);
200  dhi.error_info->end_response();
201 
202  // Write a message in the log file about this error...
203  log_error(e);
204 
205  return e.get_error_type();
206 }
207 
217 void BESExceptionManager::dump(ostream &strm) const
218 {
219  strm << BESIndent::LMarg << "BESExceptionManager::dump - (" << (void *) this << ")" << endl;
220  BESIndent::Indent();
221  strm << BESIndent::LMarg << "# registered callbacks: " << _ehm_list.size() << endl;
222  BESIndent::UnIndent();
223 }
224 
226 BESExceptionManager::TheEHM()
227 {
228  if (_instance == 0) {
229  _instance = new BESExceptionManager();
230  }
231  return _instance;
232 }
233 
virtual void add_ehm_callback(p_bes_ehm ehm)
Register an exception handler with the manager.
virtual std::string get_message()
get the error message for this exception
Definition: BESError.h:97
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: TheBESKeys.cc:422
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
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:62
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
Structure storing information used by the BES to handle the request.
virtual std::string get_file()
get the file name where the exception was thrown
Definition: BESError.h:105
virtual void dump(ostream &strm) const
dumps information about this object
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.
virtual int get_line()
get the line number where the exception was thrown
Definition: BESError.h:113