irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

irods_error.cpp
Go to the documentation of this file.
1 // =-=-=-=-=-=-=-
2 #include "irods_error.hpp"
3 
4 // =-=-=-=-=-=-=-
5 // irods includes
6 #include "rodsLog.h"
7 
8 // =-=-=-=-=-=-=-
9 // boost includes
10 #include <boost/lexical_cast.hpp>
11 
12 // =-=-=-=-=-=-=-
13 // stl includes
14 #include <iostream>
15 #include <sstream>
16 
17 namespace irods {
18 
19 // =-=-=-=-=-=-=-
20 // Static private const char * strings:
21 const char *error::iRODS_token_ = "iRODS";
22 const char *error::colon_token_ = " : ";
23 const char *error::status_token_ = " status [";
24 
25 // =-=-=-=-=-=-=-
26 // private - helper fcn to build the result string
28  std::string _file,
29  int _line,
30  std::string _fcn ) {
31  // =-=-=-=-=-=-=-
32  // decorate message based on status
33  std::string result;
34  if ( status_ ) {
35  result = "[+]\t";
36  }
37  else {
38  result = "[-]\t";
39  }
40 
41  // =-=-=-=-=-=-=-
42  // only keep the file name the path back to iRODS
43  std::string line_info;
44  try { //replace with std::to_string when we have c++14
45  line_info = _file + ":" + boost::lexical_cast<std::string>( _line ) + ":" + _fcn;
46  }
47  catch ( const boost::bad_lexical_cast& ) {
48  line_info = _file + ":<unknown line number>:" + _fcn;
49  }
50 
51  size_t pos = line_info.find( error::iRODS_token_ );
52  if ( std::string::npos != pos ) {
53  line_info = line_info.substr( pos );
54  }
55 
56  // =-=-=-=-=-=-=-
57  // get the rods error and errno string
58  char* errno_str = NULL;
59  const char* irods_err = rodsErrorName( code_, &errno_str );
60 
61  // =-=-=-=-=-=-=-
62  // compose resulting message given all components
63  result += line_info + error::colon_token_ +
64  error::status_token_ + irods_err + "] errno [" + errno_str + "]"
65  + " -- message [" + message_ + "]";
66 
67  free( errno_str );
68  return result;
69 
70  } // build_result_string
71 
72 // =-=-=-=-=-=-=-
73 // public - default constructor
74  error::error( ) : status_( false ), code_( 0 ), message_( "" ) {
75  } // ctor
76 
77 // =-=-=-=-=-=-=-
78 // public - useful constructor
80  bool _status,
81  long long _code,
82  std::string _msg,
83  std::string _file,
84  int _line,
85  std::string _fcn ) :
86  status_( _status ),
87  code_( _code ),
88  message_( _msg ) {
89 
90  // =-=-=-=-=-=-=-
91  // cache message on message stack
92  if ( !_msg.empty() ) {
93  result_stack_.push_back( build_result_string( _file, _line, _fcn ) );
94  }
95 
96  } // ctor
97 
99  bool _status,
100  long long _code,
101  boost::format _msg,
102  std::string _file,
103  int _line,
104  std::string _fcn ) :
105  error::error( _status, _code, _msg.str(), _file, _line, _fcn ) {}
106 
107 // =-=-=-=-=-=-=-
108 // public - deprecated constructor - since 4.0.3
110  bool ,
111  long long ,
112  std::string _msg,
113  std::string _file,
114  int _line,
115  std::string _fcn,
116  const error& _rhs ) :
117  status_( _rhs.status() ),
118  code_( _rhs.code() ),
119  message_( _msg ) {
120  // =-=-=-=-=-=-=-
121  // cache RHS vector into our vector first
123 
124  // =-=-=-=-=-=-=-
125  // cache message on message stack
126  result_stack_.push_back( build_result_string( _file, _line, _fcn ) );
127 
128  } // ctor
129 
130 // =-=-=-=-=-=-=-
131 // public - useful constructor
133  std::string _msg,
134  std::string _file,
135  int _line,
136  std::string _fcn,
137  const error& _rhs ) :
138  error(_rhs) {
139 
140  if (!_msg.empty()) {
141  message_ = _msg;
142  }
143 
144  if (exception_) {
145  exception_->add_message(_msg + ": " + build_result_string( _file, _line, _fcn ));
146  return;
147  }
148  result_stack_.push_back(build_result_string(_file, _line, _fcn));
149  } // ctor
150 
151 // =-=-=-=-=-=-=-
152 // public - copy constructor
153  error::error( const error& _rhs ) {
154  status_ = _rhs.status_;
155  code_ = _rhs.code_;
156  message_ = _rhs.message_;
158  exception_ = _rhs.exception_;
159  } // cctor
160 
161 // =-=-=-=-=-=-=-
162 // public - constructor from exception
163  error::error( const exception& _exc ) :
164  status_(false),
165  code_(_exc.code()),
166  exception_(_exc) {
167  } // cctor
168 
169 // =-=-=-=-=-=-=-
170 // public - Destructor
172  } // dtor
173 
174 // =-=-=-=-=-=-=-
175 // public - Assignment Operator
176  error& error::operator=( const error& _rhs ) {
177  status_ = _rhs.status_;
178  code_ = _rhs.code_;
179  message_ = _rhs.message_;
181  exception_ = _rhs.exception_;
182  return *this;
183  } // assignment operator
184 
185 // =-=-=-=-=-=-=-
186 // public - return the status of this error object
187  bool error::status( ) const {
188  return status_;
189 
190  } // status
191 
192 // =-=-=-=-=-=-=-
193 // public - return the code of this error object
194  long long error::code( ) const {
195  return code_;
196 
197  } // code
198 
199 // =-=-=-=-=-=-=-
200 // public - return the composite result for logging, etc.
201  std::string error::result() const {
202  if ( exception_ ) {
203  return exception_->what();
204  }
205 
206  // compose single string of the result stack for print out
207  std::string result;
208  std::string tabs = "";
209  for (auto rit = result_stack_.rbegin() ; rit != result_stack_.rend(); ++rit)
210  {
211  if (rit != result_stack_.rbegin())
212  {
213  result += "\n";
214  }
215  result += tabs;
216  result += *rit;
217  tabs += "\t";
218 
219  }
220 
221  // add extra newline for formatting
222  result += "\n\n";
223  return result;
224 
225  } // result
226 
227 // =-=-=-=-=-=-=-
228 // public - return a user-consumable composite result.
229  std::string error::user_result() const {
230  if ( exception_ ) {
231  return exception_->what();
232  }
233 
234  // compose single string of the result stack for print out
235  std::string result;
236  for (auto rit = result_stack_.rbegin() ; rit != result_stack_.rend(); ++rit)
237  {
238  if (rit != result_stack_.rbegin())
239  {
240  result += "\n";
241  }
242  std::string msg = *rit;
243  std::string tok = error::colon_token_;
244  tok += error::status_token_;
245  size_t pos = msg.find( tok );
246  if ( std::string::npos != pos ) {
247  msg = msg.substr( pos+tok.size() );
248  }
249  result += msg;
250  }
251  result += "\n";
252  return result;
253 
254  } // result
255 
256 // =-=-=-=-=-=-=-
257 // public - return the status_ - deprecated in 4.0.3
258  bool error::ok() {
259  return status_;
260  } // ok
261 
262 // =-=-=-=-=-=-=-
263 // public - return the status_
264  bool error::ok() const {
265  return status_;
266  } // ok
267 
268 
270  bool expr_,
271  long long code_,
272  const std::string& file_,
273  const std::string& function_,
274  const std::string& format_,
275  int line_,
276  ... ) {
277  error result = SUCCESS();
278  if ( !expr_ ) {
279  va_list ap;
280  va_start( ap, line_ );
281  const int buffer_size = 4096;
282  char buffer[buffer_size];
283  vsnprintf( buffer, buffer_size, format_.c_str(), ap );
284  va_end( ap );
285  std::stringstream msg;
286  msg << buffer;
287  result = error( false, code_, msg.str(), file_, line_, function_ );
288  }
289  return result;
290  }
291 
293  const error& _prev_error,
294  const std::string& _file,
295  const std::string& _function,
296  const std::string& _format,
297  int _line,
298  ... ) {
299  std::stringstream msg;
300  if ( !_prev_error.ok() ) {
301  va_list ap;
302  va_start( ap, _line );
303  const int buffer_size = 4096;
304  char buffer[buffer_size];
305  vsnprintf( buffer, buffer_size, _format.c_str(), ap );
306  va_end( ap );
307  msg << buffer;
308  }
309  return error( msg.str(), _file, _line, _function, _prev_error );
310  }
311 
312 }; // namespace irods
NULL
#define NULL
Definition: rodsDef.h:70
irods::error::user_result
std::string user_result() const
Definition: irods_error.cpp:229
irods::error::status_
bool status_
Definition: irods_error.hpp:91
irods::error::error
error()
Definition: irods_error.cpp:74
irods::assert_pass
error assert_pass(const error &_error, const std::string &_file, const std::string &_function, const std::string &_format, int line_,...)
Definition: irods_error.cpp:292
irods::error::colon_token_
static const char * colon_token_
Definition: irods_error.hpp:101
irods::error::code_
long long code_
Definition: irods_error.hpp:92
rodsErrorName
const char * rodsErrorName(int errorValue, char **subName)
Definition: rodsLog.cpp:373
irods::error::exception_
boost::optional< exception > exception_
Definition: irods_error.hpp:95
irods::error::~error
~error()
Definition: irods_error.cpp:171
SUCCESS
#define SUCCESS()
Definition: irods_error.hpp:121
irods::error::code
long long code() const
Definition: irods_error.cpp:194
irods::error::status_token_
static const char * status_token_
Definition: irods_error.hpp:102
irods
Definition: apiHandler.hpp:35
irods.pypyodbc.buffer
buffer
Definition: pypyodbc.py:46
irods::error::message_
std::string message_
Definition: irods_error.hpp:93
irods.pypyodbc.status
status
Definition: pypyodbc.py:467
irods::assert_error
error assert_error(bool expr_, long long code_, const std::string &file_, const std::string &function_, const std::string &format_, int line_,...)
Definition: irods_error.cpp:269
rodsLog.h
irods::error
Definition: irods_error.hpp:23
irods::error::build_result_string
std::string build_result_string(std::string, int, std::string)
Definition: irods_error.cpp:27
error
int error
Definition: filesystem.cpp:101
irods::exception
Definition: irods_exception.hpp:15
irods::error::status
bool status() const
Definition: irods_error.cpp:187
irods_error.hpp
irods::error::iRODS_token_
static const char * iRODS_token_
Definition: irods_error.hpp:100
irods::error::ok
bool ok()
Definition: irods_error.cpp:258
irods::error::result
std::string result() const
Definition: irods_error.cpp:201
irods::error::result_stack_
std::vector< std::string > result_stack_
Definition: irods_error.hpp:94
irods::error::operator=
error & operator=(const error &)
Definition: irods_error.cpp:176