libzypp  13.10.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 
284  template<class _Excpt>
285  void _ZYPP_THROW( const _Excpt & excpt_r, const exception_detail::CodeLocation & where_r ) __attribute__((noreturn));
286  template<class _Excpt>
287  void _ZYPP_THROW( const _Excpt & excpt_r, const exception_detail::CodeLocation & where_r )
288  {
289  excpt_r.relocate( where_r );
290  Exception::log( excpt_r, where_r, "THROW: " );
291  throw( excpt_r );
292  }
293 
295  template<class _Excpt>
296  void _ZYPP_CAUGHT( const _Excpt & excpt_r, const exception_detail::CodeLocation & where_r )
297  {
298  Exception::log( excpt_r, where_r, "CAUGHT: " );
299  }
300 
302  template<class _Excpt>
303  void _ZYPP_RETHROW( const _Excpt & excpt_r, const exception_detail::CodeLocation & where_r ) __attribute__((noreturn));
304  template<class _Excpt>
305  void _ZYPP_RETHROW( const _Excpt & excpt_r, const exception_detail::CodeLocation & where_r )
306  {
307  Exception::log( excpt_r, where_r, "RETHROW: " );
308  excpt_r.relocate( where_r );
309  throw;
310  }
311 
313 
320 #define ZYPP_THROW(EXCPT)\
321  _ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION )
322 
324 #define ZYPP_CAUGHT(EXCPT)\
325  _ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION )
326 
328 #define ZYPP_RETHROW(EXCPT)\
329  _ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION )
330 
331 
333 #define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
334  ZYPP_THROW( EXCPTTYPE( MSG ) )
335 
337 #define ZYPP_THROW_ERRNO(EXCPTTYPE)\
338  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
339 
341 #define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\
342  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) )
343 
345 #define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\
346  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) )
347 
349 #define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\
350  ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) )
351 
352 
354 } // namespace zypp
356 #endif // ZYPP_BASE_EXCEPTION_H
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
void _ZYPP_THROW(const _Excpt &excpt_r, const exception_detail::CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW macros.
Definition: Exception.h:287
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
void _ZYPP_CAUGHT(const _Excpt &excpt_r, const exception_detail::CodeLocation &where_r)
Helper for ZYPP_THROW macros.
Definition: Exception.h:296
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
void _ZYPP_RETHROW(const _Excpt &excpt_r, const exception_detail::CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW macros.
Definition: Exception.h:305
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 &amp; operator&lt;&lt;.
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
SolvableIdType size_type
Definition: PoolMember.h:99
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
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