bes  Updated for version 3.17.0
BESDapError.cc
1 // BESDapError.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 <sstream>
34 #include <iostream>
35 
36 using std::ostringstream;
37 
38 #include "BESDapError.h"
39 #include "BESContextManager.h"
40 #include "BESDapErrorInfo.h"
41 
64 int BESDapError::convert_error_code(int error_code, int current_error_type)
65 {
66  if (current_error_type == BES_INTERNAL_FATAL_ERROR) return current_error_type;
67  switch (error_code) {
68  case undefined_error:
69  case unknown_error: {
70  return BES_INTERNAL_ERROR;
71  break;
72  }
73  case internal_error: {
74  return BES_INTERNAL_FATAL_ERROR;
75  break;
76  }
77  case no_such_file: {
78  return BES_NOT_FOUND_ERROR;
79  break;
80  }
81  case no_such_variable:
82  case malformed_expr: {
83  return BES_SYNTAX_USER_ERROR;
84  break;
85  }
86  case no_authorization:
87  case cannot_read_file:
88  case dummy_message: {
89  return BES_FORBIDDEN_ERROR;
90  break;
91  }
92  default: {
93  return BES_INTERNAL_ERROR;
94  break;
95  }
96  }
97  return BES_INTERNAL_ERROR;
98 }
99 
110 {
111  // If we are handling errors in a dap2 context, then create a
112  // DapErrorInfo object to transmit/print the error as a dap2
113  // response.
114  bool found = false;
115  // I changed 'dap_format' to 'errors' in the following line. jhrg 10/6/08
116  string context = BESContextManager::TheManager()->get_context("errors", found);
117  if (context == "dap2") {
118  ErrorCode ec = unknown_error;
119  BESDapError *de = dynamic_cast<BESDapError*>(&e);
120  if (de) {
121  ec = de->get_error_code();
122  }
124  dhi.error_info = new BESDapErrorInfo(ec, e.get_message());
125 
126  return e.get_error_type();
127  }
128  else {
129  // If we are not in a dap2 context and the exception is a dap
130  // handler exception, then convert the error message to include the
131  // error code. If it is or is not a dap exception, we simply return
132  // that the exception was not handled.
133  BESError *e_p = &e;
134  BESDapError *de = dynamic_cast<BESDapError*>(e_p);
135  if (de) {
136  ostringstream s;
137  s << "libdap exception building response: error_code = " << de->get_error_code() << ": "
138  << de->get_message();
139  e.set_message(s.str());
140  e.set_error_type(convert_error_code(de->get_error_code(), e.get_error_type()));
141  }
142  }
143  return 0;
144 }
145 
152 void BESDapError::dump(ostream &strm) const
153 {
154  strm << BESIndent::LMarg << "BESDapError::dump - (" << (void *) this << ")" << endl;
155  BESIndent::Indent();
156  strm << BESIndent::LMarg << "error code = " << get_error_code() << endl;
157  BESError::dump(strm);
158  BESIndent::UnIndent();
159 }
160 
static int convert_error_code(int error_code, int current_error_type)
converts the libdap error code to the bes error type
Definition: BESDapError.cc:64
silent informational response object
virtual void set_error_type(int type)
Set the return code for this particular error class.
Definition: BESError.h:127
virtual std::string get_message()
get the error message for this exception
Definition: BESError.h:97
virtual string get_context(const string &name, bool &found)
retrieve the value of the specified context from the BES
virtual void dump(std::ostream &strm) const
Displays debug information about this object.
Definition: BESError.cc:44
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
error object created from libdap error objects and can handle those errors
Definition: BESDapError.h:51
virtual void set_message(const std::string &msg)
set the error message for this exception
Definition: BESError.h:89
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESDapError.cc:152
Structure storing information used by the BES to handle the request.
static int handleException(BESError &e, BESDataHandlerInterface &dhi)
handles exceptions if the error context is set to dap2
Definition: BESDapError.cc:109
BESInfo * error_info
error information object