libzypp  17.32.4
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 #include <utility>
22 
23 #include <zypp-core/base/Errno.h>
24 
26 namespace zypp
27 {
28  namespace exception_detail
30  {
31 
35  struct CodeLocation
36  {
37  friend std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
38 
41  : _line( 0 )
42  {}
43 
45  CodeLocation( std::string file_r,
46  std::string func_r,
47  unsigned line_r )
48  : _file(std::move( file_r )), _func(std::move( func_r )), _line( line_r )
49  {}
50 
52  std::string asString() const;
53 
54  private:
55  std::string _file;
56  std::string _func;
57  unsigned _line;
58  };
60 
62  //#define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(__FILE__,__FUNCTION__,__LINE__)
63 #define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(( *__FILE__ == '/' ? strrchr( __FILE__, '/' ) + 1 : __FILE__ ),__FUNCTION__,__LINE__)
64 
66  std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
67 
69  } // namespace exception_detail
71 
73  //
74  // CLASS NAME : Exception
146  class Exception : public std::exception
147  {
148  friend std::ostream & operator<<( std::ostream & str, const Exception & obj );
149 
150  public:
152  using History = std::list<std::string>;
153  using HistoryIterator = History::const_iterator;
155 
159  Exception();
160 
164  Exception( const std::string & msg_r );
166  Exception( std::string && msg_r );
167 
172  Exception( const std::string & msg_r, const Exception & history_r );
174  Exception( std::string && msg_r, const Exception & history_r );
176  Exception( const std::string & msg_r, Exception && history_r );
178  Exception( std::string && msg_r, Exception && history_r );
179 
181  ~Exception() throw() override;
182 
184  const CodeLocation & where() const
185  { return _where; }
186 
188  void relocate( const CodeLocation & where_r ) const
189  { _where = where_r; }
190 
196  const std::string & msg() const
197  { return _msg; }
198 
200  std::string asString() const;
201 
205  std::string asUserString() const;
206 
207  public:
215 
217  void remember( const Exception & old_r );
219  void remember( Exception && old_r );
220 
222  void remember( std::exception_ptr old_r );
223 
228  void remember( const std::string & msg_r )
229  { addHistory( msg_r ); }
231  void remember( std::string && msg_r )
232  { addHistory( std::move(msg_r) ); }
233 
235  void addHistory( const std::string & msg_r );
237  void addHistory( std::string && msg_r );
238 
240  template<class TContainer>
241  void addToHistory( const TContainer & msgc_r )
242  {
243  for ( const std::string & el : msgc_r )
244  addHistory( el );
245  }
247  template<class TContainer>
248  void moveToHistory( TContainer && msgc_r )
249  {
250  for ( std::string & el : msgc_r )
251  addHistory( std::move(el) );
252  }
253 
256  { return _history.begin(); }
257 
260  { return _history.end(); }
261 
263  bool historyEmpty() const
264  { return _history.empty(); }
265 
268  { return _history.size(); }
269 
280  std::string historyAsString() const;
281 
283  std::string asUserHistory() const;
285 
286  protected:
287 
289  virtual std::ostream & dumpOn( std::ostream & str ) const;
290 
291  public:
293  static std::string strErrno( int errno_r );
295  static std::string strErrno( int errno_r, std::string msg_r );
296 
297  public:
301  static void log( const Exception & excpt_r, const CodeLocation & where_r,
302  const char *const prefix_r );
304  static void log( const char * typename_r, const CodeLocation & where_r,
305  const char *const prefix_r );
306  private:
308  std::string _msg;
310 
312  const char * what() const throw() override
313  { return _msg.c_str(); }
314 
319  std::ostream & dumpError( std::ostream & str ) const;
320  };
322 
324  std::ostream & operator<<( std::ostream & str, const Exception & obj );
325 
327  namespace exception_detail
328  {
330  template<class TExcpt>
332 
334  template<class TExcpt>
336 
337 
339  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
340  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
341  template<class TExcpt, EnableIfIsException<TExcpt>>
342  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
343  {
344  excpt_r.relocate( where_r );
345  Exception::log( excpt_r, where_r, "THROW: " );
346  throw( excpt_r );
347  }
348 
350  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
351  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
352  template<class TExcpt, EnableIfNotException<TExcpt>>
353  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
354  {
355  Exception::log( typeid(excpt_r).name(), where_r, "THROW: " );
356  throw( excpt_r );
357  }
358 
359 
361  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
362  void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
363  {
364  Exception::log( excpt_r, where_r, "CAUGHT: " );
365  }
366 
368  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
369  void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
370  {
371  Exception::log( typeid(excpt_r).name(), where_r, "CAUGHT: " );
372  }
373 
374 
376  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
377  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
378  template<class TExcpt, EnableIfIsException<TExcpt>>
379  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
380  {
381  Exception::log( excpt_r, where_r, "RETHROW: " );
382  excpt_r.relocate( where_r );
383  throw;
384  }
385 
387  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
388  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
389  template<class TExcpt, EnableIfNotException<TExcpt>>
390  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
391  {
392  Exception::log( excpt_r, where_r, "RETHROW: " );
393  throw;
394  }
395 
396  void do_ZYPP_RETHROW( const std::exception_ptr & excpt_r, const CodeLocation & where_r );
397 
399  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
400  std::exception_ptr do_ZYPP_EXCPT_PTR( TExcpt && excpt_r, const CodeLocation & where_r );
401  template<class TExcpt, EnableIfIsException<TExcpt>>
402  std::exception_ptr do_ZYPP_EXCPT_PTR( TExcpt && excpt_r, const CodeLocation & where_r )
403  {
404  excpt_r.relocate( where_r );
405  Exception::log( excpt_r, where_r, "EXCPTR: " );
406  return std::make_exception_ptr( std::forward<TExcpt>(excpt_r) );
407  }
408 
410  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
411  std::exception_ptr do_ZYPP_EXCPT_PTR( TExcpt && excpt_r, const CodeLocation & where_r );
412  template<class TExcpt, EnableIfNotException<TExcpt>>
413  std::exception_ptr do_ZYPP_EXCPT_PTR( TExcpt && excpt_r, const CodeLocation & where_r )
414  {
415  Exception::log( typeid(excpt_r).name(), where_r, "EXCPTR: " );
416  return std::make_exception_ptr( std::forward<TExcpt>(excpt_r) );
417  }
418 
419 
420  } // namespace exception_detail
422 
429 #define ZYPP_THROW(EXCPT)\
430  ::zypp::exception_detail::do_ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION )
431 
433 #define ZYPP_EXCPT_PTR(EXCPT)\
434  ::zypp::exception_detail::do_ZYPP_EXCPT_PTR( EXCPT, ZYPP_EX_CODELOCATION )
435 
437 #define ZYPP_CAUGHT(EXCPT)\
438  ::zypp::exception_detail::do_ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION )
439 
441 #define ZYPP_RETHROW(EXCPT)\
442  ::zypp::exception_detail::do_ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION )
443 
444 
446 #define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
447  ZYPP_THROW( EXCPTTYPE( MSG ) )
448 
450 #define ZYPP_THROW_ERRNO(EXCPTTYPE)\
451  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
452 
454 #define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\
455  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) )
456 
458 #define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\
459  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) )
460 
462 #define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\
463  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) )
464 
465 
467 } // namespace zypp
469 #endif // ZYPP_BASE_EXCEPTION_H
HistoryIterator historyEnd() const
Iterator pointing behind the last message.
Definition: Exception.h:259
void addToHistory(const TContainer &msgc_r)
addHistory from string container types (oldest first)
Definition: Exception.h:241
void do_ZYPP_CAUGHT(const TExcpt &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_THROW( Exception ).
Definition: Exception.h:362
void do_ZYPP_RETHROW(const std::exception_ptr &excpt_r, const CodeLocation &where_r)
Definition: Exception.cc:41
std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition: Exception.cc:38
CodeLocation _where
Definition: Exception.h:307
History::size_type HistorySize
Definition: Exception.h:154
virtual std::ostream & dumpOn(std::ostream &str) const
Overload this to print a proper error message.
Definition: Exception.cc:180
static std::string strErrno(int errno_r)
Make a string from errno_r.
Definition: Exception.cc:190
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition: Exception.cc:159
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
Definition: Arch.h:363
std::enable_if_t< std::is_base_of_v< Exception, TExcpt >, int > EnableIfIsException
SFINAE: Hide template signature unless TExcpt is derived from Exception.
Definition: Exception.h:331
std::string _msg
Definition: Exception.h:308
HistorySize historySize() const
The size of the history list.
Definition: Exception.h:267
void remember(const Exception &old_r)
Store an other Exception as history.
Definition: Exception.cc:124
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:199
void moveToHistory(TContainer &&msgc_r)
addHistory from string container types (oldest first) moving
Definition: Exception.h:248
HistoryIterator historyBegin() const
Iterator pointing to the most recent message.
Definition: Exception.h:255
typename enable_if< B, T >::type enable_if_t
Definition: TypeTraits.h:42
History::const_iterator HistoryIterator
Definition: Exception.h:153
std::string asString() const
Error message provided by dumpOn as string.
Definition: Exception.cc:94
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition: Exception.cc:110
std::string asUserString() const
Translated error message as string suitable for the user.
Definition: Exception.cc:101
friend std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition: Exception.cc:38
std::enable_if_t< !std::is_base_of_v< Exception, TExcpt >, int > EnableIfNotException
SFINAE: Hide template signature if TExcpt is derived from Exception.
Definition: Exception.h:335
const CodeLocation & where() const
Return CodeLocation.
Definition: Exception.h:184
bool historyEmpty() const
Whether the history list is empty.
Definition: Exception.h:263
std::string historyAsString() const
The history as string.
Definition: Exception.cc:165
History _history
Definition: Exception.h:309
CodeLocation(std::string file_r, std::string func_r, unsigned line_r)
Ctor.
Definition: Exception.h:45
Exception()
Default ctor.
Definition: Exception.cc:64
void remember(const std::string &msg_r)
Remembering a plain string is most probably not wanted - we addHistory.
Definition: Exception.h:228
const char * what() const override
Return message string.
Definition: Exception.h:312
struct zypp::media::MediaBlock __attribute__
friend std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:186
void remember(std::string &&msg_r)
Definition: Exception.h:231
Base class for Exception.
Definition: Exception.h:146
void do_ZYPP_THROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW( Exception ).
Definition: Exception.h:342
std::ostream & dumpError(std::ostream &str) const
Called by std::ostream & operator<<.
Definition: Exception.cc:183
exception_detail::CodeLocation CodeLocation
Definition: Exception.h:151
~Exception() override
Dtor.
Definition: Exception.cc:91
Keep FILE, FUNCTION and LINE.
Definition: Exception.h:35
std::list< std::string > History
Definition: Exception.h:152
void relocate(const CodeLocation &where_r) const
Exchange location on rethrow.
Definition: Exception.h:188
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
SolvableIdType size_type
Definition: PoolMember.h:126
std::exception_ptr do_ZYPP_EXCPT_PTR(TExcpt &&excpt_r, const CodeLocation &where_r)
Helper for ZYPP_EXCPT_PTR( Exception ).
Definition: Exception.h:402
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:196