00001 // BESDapError.cc 00002 00003 // This file is part of bes, A C++ back-end server implementation framework 00004 // for the OPeNDAP Data Access Protocol. 00005 00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research 00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu> 00008 // 00009 // This library is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU Lesser General Public 00011 // License as published by the Free Software Foundation; either 00012 // version 2.1 of the License, or (at your option) any later version. 00013 // 00014 // This library is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 // Lesser General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU Lesser General Public 00020 // License along with this library; if not, write to the Free Software 00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 // 00023 // You can contact University Corporation for Atmospheric Research at 00024 // 3080 Center Green Drive, Boulder, CO 80301 00025 00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005 00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR. 00028 // 00029 // Authors: 00030 // pwest Patrick West <pwest@ucar.edu> 00031 // jgarcia Jose Garcia <jgarcia@ucar.edu> 00032 00033 #include <sstream> 00034 00035 using std::ostringstream; 00036 00037 #include "BESDapError.h" 00038 #include "BESContextManager.h" 00039 #include "BESDapErrorInfo.h" 00040 00063 int 00064 BESDapError::convert_error_code( int error_code, int current_error_type ) 00065 { 00066 if( current_error_type == BES_INTERNAL_FATAL_ERROR ) 00067 return current_error_type ; 00068 switch( error_code ) 00069 { 00070 case undefined_error: 00071 case unknown_error: 00072 { 00073 return BES_INTERNAL_ERROR ; 00074 break ; 00075 } 00076 case internal_error: 00077 { 00078 return BES_INTERNAL_FATAL_ERROR ; 00079 break ; 00080 } 00081 case no_such_file: 00082 { 00083 return BES_NOT_FOUND_ERROR ; 00084 break ; 00085 } 00086 case no_such_variable: 00087 case malformed_expr: 00088 { 00089 return BES_SYNTAX_USER_ERROR ; 00090 break ; 00091 } 00092 case no_authorization: 00093 case cannot_read_file: 00094 case dummy_message: 00095 { 00096 return BES_FORBIDDEN_ERROR ; 00097 break ; 00098 } 00099 default: 00100 { 00101 return BES_INTERNAL_ERROR ; 00102 break ; 00103 } 00104 } 00105 return BES_INTERNAL_ERROR ; 00106 } 00107 00117 int 00118 BESDapError::handleException( BESError &e, BESDataHandlerInterface &dhi ) 00119 { 00120 // If we are handling errors in a dap2 context, then create a 00121 // DapErrorInfo object to transmit/print the error as a dap2 00122 // response. 00123 bool found = false ; 00124 // I changed 'dap_format' to 'errors' in the following line. jhrg 10/6/08 00125 string context = BESContextManager::TheManager()->get_context( "errors", found ) ; 00126 if( context == "dap2" ) 00127 { 00128 ErrorCode ec = unknown_error ; 00129 BESDapError *de = dynamic_cast<BESDapError*>( &e); 00130 if( de ) 00131 { 00132 ec = de->get_error_code() ; 00133 } 00134 e.set_error_type( convert_error_code( ec, e.get_error_type() ) ) ; 00135 dhi.error_info = new BESDapErrorInfo( ec, e.get_message() ) ; 00136 00137 return e.get_error_type() ; 00138 } 00139 else 00140 { 00141 // If we are not in a dap2 context and the exception is a dap 00142 // handler exception, then convert the error message to include the 00143 // error code. If it is or is not a dap exception, we simply return 00144 // that the exception was not handled. 00145 BESDapError *de = dynamic_cast<BESDapError*>( &e); 00146 if( de ) 00147 { 00148 ostringstream s; 00149 s << "libdap exception building response" 00150 << ": error_code = " << de->get_error_code() 00151 << ": " << de->get_message() ; 00152 e.set_message( s.str() ) ; 00153 e.set_error_type( convert_error_code( de->get_error_code(), 00154 e.get_error_type() ) ) ; 00155 } 00156 } 00157 return 0 ; 00158 } 00159