libzypp  17.31.31
Exception.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_BASE_EXCEPTION_H
13 #define ZYPP_BASE_EXCEPTION_H
14 
15 #include <iosfwd>
16 #include <string>
17 #include <list>
18 #include <stdexcept>
19 #include <typeinfo>
20 #include <type_traits>
21 
22 #include <zypp-core/base/Errno.h>
23 
25 namespace zypp
26 {
27  namespace exception_detail
29  {
30 
34  struct CodeLocation
35  {
36  friend std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
37 
40  : _line( 0 )
41  {}
42 
44  CodeLocation( const std::string & file_r,
45  const std::string & func_r,
46  unsigned line_r )
47  : _file( file_r ), _func( func_r ), _line( line_r )
48  {}
49 
51  std::string asString() const;
52 
53  private:
54  std::string _file;
55  std::string _func;
56  unsigned _line;
57  };
59 
61  //#define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(__FILE__,__FUNCTION__,__LINE__)
62 #define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(( *__FILE__ == '/' ? strrchr( __FILE__, '/' ) + 1 : __FILE__ ),__FUNCTION__,__LINE__)
63 
65  std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
66 
68  } // namespace exception_detail
70 
72  //
73  // CLASS NAME : Exception
145  class Exception : public std::exception
146  {
147  friend std::ostream & operator<<( std::ostream & str, const Exception & obj );
148 
149  public:
151  typedef std::list<std::string> History;
152  typedef History::const_iterator HistoryIterator;
154 
158  Exception();
159 
163  Exception( const std::string & msg_r );
165  Exception( std::string && msg_r );
166 
171  Exception( const std::string & msg_r, const Exception & history_r );
173  Exception( std::string && msg_r, const Exception & history_r );
175  Exception( const std::string & msg_r, Exception && history_r );
177  Exception( std::string && msg_r, Exception && history_r );
178 
180  virtual ~Exception() throw();
181 
183  const CodeLocation & where() const
184  { return _where; }
185 
187  void relocate( const CodeLocation & where_r ) const
188  { _where = where_r; }
189 
195  const std::string & msg() const
196  { return _msg; }
197 
199  std::string asString() const;
200 
204  std::string asUserString() const;
205 
206  public:
214 
216  void remember( const Exception & old_r );
218  void remember( Exception && old_r );
219 
221  void remember( std::exception_ptr old_r );
222 
227  void remember( const std::string & msg_r )
228  { addHistory( msg_r ); }
230  void remember( std::string && msg_r )
231  { addHistory( std::move(msg_r) ); }
232 
234  void addHistory( const std::string & msg_r );
236  void addHistory( std::string && msg_r );
237 
239  template<class TContainer>
240  void addToHistory( const TContainer & msgc_r )
241  {
242  for ( const std::string & el : msgc_r )
243  addHistory( el );
244  }
246  template<class TContainer>
247  void moveToHistory( TContainer && msgc_r )
248  {
249  for ( std::string & el : msgc_r )
250  addHistory( std::move(el) );
251  }
252 
255  { return _history.begin(); }
256 
259  { return _history.end(); }
260 
262  bool historyEmpty() const
263  { return _history.empty(); }
264 
267  { return _history.size(); }
268 
279  std::string historyAsString() const;
280 
282  std::string asUserHistory() const;
284 
285  protected:
286 
288  virtual std::ostream & dumpOn( std::ostream & str ) const;
289 
290  public:
292  static std::string strErrno( int errno_r );
294  static std::string strErrno( int errno_r, const std::string & msg_r );
296  static std::string strErrno( int errno_r, std::string && msg_r );
297 
298  public:
302  static void log( const Exception & excpt_r, const CodeLocation & where_r,
303  const char *const prefix_r );
305  static void log( const char * typename_r, const CodeLocation & where_r,
306  const char *const prefix_r );
307  private:
309  std::string _msg;
311 
313  virtual const char * what() const throw()
314  { return _msg.c_str(); }
315 
320  std::ostream & dumpError( std::ostream & str ) const;
321  };
323 
325  std::ostream & operator<<( std::ostream & str, const Exception & obj );
326 
328  namespace exception_detail
329  {
331  template<class TExcpt>
332  using EnableIfIsException = typename std::enable_if< std::is_base_of<Exception,TExcpt>::value, int>::type;
333 
335  template<class TExcpt>
336  using EnableIfNotException = typename std::enable_if< !std::is_base_of<Exception,TExcpt>::value, int>::type;
337 
338 
340  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
341  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
342  template<class TExcpt, EnableIfIsException<TExcpt>>
343  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
344  {
345  excpt_r.relocate( where_r );
346  Exception::log( excpt_r, where_r, "THROW: " );
347  throw( excpt_r );
348  }
349 
351  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
352  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
353  template<class TExcpt, EnableIfNotException<TExcpt>>
354  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
355  {
356  Exception::log( typeid(excpt_r).name(), where_r, "THROW: " );
357  throw( excpt_r );
358  }
359 
360 
362  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
363  void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
364  {
365  Exception::log( excpt_r, where_r, "CAUGHT: " );
366  }
367 
369  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
370  void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
371  {
372  Exception::log( typeid(excpt_r).name(), where_r, "CAUGHT: " );
373  }
374 
375 
377  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
378  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
379  template<class TExcpt, EnableIfIsException<TExcpt>>
380  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
381  {
382  Exception::log( excpt_r, where_r, "RETHROW: " );
383  excpt_r.relocate( where_r );
384  throw;
385  }
386 
388  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
389  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
390  template<class TExcpt, EnableIfNotException<TExcpt>>
391  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
392  {
393  Exception::log( excpt_r, where_r, "RETHROW: " );
394  throw;
395  }
396 
398  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
399  std::exception_ptr do_ZYPP_EXCPT_PTR( const TExcpt & excpt_r, const CodeLocation & where_r );
400  template<class TExcpt, EnableIfIsException<TExcpt>>
401  std::exception_ptr do_ZYPP_EXCPT_PTR( const TExcpt & excpt_r, const CodeLocation & where_r )
402  {
403  excpt_r.relocate( where_r );
404  Exception::log( excpt_r, where_r, "EXCPTR: " );
405  return std::make_exception_ptr( excpt_r );
406  }
407 
409  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
410  std::exception_ptr do_ZYPP_EXCPT_PTR( const TExcpt & excpt_r, const CodeLocation & where_r );
411  template<class TExcpt, EnableIfNotException<TExcpt>>
412  std::exception_ptr do_ZYPP_EXCPT_PTR( const TExcpt & excpt_r, const CodeLocation & where_r )
413  {
414  Exception::log( typeid(excpt_r).name(), where_r, "EXCPTR: " );
415  return std::make_exception_ptr( excpt_r );
416  }
417 
418 
419  } // namespace exception_detail
421 
428 #define ZYPP_THROW(EXCPT)\
429  ::zypp::exception_detail::do_ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION )
430 
432 #define ZYPP_EXCPT_PTR(EXCPT)\
433  ::zypp::exception_detail::do_ZYPP_EXCPT_PTR( EXCPT, ZYPP_EX_CODELOCATION )
434 
436 #define ZYPP_CAUGHT(EXCPT)\
437  ::zypp::exception_detail::do_ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION )
438 
440 #define ZYPP_RETHROW(EXCPT)\
441  ::zypp::exception_detail::do_ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION )
442 
443 
445 #define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
446  ZYPP_THROW( EXCPTTYPE( MSG ) )
447 
449 #define ZYPP_THROW_ERRNO(EXCPTTYPE)\
450  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
451 
453 #define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\
454  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) )
455 
457 #define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\
458  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) )
459 
461 #define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\
462  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) )
463 
464 
466 } // namespace zypp
468 #endif // ZYPP_BASE_EXCEPTION_H
HistoryIterator historyEnd() const
Iterator pointing behind the last message.
Definition: Exception.h:258
void addToHistory(const TContainer &msgc_r)
addHistory from string container types (oldest first)
Definition: Exception.h:240
void do_ZYPP_CAUGHT(const TExcpt &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_THROW( Exception ).
Definition: Exception.h:363
typename std::enable_if< !std::is_base_of< Exception, TExcpt >::value, int >::type EnableIfNotException
SFINAE: Hide template signature if TExcpt is derived from Exception.
Definition: Exception.h:336
std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition: Exception.cc:38
CodeLocation _where
Definition: Exception.h:308
virtual std::ostream & dumpOn(std::ostream &str) const
Overload this to print a proper error message.
Definition: Exception.cc:161
static std::string strErrno(int errno_r)
Make a string from errno_r.
Definition: Exception.cc:171
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition: Exception.cc:140
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
void do_ZYPP_RETHROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW( Exception ).
Definition: Exception.h:380
virtual const char * what() const
Return message string.
Definition: Exception.h:313
std::string _msg
Definition: Exception.h:309
HistorySize historySize() const
The size of the history list.
Definition: Exception.h:266
void remember(const Exception &old_r)
Store an other Exception as history.
Definition: Exception.cc:105
static void log(const Exception &excpt_r, const CodeLocation &where_r, const char *const prefix_r)
Drop a logline on throw, catch or rethrow.
Definition: Exception.cc:183
void moveToHistory(TContainer &&msgc_r)
addHistory from string container types (oldest first) moving
Definition: Exception.h:247
HistoryIterator historyBegin() const
Iterator pointing to the most recent message.
Definition: Exception.h:254
std::list< std::string > History
Definition: Exception.h:151
std::string asString() const
Error message provided by dumpOn as string.
Definition: Exception.cc:75
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition: Exception.cc:91
std::string asUserString() const
Translated error message as string suitable for the user.
Definition: Exception.cc:82
friend std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition: Exception.cc:38
const CodeLocation & where() const
Return CodeLocation.
Definition: Exception.h:183
typename std::enable_if< std::is_base_of< Exception, TExcpt >::value, int >::type EnableIfIsException
SFINAE: Hide template signature unless TExcpt is derived from Exception.
Definition: Exception.h:332
bool historyEmpty() const
Whether the history list is empty.
Definition: Exception.h:262
std::string historyAsString() const
The history as string.
Definition: Exception.cc:146
virtual ~Exception()
Dtor.
Definition: Exception.cc:72
History _history
Definition: Exception.h:310
SolvableIdType size_type
Definition: PoolMember.h:126
Exception()
Default ctor.
Definition: Exception.cc:45
void remember(const std::string &msg_r)
Remembering a plain string is most probably not wanted - we addHistory.
Definition: Exception.h:227
CodeLocation(const std::string &file_r, const std::string &func_r, unsigned line_r)
Ctor.
Definition: Exception.h:44
std::exception_ptr do_ZYPP_EXCPT_PTR(const TExcpt &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_EXCPT_PTR( Exception ).
Definition: Exception.h:401
struct zypp::media::MediaBlock __attribute__
friend std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:167
void remember(std::string &&msg_r)
Definition: Exception.h:230
Base class for Exception.
Definition: Exception.h:145
History::size_type HistorySize
Definition: Exception.h:153
exception_detail::CodeLocation CodeLocation
Definition: Exception.h:150
void do_ZYPP_THROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW( Exception ).
Definition: Exception.h:343
std::ostream & dumpError(std::ostream &str) const
Called by std::ostream & operator<<.
Definition: Exception.cc:164
Keep FILE, FUNCTION and LINE.
Definition: Exception.h:34
void relocate(const CodeLocation &where_r) const
Exchange location on rethrow.
Definition: Exception.h:187
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
History::const_iterator HistoryIterator
Definition: Exception.h:152
std::string asString() const
Location as string.
Definition: Exception.cc:30
const std::string & msg() const
Return the message string provided to the ctor.
Definition: Exception.h:195