| 1 |
/* |
| 2 |
* This File is part of Davix, The IO library for HTTP based protocols |
| 3 |
* Copyright (C) CERN 2013 |
| 4 |
* Author: Adrien Devresse <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <davix_internal.hpp> |
| 23 |
#include <utils/davix_logger_internal.hpp> |
| 24 |
#include <utils/davix_types.hpp> |
| 25 |
#include <status/davixstatusrequest.hpp> |
| 26 |
#include <status/DavixErrorInternal.hpp> |
| 27 |
|
| 28 |
namespace Davix { |
| 29 |
|
| 30 |
DavixError::DavixError(const std::string &scope, StatusCode::Code errCode, const std::string &errMsg) : |
| 31 |
d_ptr(new DavixErrorInternal(scope, errCode, errMsg)){ |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
DavixError::DavixError(const DavixError & e) : |
| 36 |
d_ptr(new DavixErrorInternal(*(e.d_ptr))){ |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
DavixError & DavixError::operator =(const DavixError & e){ |
| 41 |
DavixError tmp(e); |
| 42 |
this->swap(tmp); |
| 43 |
return *this; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
DavixError::~DavixError(){ |
| 48 |
delete d_ptr; |
| 49 |
} |
| 50 |
|
| 51 |
DavixError* DavixError::clone(){ |
| 52 |
return new DavixError(*this); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
const std::string & DavixError::getErrMsg() const{ |
| 57 |
return d_ptr->_errMsg; |
| 58 |
} |
| 59 |
|
| 60 |
void DavixError::setErrMsg(const std::string &msg){ |
| 61 |
d_ptr->_errMsg = msg; |
| 62 |
} |
| 63 |
|
| 64 |
void DavixError::setStatus(const StatusCode::Code c){ |
| 65 |
d_ptr->_code = c; |
| 66 |
} |
| 67 |
|
| 68 |
StatusCode::Code DavixError::getStatus() const{ |
| 69 |
return d_ptr->_code; |
| 70 |
} |
| 71 |
|
| 72 |
const std::string & DavixError::getErrScope() const{ |
| 73 |
return d_ptr->_scope; |
| 74 |
} |
| 75 |
|
| 76 |
void DavixError::setErrScope(const std::string &scope){ |
| 77 |
d_ptr->_scope = scope; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
void DavixError::setupError(DavixError **err, const std::string &scope, StatusCode::Code errCode, const std::string &errMsg){ |
| 83 |
if(err){ |
| 84 |
if(*err){ |
| 85 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_CORE, "Error Override of previous DavixError, BUG !"); |
| 86 |
// error msg |
| 87 |
} |
| 88 |
*err = new DavixError(scope, errCode, errMsg); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
void DavixError::propagateError(DavixError **newErr, DavixError *oldErr){ |
| 94 |
propagatePrefixedError(newErr, oldErr, ""); |
| 95 |
} |
| 96 |
|
| 97 |
void DavixError::propagatePrefixedError(DavixError **newErr, DavixError *oldErr, const std::string &prefix){ |
| 98 |
if(oldErr== NULL) |
| 99 |
return; |
| 100 |
|
| 101 |
if(newErr){ |
| 102 |
if(*newErr != NULL){ |
| 103 |
std::cerr << "***ERROR*** in propagateError, *newErr is not NULL impossible to overwrite ... " |
| 104 |
" old error was" << oldErr->getErrMsg()<< std::endl; |
| 105 |
}else{ |
| 106 |
*newErr = oldErr; |
| 107 |
if(*newErr && prefix.empty() == false){ |
| 108 |
std::string new_mess(prefix); |
| 109 |
new_mess.append(" "); |
| 110 |
(*newErr)->d_ptr->_errMsg = new_mess.append((*newErr)->d_ptr->_errMsg); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
void DavixError::swap(DavixError &err){ |
| 118 |
std::swap(d_ptr, err.d_ptr); |
| 119 |
} |
| 120 |
|
| 121 |
void DavixError::clearError(DavixError **err){ |
| 122 |
if(err && *err){ |
| 123 |
delete *err; |
| 124 |
*err = NULL; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
void errno_to_davix_exception(int errno_code, const std::string &scope, const std::string &msg){ |
| 130 |
DavixError* tmp_err=NULL; |
| 131 |
davix_errno_to_davix_error(errno_code, scope, msg, &tmp_err); |
| 132 |
checkDavixError(&tmp_err); |
| 133 |
} |
| 134 |
|
| 135 |
void davix_errno_to_davix_error(int errcode, const std::string & scope, const std::string & msg, DavixError** newErr){ |
| 136 |
StatusCode::Code c; |
| 137 |
std::string msg_final; |
| 138 |
switch(errcode){ |
| 139 |
case 0: |
| 140 |
return; |
| 141 |
case ENOENT: |
| 142 |
c = StatusCode::FileNotFound; |
| 143 |
msg_final = "File not found " + msg; |
| 144 |
break; |
| 145 |
case EPERM: |
| 146 |
case EACCES: |
| 147 |
c = StatusCode::PermissionRefused; |
| 148 |
msg_final = "Permission refused " + msg; |
| 149 |
break; |
| 150 |
case EISDIR: |
| 151 |
c = StatusCode::IsADirectory; |
| 152 |
msg_final = "is a directory " + msg; |
| 153 |
break; |
| 154 |
case EINVAL: |
| 155 |
c = StatusCode::InvalidArgument; |
| 156 |
msg_final = "invalid argument " + msg; |
| 157 |
break; |
| 158 |
case EIO: |
| 159 |
c = StatusCode::InvalidArgument; |
| 160 |
msg_final = "Input/output error" + msg; |
| 161 |
break; |
| 162 |
case ENOTDIR: |
| 163 |
c = StatusCode::IsNotADirectory; |
| 164 |
msg_final = "is not a directory" + msg; |
| 165 |
break; |
| 166 |
default: |
| 167 |
c = StatusCode::SystemError; |
| 168 |
msg_final = std::string(strerror(errcode)) + " " + msg; |
| 169 |
} |
| 170 |
DavixError::setupError(newErr, scope, c, msg_final); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
// extension |
| 175 |
struct DavixException::DavixExceptionIntern{ |
| 176 |
|
| 177 |
}; |
| 178 |
|
| 179 |
|
| 180 |
DavixException::DavixException(const std::string &scope, StatusCode::Code c, const std::string &msg) throw() : |
| 181 |
std::exception(), |
| 182 |
e(scope, c, msg), |
| 183 |
d_ptr(NULL){ |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
DavixException::DavixException(const DavixException &orig) throw() : e(orig.e), d_ptr(orig.d_ptr) |
| 188 |
{ |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
DavixException::DavixException(DavixError **err) : |
| 193 |
std::exception(), |
| 194 |
e( "Davix::Error", StatusCode::UnknownError, "Error, no valid DavixError triggered"), |
| 195 |
d_ptr(NULL){ |
| 196 |
if(err != NULL && *err != NULL){ |
| 197 |
e.swap(**err); |
| 198 |
DavixError::clearError(err); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
void checkDavixError(DavixError **err){ |
| 204 |
if(err && *err){ |
| 205 |
throw DavixException(err); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
DavixException::~DavixException() throw(){ |
| 211 |
delete d_ptr; |
| 212 |
} |
| 213 |
|
| 214 |
const char* DavixException::what() const throw() { |
| 215 |
return e.getErrMsg().c_str(); |
| 216 |
} |
| 217 |
|
| 218 |
StatusCode::Code DavixException::code() const throw(){ |
| 219 |
return e.getStatus(); |
| 220 |
} |
| 221 |
|
| 222 |
const char* DavixException::scope() const throw(){ |
| 223 |
return e.getErrScope().c_str(); |
| 224 |
} |
| 225 |
|
| 226 |
void DavixException::toDavixError(DavixError **err){ |
| 227 |
DavixError::propagateError(err, new DavixError(this->e)); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
std::string davix_scope_stat_str(){ |
| 232 |
return "Davix::stat"; |
| 233 |
} |
| 234 |
|
| 235 |
std::string davix_scope_mkdir_str(){ |
| 236 |
return "Davix::mkdir"; |
| 237 |
} |
| 238 |
|
| 239 |
std::string davix_scope_rm_str(){ |
| 240 |
return "Davix::rm"; |
| 241 |
} |
| 242 |
|
| 243 |
std::string davix_scope_mv_str(){ |
| 244 |
return "Davix::mv"; |
| 245 |
} |
| 246 |
|
| 247 |
std::string davix_scope_directory_listing_str(){ |
| 248 |
return "Davix::Posix::listdir"; |
| 249 |
} |
| 250 |
|
| 251 |
std::string davix_scope_http_request(){ |
| 252 |
return "Davix::HttpRequest"; |
| 253 |
} |
| 254 |
|
| 255 |
std::string davix_scope_meta(){ |
| 256 |
return "Davix::MetaDataOps"; |
| 257 |
} |
| 258 |
|
| 259 |
std::string davix_scope_xml_parser(){ |
| 260 |
return "Davix::XMLParser"; |
| 261 |
} |
| 262 |
|
| 263 |
std::string davix_scope_uri_parser(){ |
| 264 |
return "Davix::Uri::Parser"; |
| 265 |
} |
| 266 |
|
| 267 |
std::string davix_scope_davOps_str(){ |
| 268 |
return "Davix::Dav::Ops"; |
| 269 |
} |
| 270 |
|
| 271 |
std::string davix_scope_io_buff(){ |
| 272 |
return "Davix::IO:Buff"; |
| 273 |
} |
| 274 |
|
| 275 |
std::string davix_scope_x509cred(){ |
| 276 |
return "Davix::X509cred"; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
} // namespace Davix |
| 282 |
|
| 283 |
|
| 284 |
|