libzypp  17.30.0
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 {
28  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 
224  void addHistory( const std::string & msg_r );
226  void addHistory( std::string && msg_r );
227 
229  template<class TContainer>
230  void addToHistory( const TContainer & msgc_r )
231  {
232  for ( const std::string & el : msgc_r )
233  addHistory( el );
234  }
236  template<class TContainer>
237  void moveToHistory( TContainer && msgc_r )
238  {
239  for ( std::string & el : msgc_r )
240  addHistory( std::move(el) );
241  }
242 
245  { return _history.begin(); }
246 
249  { return _history.end(); }
250 
252  bool historyEmpty() const
253  { return _history.empty(); }
254 
257  { return _history.size(); }
258 
269  std::string historyAsString() const;
270 
272  std::string asUserHistory() const;
274 
275  protected:
276 
278  virtual std::ostream & dumpOn( std::ostream & str ) const;
279 
280  public:
282  static std::string strErrno( int errno_r );
284  static std::string strErrno( int errno_r, const std::string & msg_r );
286  static std::string strErrno( int errno_r, std::string && msg_r );
287 
288  public:
292  static void log( const Exception & excpt_r, const CodeLocation & where_r,
293  const char *const prefix_r );
295  static void log( const char * typename_r, const CodeLocation & where_r,
296  const char *const prefix_r );
297  private:
299  std::string _msg;
301 
303  virtual const char * what() const throw()
304  { return _msg.c_str(); }
305 
310  std::ostream & dumpError( std::ostream & str ) const;
311  };
313 
315  std::ostream & operator<<( std::ostream & str, const Exception & obj );
316 
318  namespace exception_detail
319  {
321  template<class TExcpt>
322  using EnableIfIsException = typename std::enable_if< std::is_base_of<Exception,TExcpt>::value, int>::type;
323 
325  template<class TExcpt>
326  using EnableIfNotException = typename std::enable_if< !std::is_base_of<Exception,TExcpt>::value, int>::type;
327 
328 
330  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
331  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
332  template<class TExcpt, EnableIfIsException<TExcpt>>
333  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
334  {
335  excpt_r.relocate( where_r );
336  Exception::log( excpt_r, where_r, "THROW: " );
337  throw( excpt_r );
338  }
339 
341  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
342  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
343  template<class TExcpt, EnableIfNotException<TExcpt>>
344  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
345  {
346  Exception::log( typeid(excpt_r).name(), where_r, "THROW: " );
347  throw( excpt_r );
348  }
349 
350 
352  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
353  void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
354  {
355  Exception::log( excpt_r, where_r, "CAUGHT: " );
356  }
357 
359  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
360  void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
361  {
362  Exception::log( typeid(excpt_r).name(), where_r, "CAUGHT: " );
363  }
364 
365 
367  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
368  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
369  template<class TExcpt, EnableIfIsException<TExcpt>>
370  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
371  {
372  Exception::log( excpt_r, where_r, "RETHROW: " );
373  excpt_r.relocate( where_r );
374  throw;
375  }
376 
378  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
379  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
380  template<class TExcpt, EnableIfNotException<TExcpt>>
381  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
382  {
383  Exception::log( excpt_r, where_r, "RETHROW: " );
384  throw;
385  }
386 
388  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
389  std::exception_ptr do_ZYPP_EXCPT_PTR( const TExcpt & excpt_r, const CodeLocation & where_r );
390  template<class TExcpt, EnableIfIsException<TExcpt>>
391  std::exception_ptr do_ZYPP_EXCPT_PTR( const TExcpt & excpt_r, const CodeLocation & where_r )
392  {
393  excpt_r.relocate( where_r );
394  Exception::log( excpt_r, where_r, "THROW: " );
395  return std::make_exception_ptr( excpt_r );
396  }
397 
399  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
400  std::exception_ptr do_ZYPP_EXCPT_PTR( const TExcpt & excpt_r, const CodeLocation & where_r );
401  template<class TExcpt, EnableIfNotException<TExcpt>>
402  std::exception_ptr do_ZYPP_EXCPT_PTR( const TExcpt & excpt_r, const CodeLocation & where_r )
403  {
404  Exception::log( typeid(excpt_r).name(), where_r, "THROW: " );
405  return std::make_exception_ptr( excpt_r );
406  }
407 
408 
409  } // namespace exception_detail
411 
418 #define ZYPP_THROW(EXCPT)\
419  ::zypp::exception_detail::do_ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION )
420 
422 #define ZYPP_EXCPT_PTR(EXCPT)\
423  ::zypp::exception_detail::do_ZYPP_EXCPT_PTR( EXCPT, ZYPP_EX_CODELOCATION )
424 
426 #define ZYPP_CAUGHT(EXCPT)\
427  ::zypp::exception_detail::do_ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION )
428 
430 #define ZYPP_RETHROW(EXCPT)\
431  ::zypp::exception_detail::do_ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION )
432 
433 
435 #define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
436  ZYPP_THROW( EXCPTTYPE( MSG ) )
437 
439 #define ZYPP_THROW_ERRNO(EXCPTTYPE)\
440  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
441 
443 #define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\
444  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) )
445 
447 #define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\
448  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) )
449 
451 #define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\
452  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) )
454 
456 } // namespace zypp
458 #endif // ZYPP_BASE_EXCEPTION_H
Base class for Exception.
Definition: Exception.h:146
std::string _msg
Definition: Exception.h:299
static std::string strErrno(int errno_r)
Make a string from errno_r.
Definition: Exception.cc:171
virtual ~Exception()
Dtor.
Definition: Exception.cc:72
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition: Exception.cc:91
std::ostream & dumpError(std::ostream &str) const
Called by std::ostream & operator<<.
Definition: Exception.cc:164
const CodeLocation & where() const
Return CodeLocation.
Definition: Exception.h:183
std::string asUserString() const
Translated error message as string suitable for the user.
Definition: Exception.cc:82
History::size_type HistorySize
Definition: Exception.h:153
CodeLocation _where
Definition: Exception.h:298
History _history
Definition: Exception.h:300
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition: Exception.cc:140
std::string historyAsString() const
The history as string.
Definition: Exception.cc:146
virtual std::ostream & dumpOn(std::ostream &str) const
Overload this to print a proper error message.
Definition: Exception.cc:161
std::string asString() const
Error message provided by dumpOn as string.
Definition: Exception.cc:75
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
HistoryIterator historyBegin() const
Iterator pointing to the most recent message.
Definition: Exception.h:244
std::list< std::string > History
Definition: Exception.h:151
const std::string & msg() const
Return the message string provided to the ctor.
Definition: Exception.h:195
Exception()
Default ctor.
Definition: Exception.cc:45
friend std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:167
void moveToHistory(TContainer &&msgc_r)
addHistory from string container types (oldest first) moving
Definition: Exception.h:237
HistoryIterator historyEnd() const
Iterator pointing behind the last message.
Definition: Exception.h:248
History::const_iterator HistoryIterator
Definition: Exception.h:152
void addToHistory(const TContainer &msgc_r)
addHistory from string container types (oldest first)
Definition: Exception.h:230
virtual const char * what() const
Return message string.
Definition: Exception.h:303
void relocate(const CodeLocation &where_r) const
Exchange location on rethrow.
Definition: Exception.h:187
HistorySize historySize() const
The size of the history list.
Definition: Exception.h:256
bool historyEmpty() const
Whether the history list is empty.
Definition: Exception.h:252
exception_detail::CodeLocation CodeLocation
Definition: Exception.h:150
void remember(const Exception &old_r)
Store an other Exception as history.
Definition: Exception.cc:105
String related utilities and Regular expression matching.
void do_ZYPP_RETHROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW( Exception ).
Definition: Exception.h:370
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:326
void do_ZYPP_CAUGHT(const TExcpt &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_THROW( Exception ).
Definition: Exception.h:353
std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition: Exception.cc:38
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:322
void do_ZYPP_THROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW( Exception ).
Definition: Exception.h:333
std::exception_ptr do_ZYPP_EXCPT_PTR(const TExcpt &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_EXCPT_PTR( Exception ).
Definition: Exception.h:391
SolvableIdType size_type
Definition: PoolMember.h:126
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
Keep FILE, FUNCTION and LINE.
Definition: Exception.h:35
CodeLocation(const std::string &file_r, const std::string &func_r, unsigned line_r)
Ctor.
Definition: Exception.h:44
std::string asString() const
Location as string.
Definition: Exception.cc:30
friend std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition: Exception.cc:38