libzypp  15.28.6
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 );
162 
167  Exception( const std::string & msg_r, const Exception & history_r );
168 
170  virtual ~Exception() throw();
171 
173  const CodeLocation & where() const
174  { return _where; }
175 
177  void relocate( const CodeLocation & where_r ) const
178  { _where = where_r; }
179 
185  const std::string & msg() const
186  { return _msg; }
187 
189  std::string asString() const;
190 
194  std::string asUserString() const;
195 
196  public:
204 
206  void remember( const Exception & old_r );
207 
209  void addHistory( const std::string & msg_r );
210 
213  { return _history.begin(); }
214 
217  { return _history.end(); }
218 
220  bool historyEmpty() const
221  { return _history.empty(); }
222 
225  { return _history.size(); }
226 
237  std::string historyAsString() const;
238 
240  std::string asUserHistory() const;
242 
243  protected:
244 
246  virtual std::ostream & dumpOn( std::ostream & str ) const;
247 
248  public:
250  static std::string strErrno( int errno_r );
252  static std::string strErrno( int errno_r, const std::string & msg_r );
253 
254  public:
258  static void log( const Exception & excpt_r, const CodeLocation & where_r,
259  const char *const prefix_r );
260 
261  private:
263  std::string _msg;
265 
267  virtual const char * what() const throw()
268  { return _msg.c_str(); }
269 
274  std::ostream & dumpError( std::ostream & str ) const;
275  };
277 
279  std::ostream & operator<<( std::ostream & str, const Exception & obj );
280 
282  namespace exception_detail
283  {
285  template<class TExcpt>
286  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
287  template<class TExcpt>
288  void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
289  {
290  excpt_r.relocate( where_r );
291  Exception::log( excpt_r, where_r, "THROW: " );
292  throw( excpt_r );
293  }
294 
296  template<class TExcpt>
297  void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
298  {
299  Exception::log( excpt_r, where_r, "CAUGHT: " );
300  }
301 
303  template<class TExcpt>
304  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
305  template<class TExcpt>
306  void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
307  {
308  Exception::log( excpt_r, where_r, "RETHROW: " );
309  excpt_r.relocate( where_r );
310  throw;
311  }
312  } // namespace exception_detail
314 
321 #define ZYPP_THROW(EXCPT)\
322  ::zypp::exception_detail::do_ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION )
323 
325 #define ZYPP_CAUGHT(EXCPT)\
326  ::zypp::exception_detail::do_ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION )
327 
329 #define ZYPP_RETHROW(EXCPT)\
330  ::zypp::exception_detail::do_ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION )
331 
332 
334 #define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
335  ZYPP_THROW( EXCPTTYPE( MSG ) )
336 
338 #define ZYPP_THROW_ERRNO(EXCPTTYPE)\
339  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
340 
342 #define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\
343  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) )
344 
346 #define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\
347  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) )
348 
350 #define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\
351  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) )
352 
353 
355 } // namespace zypp
357 #endif // ZYPP_BASE_EXCEPTION_H
void do_ZYPP_CAUGHT(const TExcpt &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_THROW macros.
Definition: Exception.h:297
std::string historyAsString() const
The history as string.
Definition: Exception.cc:104
const std::string & msg() const
Return the message string provided to the ctor.
Definition: Exception.h:185
std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition: Exception.cc:38
CodeLocation _where
Definition: Exception.h:262
bool historyEmpty() const
Whether the history list is empty.
Definition: Exception.h:220
static std::string strErrno(int errno_r)
Make a string from errno_r.
Definition: Exception.cc:124
const CodeLocation & where() const
Return CodeLocation.
Definition: Exception.h:173
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition: Exception.cc:99
virtual std::ostream & dumpOn(std::ostream &str) const
Overload this to print a proper error message.
Definition: Exception.cc:114
void relocate(const CodeLocation &where_r) const
Exchange location on rethrow.
Definition: Exception.h:177
virtual const char * what() const
Return message string.
Definition: Exception.h:267
std::string _msg
Definition: Exception.h:263
HistoryIterator historyBegin() const
Iterator pointing to the most recent message.
Definition: Exception.h:212
void remember(const Exception &old_r)
Store an other Exception as history.
Definition: Exception.cc:89
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:134
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:120
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:117
std::string asString() const
Error message provided by dumpOn as string.
Definition: Exception.cc:59
virtual ~Exception()
Dtor.
Definition: Exception.cc:56
History _history
Definition: Exception.h:264
void do_ZYPP_RETHROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW macros.
Definition: Exception.h:306
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:216
friend std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:120
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:288
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:75
Keep FILE, FUNCTION and LINE.
Definition: Exception.h:32
HistorySize historySize() const
The size of the history list.
Definition: Exception.h:224
History::const_iterator HistoryIterator
Definition: Exception.h:150
std::string asUserString() const
Translated error message as string suitable for the user.
Definition: Exception.cc:66