libzypp  16.22.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 
20 #include "zypp/base/Errno.h"
21 
23 namespace zypp
24 {
25  namespace exception_detail
27  {
28 
32  struct CodeLocation
33  {
34  friend std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
35 
38  : _line( 0 )
39  {}
40 
42  CodeLocation( const std::string & file_r,
43  const std::string & func_r,
44  unsigned line_r )
45  : _file( file_r ), _func( func_r ), _line( line_r )
46  {}
47 
49  std::string asString() const;
50 
51  private:
52  std::string _file;
53  std::string _func;
54  unsigned _line;
55  };
57 
59  //#define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(__FILE__,__FUNCTION__,__LINE__)
60 #define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(( *__FILE__ == '/' ? strrchr( __FILE__, '/' ) + 1 : __FILE__ ),__FUNCTION__,__LINE__)
61 
63  std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
64 
66  } // namespace exception_detail
68 
70  //
71  // CLASS NAME : Exception
143  class Exception : public std::exception
144  {
145  friend std::ostream & operator<<( std::ostream & str, const Exception & obj );
146 
147  public:
149  typedef std::list<std::string> History;
150  typedef History::const_iterator HistoryIterator;
152 
156  Exception();
157 
161  Exception( const std::string & msg_r );
163  Exception( std::string && msg_r );
164 
169  Exception( const std::string & msg_r, const Exception & history_r );
171  Exception( std::string && msg_r, const Exception & history_r );
173  Exception( const std::string & msg_r, Exception && history_r );
175  Exception( std::string && msg_r, Exception && history_r );
176 
178  virtual ~Exception() throw();
179 
181  const CodeLocation & where() const
182  { return _where; }
183 
185  void relocate( const CodeLocation & where_r ) const
186  { _where = where_r; }
187 
193  const std::string & msg() const
194  { return _msg; }
195 
197  std::string asString() const;
198 
202  std::string asUserString() const;
203 
204  public:
212 
214  void remember( const Exception & old_r );
216  void remember( Exception && old_r );
217 
219  void addHistory( const std::string & msg_r );
221  void addHistory( std::string && msg_r );
222 
224  template<class TContainer>
225  void addToHistory( const TContainer & msgc_r )
226  {
227  for ( const std::string & el : msgc_r )
228  addHistory( el );
229  }
231  template<class TContainer>
232  void moveToHistory( TContainer && msgc_r )
233  {
234  for ( std::string & el : msgc_r )
235  addHistory( std::move(el) );
236  }
237 
240  { return _history.begin(); }
241 
244  { return _history.end(); }
245 
247  bool historyEmpty() const
248  { return _history.empty(); }
249 
252  { return _history.size(); }
253 
264  std::string historyAsString() const;
265 
267  std::string asUserHistory() const;
269 
270  protected:
271 
273  virtual std::ostream & dumpOn( std::ostream & str ) const;
274 
275  public:
277  static std::string strErrno( int errno_r );
279  static std::string strErrno( int errno_r, const std::string & msg_r );
281  static std::string strErrno( int errno_r, std::string && msg_r );
282 
283  public:
287  static void log( const Exception & excpt_r, const CodeLocation & where_r,
288  const char *const prefix_r );
289 
290  private:
292  std::string _msg;
294 
296  virtual const char * what() const throw()
297  { return _msg.c_str(); }
298 
303  std::ostream & dumpError( std::ostream & str ) const;
304  };
306 
308  std::ostream & operator<<( std::ostream & str, const Exception & obj );
309 
311  namespace exception_detail
312  {
314  template<class TExcpt>
315  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
316  template<class TExcpt>
317  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
318  {
319  excpt_r.relocate( where_r );
320  Exception::log( excpt_r, where_r, "THROW: " );
321  throw( excpt_r );
322  }
323 
325  template<class TExcpt>
326  void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
327  {
328  Exception::log( excpt_r, where_r, "CAUGHT: " );
329  }
330 
332  template<class TExcpt>
333  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
334  template<class TExcpt>
335  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
336  {
337  Exception::log( excpt_r, where_r, "RETHROW: " );
338  excpt_r.relocate( where_r );
339  throw;
340  }
341  } // namespace exception_detail
343 
350 #define ZYPP_THROW(EXCPT)\
351  ::zypp::exception_detail::do_ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION )
352 
354 #define ZYPP_CAUGHT(EXCPT)\
355  ::zypp::exception_detail::do_ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION )
356 
358 #define ZYPP_RETHROW(EXCPT)\
359  ::zypp::exception_detail::do_ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION )
360 
361 
363 #define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
364  ZYPP_THROW( EXCPTTYPE( MSG ) )
365 
367 #define ZYPP_THROW_ERRNO(EXCPTTYPE)\
368  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
369 
371 #define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\
372  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) )
373 
375 #define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\
376  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) )
377 
379 #define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\
380  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) )
381 
382 
384 } // namespace zypp
386 #endif // ZYPP_BASE_EXCEPTION_H
void addToHistory(const TContainer &msgc_r)
addHistory from string container types (oldest first)
Definition: Exception.h:225
void do_ZYPP_CAUGHT(const TExcpt &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_THROW macros.
Definition: Exception.h:326
std::string historyAsString() const
The history as string.
Definition: Exception.cc:131
const std::string & msg() const
Return the message string provided to the ctor.
Definition: Exception.h:193
std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition: Exception.cc:38
CodeLocation _where
Definition: Exception.h:291
bool historyEmpty() const
Whether the history list is empty.
Definition: Exception.h:247
static std::string strErrno(int errno_r)
Make a string from errno_r.
Definition: Exception.cc:151
const CodeLocation & where() const
Return CodeLocation.
Definition: Exception.h:181
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition: Exception.cc:125
virtual std::ostream & dumpOn(std::ostream &str) const
Overload this to print a proper error message.
Definition: Exception.cc:141
void relocate(const CodeLocation &where_r) const
Exchange location on rethrow.
Definition: Exception.h:185
virtual const char * what() const
Return message string.
Definition: Exception.h:296
std::string _msg
Definition: Exception.h:292
HistoryIterator historyBegin() const
Iterator pointing to the most recent message.
Definition: Exception.h:239
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:163
void moveToHistory(TContainer &&msgc_r)
addHistory from string container types (oldest first) moving
Definition: Exception.h:232
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:147
std::list< std::string > History
Definition: Exception.h:149
friend std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition: Exception.cc:38
std::ostream & dumpError(std::ostream &str) const
Called by std::ostream & operator<<.
Definition: Exception.cc:144
std::string asString() const
Error message provided by dumpOn as string.
Definition: Exception.cc:75
virtual ~Exception()
Dtor.
Definition: Exception.cc:72
History _history
Definition: Exception.h:293
void do_ZYPP_RETHROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW macros.
Definition: Exception.h:335
SolvableIdType size_type
Definition: PoolMember.h:152
std::string asString() const
Location as string.
Definition: Exception.cc:30
Exception()
Default ctor.
Definition: Exception.cc:45
CodeLocation(const std::string &file_r, const std::string &func_r, unsigned line_r)
Ctor.
Definition: Exception.h:42
HistoryIterator historyEnd() const
Iterator pointing behind the last message.
Definition: Exception.h:243
friend std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:147
Base class for Exception.
Definition: Exception.h:143
void do_ZYPP_THROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW macros.
Definition: Exception.h:317
History::size_type HistorySize
Definition: Exception.h:151
exception_detail::CodeLocation CodeLocation
Definition: Exception.h:148
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition: Exception.cc:91
Keep FILE, FUNCTION and LINE.
Definition: Exception.h:32
HistorySize historySize() const
The size of the history list.
Definition: Exception.h:251
History::const_iterator HistoryIterator
Definition: Exception.h:150
std::string asUserString() const
Translated error message as string suitable for the user.
Definition: Exception.cc:82