libzypp  17.37.5
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 
44  ~CodeLocation() = default;
45  CodeLocation(const CodeLocation &) = default;
46  CodeLocation(CodeLocation &&) = default;
47  CodeLocation &operator=(const CodeLocation &) = default;
48  CodeLocation &operator=(CodeLocation &&) = default;
49 
51  CodeLocation( std::string file_r,
52  std::string func_r,
53  unsigned line_r )
54  : _file(std::move( file_r )), _func(std::move( func_r )), _line( line_r )
55  {}
56 
58  std::string asString() const;
59 
60  private:
61  std::string _file;
62  std::string _func;
63  unsigned _line;
64  };
66 
68  //#define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(__FILE__,__FUNCTION__,__LINE__)
69 #define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(( *__FILE__ == '/' ? strrchr( __FILE__, '/' ) + 1 : __FILE__ ),__FUNCTION__,__LINE__)
70 
72  std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
73 
75  } // namespace exception_detail
77 
79  //
80  // CLASS NAME : Exception
152  class ZYPP_API Exception : public std::exception
153  {
154  friend std::ostream & operator<<( std::ostream & str, const Exception & obj );
155 
156  public:
158  using History = std::list<std::string>;
159  using HistoryIterator = History::const_iterator;
161 
165  Exception();
166 
170  Exception( const std::string & msg_r );
172  Exception( std::string && msg_r );
173 
178  Exception( const std::string & msg_r, const Exception & history_r );
180  Exception( std::string && msg_r, const Exception & history_r );
182  Exception( const std::string & msg_r, Exception && history_r );
184  Exception( std::string && msg_r, Exception && history_r );
185 
187  ~Exception() throw() override;
188 
190  const CodeLocation & where() const
191  { return _where; }
192 
194  void relocate( const CodeLocation & where_r ) const
195  { _where = where_r; }
196 
198  void relocate( CodeLocation &&where_r ) const
199  { _where = std::move(where_r); }
200 
206  const std::string & msg() const
207  { return _msg; }
208 
210  std::string asString() const;
211 
215  std::string asUserString() const;
216 
217  public:
225 
227  void remember( const Exception & old_r );
229  void remember( Exception && old_r );
230 
232  void remember( std::exception_ptr old_r );
233 
238  void remember( const std::string & msg_r )
239  { addHistory( msg_r ); }
241  void remember( std::string && msg_r )
242  { addHistory( std::move(msg_r) ); }
243 
245  void addHistory( const std::string & msg_r );
247  void addHistory( std::string && msg_r );
248 
250  template<class TContainer>
251  void addToHistory( const TContainer & msgc_r )
252  {
253  for ( const std::string & el : msgc_r )
254  addHistory( el );
255  }
257  template<class TContainer>
258  void moveToHistory( TContainer && msgc_r )
259  {
260  for ( std::string & el : msgc_r )
261  addHistory( std::move(el) );
262  }
263 
266  { return _history.begin(); }
267 
270  { return _history.end(); }
271 
273  bool historyEmpty() const
274  { return _history.empty(); }
275 
278  { return _history.size(); }
279 
290  std::string historyAsString() const;
291 
293  std::string asUserHistory() const;
295 
296  protected:
297 
299  virtual std::ostream & dumpOn( std::ostream & str ) const;
300 
301  public:
303  static std::string strErrno( int errno_r );
305  static std::string strErrno( int errno_r, std::string msg_r );
306 
307  public:
311  static void log( const Exception & excpt_r, const CodeLocation & where_r,
312  const char *const prefix_r );
314  static void log( const char * typename_r, const CodeLocation & where_r,
315  const char *const prefix_r );
316  private:
318  std::string _msg;
320 
322  const char * what() const throw() override
323  { return _msg.c_str(); }
324 
329  std::ostream & dumpError( std::ostream & str ) const;
330  };
332 
334  std::ostream & operator<<( std::ostream & str, const Exception & obj ) ZYPP_API;
335 
337  std::ostream & operator<<( std::ostream & str, const std::exception_ptr &excptPtr ) ZYPP_API;
338 
340  namespace exception_detail
341  {
343  template<class TExcpt>
345 
347  template<class TExcpt>
349 
350 
352  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
353  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
354  template<class TExcpt, EnableIfIsException<TExcpt>>
355  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
356  {
357  excpt_r.relocate( where_r );
358  Exception::log( excpt_r, where_r, "THROW: " );
359  throw( excpt_r );
360  }
361 
362  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
363  void do_ZYPP_THROW( const TExcpt & excpt_r, CodeLocation && where_r ) __attribute__((noreturn));
364  template<class TExcpt, EnableIfIsException<TExcpt>>
365  void do_ZYPP_THROW( const TExcpt & excpt_r, CodeLocation && where_r )
366  {
367  Exception::log( excpt_r, where_r, "THROW: " );
368  excpt_r.relocate(std::move(where_r) );
369  throw( excpt_r );
370  }
371 
373  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
374  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
375  template<class TExcpt, EnableIfNotException<TExcpt>>
376  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
377  {
378  Exception::log( typeid(excpt_r).name(), where_r, "THROW: " );
379  throw( excpt_r );
380  }
381 
383  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
384  void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
385  {
386  Exception::log( excpt_r, where_r, "CAUGHT: " );
387  }
388 
390  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
391  void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
392  {
393  Exception::log( typeid(excpt_r).name(), where_r, "CAUGHT: " );
394  }
395 
397  void do_ZYPP_CAUGHT ( const std::exception_ptr & excpt_r, CodeLocation &&where_r );
398 
399 
401  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
402  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
403  template<class TExcpt, EnableIfIsException<TExcpt>>
404  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
405  {
406  Exception::log( excpt_r, where_r, "RETHROW: " );
407  excpt_r.relocate( where_r );
408  throw;
409  }
410 
411  template<class TExcpt, EnableIfIsException<TExcpt> = 0>
412  void do_ZYPP_RETHROW( const TExcpt & excpt_r, CodeLocation && where_r ) __attribute__((noreturn));
413  template<class TExcpt, EnableIfIsException<TExcpt>>
414  void do_ZYPP_RETHROW( const TExcpt & excpt_r, CodeLocation && where_r )
415  {
416  Exception::log( excpt_r, where_r, "RETHROW: " );
417  excpt_r.relocate( std::move(where_r) );
418  throw;
419  }
420 
422  template<class TExcpt, EnableIfNotException<TExcpt> = 0>
423  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
424  template<class TExcpt, EnableIfNotException<TExcpt>>
425  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
426  {
427  Exception::log( excpt_r, where_r, "RETHROW: " );
428  throw;
429  }
430 
431  void do_ZYPP_RETHROW( const std::exception_ptr & excpt_r, const CodeLocation & where_r );
432 
434  template<class TExcpt>
435  std::exception_ptr do_ZYPP_EXCPT_PTR( TExcpt && excpt_r, CodeLocation && where_r )
436  {
438  Exception::log( excpt_r, where_r, "THROW (EXCPTR): " );
439  excpt_r.relocate( std::move(where_r) );
440  } else {
441  Exception::log( typeid(excpt_r).name(), where_r, "THROW (EXCPTR): " );
442  }
443  return std::make_exception_ptr<std::decay_t<TExcpt>>( std::forward<TExcpt>(excpt_r) );
444  }
445 
447  std::exception_ptr do_ZYPP_FWD_EXCPT_PTR( const std::exception_ptr & excpt_r, CodeLocation &&where_r );
448 
449 
450  } // namespace exception_detail
452 
459 #define ZYPP_THROW(EXCPT)\
460  ::zypp::exception_detail::do_ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION )
461 
463 #define ZYPP_EXCPT_PTR(EXCPT)\
464  ::zypp::exception_detail::do_ZYPP_EXCPT_PTR( EXCPT, ZYPP_EX_CODELOCATION )
465 
467 #define ZYPP_FWD_EXCPT(EXCPT)\
468  ::zypp::exception_detail::do_ZYPP_FWD_EXCPT_PTR( EXCPT, ZYPP_EX_CODELOCATION )
469 
471 #define ZYPP_FWD_CURRENT_EXCPT()\
472  ::zypp::exception_detail::do_ZYPP_FWD_EXCPT_PTR( std::current_exception(), ZYPP_EX_CODELOCATION )
473 
475 #define ZYPP_CAUGHT(EXCPT)\
476  ::zypp::exception_detail::do_ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION )
477 
479 #define ZYPP_RETHROW(EXCPT)\
480  ::zypp::exception_detail::do_ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION )
481 
482 
484 #define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
485  ZYPP_THROW( EXCPTTYPE( MSG ) )
486 
488 #define ZYPP_THROW_ERRNO(EXCPTTYPE)\
489  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
490 
492 #define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\
493  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) )
494 
496 #define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\
497  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) )
498 
500 #define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\
501  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) )
502 
503 
505 } // namespace zypp
507 #endif // ZYPP_BASE_EXCEPTION_H
std::string asString(const Patch::Category &obj)
Definition: Patch.cc:122
HistoryIterator historyEnd() const
Iterator pointing behind the last message.
Definition: Exception.h:269
void addToHistory(const TContainer &msgc_r)
addHistory from string container types (oldest first)
Definition: Exception.h:251
void relocate(CodeLocation &&where_r) const
Exchange location on rethrow.
Definition: Exception.h:198
std::exception_ptr do_ZYPP_FWD_EXCPT_PTR(const std::exception_ptr &excpt_r, CodeLocation &&where_r)
Helper for ZYPP_FWD_CURRENT_EXCPT().
Definition: Exception.cc:60
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:317
void do_ZYPP_CAUGHT(const std::exception_ptr &excpt_r, CodeLocation &&where_r)
Helper for std::exception_ptr.
Definition: Exception.cc:77
History::size_type HistorySize
Definition: Exception.h:160
constexpr bool is_base_of_v
Definition: TypeTraits.h:27
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:344
std::string asUserString(VendorSupportOption opt)
converts the support option to a name intended to be printed to the user.
std::string _msg
Definition: Exception.h:318
HistorySize historySize() const
The size of the history list.
Definition: Exception.h:277
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:242
void moveToHistory(TContainer &&msgc_r)
addHistory from string container types (oldest first) moving
Definition: Exception.h:258
HistoryIterator historyBegin() const
Iterator pointing to the most recent message.
Definition: Exception.h:265
typename enable_if< B, T >::type enable_if_t
Definition: TypeTraits.h:45
History::const_iterator HistoryIterator
Definition: Exception.h:159
const Arch Arch_armv7hnl Arch_armv7nhl ZYPP_API
Definition: ResTraits.h:93
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:348
bool historyEmpty() const
Whether the history list is empty.
Definition: Exception.h:273
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
Definition: Capability.cc:580
History _history
Definition: Exception.h:319
CodeLocation & operator=(const CodeLocation &)=default
CodeLocation(std::string file_r, std::string func_r, unsigned line_r)
Ctor.
Definition: Exception.h:51
void remember(const std::string &msg_r)
Remembering a plain string is most probably not wanted - we addHistory.
Definition: Exception.h:238
const char * what() const override
Return message string.
Definition: Exception.h:322
struct zypp::media::MediaBlock __attribute__
void remember(std::string &&msg_r)
Definition: Exception.h:241
Base class for Exception.
Definition: Exception.h:152
void do_ZYPP_THROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW( Exception ).
Definition: Exception.h:355
Keep FILE, FUNCTION and LINE.
Definition: Exception.h:35
typename decay< T >::type decay_t
Definition: TypeTraits.h:42
std::list< std::string > History
Definition: Exception.h:158
void relocate(const CodeLocation &where_r) const
Exchange location on rethrow.
Definition: Exception.h:194
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, CodeLocation &&where_r)
Helper for ZYPP_EXCPT_PTR( Exception ).
Definition: Exception.h:435
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:206