libzypp  17.35.15
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 ZYPP_API 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 ) ZYPP_API;
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>
400  std::exception_ptr do_ZYPP_EXCPT_PTR( TExcpt && excpt_r, const CodeLocation & where_r )
401  {
403  excpt_r.relocate( where_r );
404  Exception::log( excpt_r, where_r, "THROW (EXCPTR): " );
405  } else {
406  Exception::log( typeid(excpt_r).name(), where_r, "THROW (EXCPTR): " );
407  }
408  return std::make_exception_ptr<std::decay_t<TExcpt>>( std::forward<TExcpt>(excpt_r) );
409  }
410 
412  std::exception_ptr do_ZYPP_FWD_EXCPT_PTR( const std::exception_ptr & excpt_r, const CodeLocation & where_r );
413 
414 
415  } // namespace exception_detail
417 
424 #define ZYPP_THROW(EXCPT)\
425  ::zypp::exception_detail::do_ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION )
426 
428 #define ZYPP_EXCPT_PTR(EXCPT)\
429  ::zypp::exception_detail::do_ZYPP_EXCPT_PTR( EXCPT, ZYPP_EX_CODELOCATION )
430 
432 #define ZYPP_FWD_EXCPT(EXCPT)\
433  ::zypp::exception_detail::do_ZYPP_FWD_EXCPT_PTR( EXCPT, ZYPP_EX_CODELOCATION )
434 
436 #define ZYPP_FWD_CURRENT_EXCPT()\
437  ::zypp::exception_detail::do_ZYPP_FWD_EXCPT_PTR( std::current_exception(), ZYPP_EX_CODELOCATION )
438 
440 #define ZYPP_CAUGHT(EXCPT)\
441  ::zypp::exception_detail::do_ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION )
442 
444 #define ZYPP_RETHROW(EXCPT)\
445  ::zypp::exception_detail::do_ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION )
446 
447 
449 #define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
450  ZYPP_THROW( EXCPTTYPE( MSG ) )
451 
453 #define ZYPP_THROW_ERRNO(EXCPTTYPE)\
454  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
455 
457 #define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\
458  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) )
459 
461 #define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\
462  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) )
463 
465 #define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\
466  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) )
467 
468 
470 } // namespace zypp
472 #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: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
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:331
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:308
HistorySize historySize() const
The size of the history list.
Definition: Exception.h:267
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:216
void moveToHistory(TContainer &&msgc_r)
addHistory from string container types (oldest first) moving
Definition: Exception.h:248
std::exception_ptr do_ZYPP_FWD_EXCPT_PTR(const std::exception_ptr &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_FWD_CURRENT_EXCPT().
Definition: Exception.cc:60
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:45
History::const_iterator HistoryIterator
Definition: Exception.h:153
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:335
bool historyEmpty() const
Whether the history list is empty.
Definition: Exception.h:263
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
Definition: Capability.cc:580
History _history
Definition: Exception.h:309
CodeLocation(std::string file_r, std::string func_r, unsigned line_r)
Ctor.
Definition: Exception.h:45
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__
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::exception_ptr do_ZYPP_EXCPT_PTR(TExcpt &&excpt_r, const CodeLocation &where_r)
Helper for ZYPP_EXCPT_PTR( Exception ).
Definition: Exception.h:400
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: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::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