libzypp  11.13.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 
26  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