libzypp: zypp/base/Exception.h Source File

libzypp 17.25.10

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
22 #include <zypp/base/Errno.h>
23
25 namespace zypp
26 {
28 namespace exception_detail
29 {
30
34 struct CodeLocation
35 {
36 friend std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
37
39 CodeLocation()
40 : _line( 0 )
41 {}
42
44 CodeLocation( const std::string & file_r,
45 const std::string & func_r,
46 unsigned line_r )
47 : _file( file_r ), _func( func_r ), _line( line_r )
48 {}
49
51 std::string asString() const;
52
53 private:
54 std::string _file;
55 std::string _func;
56 unsigned _line;
57 };
59
61 //#define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(__FILE__,__FUNCTION__,__LINE__)
62 #define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(( *__FILE__ == '/' ? strrchr( __FILE__, '/' ) + 1 : __FILE__ ),__FUNCTION__,__LINE__)
63
65 std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
66
68 } // namespace exception_detail
70
72 //
73 // CLASS NAME : Exception
145 class Exception : public std::exception
146 {
147 friend std::ostream & operator<<( std::ostream & str, const Exception & obj );
148
149 public:
150 typedef exception_detail::CodeLocation CodeLocation;
151 typedef std::list<std::string> History;
152 typedef History::const_iterator HistoryIterator;
153 typedef History::size_type HistorySize;
154
158 Exception();
159
163 Exception( const std::string & msg_r );
165 Exception( std::string && msg_r );
166
171 Exception( const std::string & msg_r, const Exception & history_r );
173 Exception( std::string && msg_r, const Exception & history_r );
175 Exception( const std::string & msg_r, Exception && history_r );
177 Exception( std::string && msg_r, Exception && history_r );
178
180 virtual ~Exception() throw();
181
183 const CodeLocation & where() const
184 { return _where; }
185
187 void relocate( const CodeLocation & where_r ) const
188 { _where = where_r; }
189
195 const std::string & msg() const
196 { return _msg; }
197
199 std::string asString() const;
200
204 std::string asUserString() const;
205
206 public:
214
216 void remember( const Exception & old_r );
218 void remember( Exception && old_r );
219
221 void addHistory( const std::string & msg_r );
223 void addHistory( std::string && msg_r );
224
226 template<class TContainer>
227 void addToHistory( const TContainer & msgc_r )
228 {
229 for ( const std::string & el : msgc_r )
230 addHistory( el );
231 }
233 template<class TContainer>
234 void moveToHistory( TContainer && msgc_r )
235 {
236 for ( std::string & el : msgc_r )
237 addHistory( std::move(el) );
238 }
239
241 HistoryIterator historyBegin() const
242 { return _history.begin(); }
243
245 HistoryIterator historyEnd() const
246 { return _history.end(); }
247
249 bool historyEmpty() const
250 { return _history.empty(); }
251
253 HistorySize historySize() const
254 { return _history.size(); }
255
266 std::string historyAsString() const;
267
269 std::string asUserHistory() const;
271
272 protected:
273
275 virtual std::ostream & dumpOn( std::ostream & str ) const;
276
277 public:
279 static std::string strErrno( int errno_r );
281 static std::string strErrno( int errno_r, const std::string & msg_r );
283 static std::string strErrno( int errno_r, std::string && msg_r );
284
285 public:
289 static void log( const Exception & excpt_r, const CodeLocation & where_r,
290 const char *const prefix_r );
292 static void log( const char * typename_r, const CodeLocation & where_r,
293 const char *const prefix_r );
294 private:
295 mutable CodeLocation _where;
296 std::string _msg;
297 History _history;
298
300 virtual const char * what() const throw()
301 { return _msg.c_str(); }
302
307 std::ostream & dumpError( std::ostream & str ) const;
308 };
310
312 std::ostream & operator<<( std::ostream & str, const Exception & obj );
313
315 namespace exception_detail
316 {
318 template<class TExcpt>
319 using EnableIfIsException = typename std::enable_if< std::is_base_of<Exception,TExcpt>::value, int>::type;
320
322 template<class TExcpt>
323 using EnableIfNotException = typename std::enable_if< !std::is_base_of<Exception,TExcpt>::value, int>::type;
324
325
327 template<class TExcpt, EnableIfIsException<TExcpt> = 0>
328 void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
329 template<class TExcpt, EnableIfIsException<TExcpt>>
330 void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
331 {
332 excpt_r.relocate( where_r );
333 Exception::log( excpt_r, where_r, "THROW: " );
334 throw( excpt_r );
335 }
336
338 template<class TExcpt, EnableIfNotException<TExcpt> = 0>
339 void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
340 template<class TExcpt, EnableIfNotException<TExcpt>>
341 void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
342 {
343 Exception::log( typeid(excpt_r).name(), where_r, "THROW: " );
344 throw( excpt_r );
345 }
346
347
349 template<class TExcpt, EnableIfIsException<TExcpt> = 0>
350 void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
351 {
352 Exception::log( excpt_r, where_r, "CAUGHT: " );
353 }
354
356 template<class TExcpt, EnableIfNotException<TExcpt> = 0>
357 void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
358 {
359 Exception::log( typeid(excpt_r).name(), where_r, "CAUGHT: " );
360 }
361
362
364 template<class TExcpt, EnableIfIsException<TExcpt> = 0>
365 void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
366 template<class TExcpt, EnableIfIsException<TExcpt>>
367 void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
368 {
369 Exception::log( excpt_r, where_r, "RETHROW: " );
370 excpt_r.relocate( where_r );
371 throw;
372 }
373
375 template<class TExcpt, EnableIfNotException<TExcpt> = 0>
376 void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
377 template<class TExcpt, EnableIfNotException<TExcpt>>
378 void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
379 {
380 Exception::log( excpt_r, where_r, "RETHROW: " );
381 throw;
382 }
383 } // namespace exception_detail
385
392 #define ZYPP_THROW(EXCPT)\
393 ::zypp::exception_detail::do_ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION )
394
396 #define ZYPP_CAUGHT(EXCPT)\
397 ::zypp::exception_detail::do_ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION )
398
400 #define ZYPP_RETHROW(EXCPT)\
401 ::zypp::exception_detail::do_ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION )
402
403
405 #define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
406 ZYPP_THROW( EXCPTTYPE( MSG ) )
407
409 #define ZYPP_THROW_ERRNO(EXCPTTYPE)\
410 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
411
413 #define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\
414 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) )
415
417 #define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\
418 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) )
419
421 #define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\
422 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) )
424
426 } // namespace zypp
428 #endif // ZYPP_BASE_EXCEPTION_H
Errno.h
zypp::Exception
Base class for Exception.
Definition: Exception.h:146
zypp::Exception::_msg
std::string _msg
Definition: Exception.h:296
zypp::Exception::strErrno
static std::string strErrno(int errno_r)
Make a string from errno_r.
Definition: Exception.cc:151
zypp::Exception::~Exception
virtual ~Exception()
Dtor.
Definition: Exception.cc:72
zypp::Exception::asUserHistory
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition: Exception.cc:91
zypp::Exception::dumpError
std::ostream & dumpError(std::ostream &str) const
Called by std::ostream & operator<<.
Definition: Exception.cc:144
zypp::Exception::where
const CodeLocation & where() const
Return CodeLocation.
Definition: Exception.h:183
zypp::Exception::asUserString
std::string asUserString() const
Translated error message as string suitable for the user.
Definition: Exception.cc:82
zypp::Exception::HistorySize
History::size_type HistorySize
Definition: Exception.h:153
zypp::Exception::_where
CodeLocation _where
Definition: Exception.h:295
zypp::Exception::_history
History _history
Definition: Exception.h:297
zypp::Exception::addHistory
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition: Exception.cc:125
zypp::Exception::historyAsString
std::string historyAsString() const
The history as string.
Definition: Exception.cc:131
zypp::Exception::dumpOn
virtual std::ostream & dumpOn(std::ostream &str) const
Overload this to print a proper error message.
Definition: Exception.cc:141
zypp::Exception::asString
std::string asString() const
Error message provided by dumpOn as string.
Definition: Exception.cc:75
zypp::Exception::log
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
zypp::Exception::historyBegin
HistoryIterator historyBegin() const
Iterator pointing to the most recent message.
Definition: Exception.h:241
zypp::Exception::History
std::list< std::string > History
Definition: Exception.h:151
zypp::Exception::msg
const std::string & msg() const
Return the message string provided to the ctor.
Definition: Exception.h:195
zypp::Exception::Exception
Exception()
Default ctor.
Definition: Exception.cc:45
zypp::Exception::operator<<
friend std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:147
zypp::Exception::moveToHistory
void moveToHistory(TContainer &&msgc_r)
addHistory from string container types (oldest first) moving
Definition: Exception.h:234
zypp::Exception::historyEnd
HistoryIterator historyEnd() const
Iterator pointing behind the last message.
Definition: Exception.h:245
zypp::Exception::HistoryIterator
History::const_iterator HistoryIterator
Definition: Exception.h:152
zypp::Exception::addToHistory
void addToHistory(const TContainer &msgc_r)
addHistory from string container types (oldest first)
Definition: Exception.h:227
zypp::Exception::what
virtual const char * what() const
Return message string.
Definition: Exception.h:300
zypp::Exception::relocate
void relocate(const CodeLocation &where_r) const
Exchange location on rethrow.
Definition: Exception.h:187
zypp::Exception::historySize
HistorySize historySize() const
The size of the history list.
Definition: Exception.h:253
zypp::Exception::historyEmpty
bool historyEmpty() const
Whether the history list is empty.
Definition: Exception.h:249
zypp::Exception::CodeLocation
exception_detail::CodeLocation CodeLocation
Definition: Exception.h:150
zypp::Exception::remember
void remember(const Exception &old_r)
Store an other Exception as history.
Definition: Exception.cc:105
str
String related utilities and Regular expression matching.
zypp::exception_detail::do_ZYPP_RETHROW
void do_ZYPP_RETHROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW( Exception ).
Definition: Exception.h:367
zypp::exception_detail::EnableIfNotException
typename std::enable_if< !std::is_base_of< Exception, TExcpt >::value, int >::type EnableIfNotException
SFINAE: Hide template signature if TExcpt is derived from Exception.
Definition: Exception.h:323
zypp::exception_detail::do_ZYPP_CAUGHT
void do_ZYPP_CAUGHT(const TExcpt &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_THROW( Exception ).
Definition: Exception.h:350
zypp::exception_detail::operator<<
std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition: Exception.cc:38
zypp::exception_detail::EnableIfIsException
typename std::enable_if< std::is_base_of< Exception, TExcpt >::value, int >::type EnableIfIsException
SFINAE: Hide template signature unless TExcpt is derived from Exception.
Definition: Exception.h:319
zypp::exception_detail::do_ZYPP_THROW
void do_ZYPP_THROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW( Exception ).
Definition: Exception.h:330
zypp::sat::detail::size_type
SolvableIdType size_type
Definition: PoolMember.h:126
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:147
zypp::exception_detail::CodeLocation
Keep FILE, FUNCTION and LINE.
Definition: Exception.h:35
zypp::exception_detail::CodeLocation::CodeLocation
CodeLocation(const std::string &file_r, const std::string &func_r, unsigned line_r)
Ctor.
Definition: Exception.h:44
zypp::exception_detail::CodeLocation::CodeLocation
CodeLocation()
Ctor.
Definition: Exception.h:39
zypp::exception_detail::CodeLocation::_file
std::string _file
Definition: Exception.h:54
zypp::exception_detail::CodeLocation::asString
std::string asString() const
Location as string.
Definition: Exception.cc:30
zypp::exception_detail::CodeLocation::_line
unsigned _line
Definition: Exception.h:56
zypp::exception_detail::CodeLocation::operator<<
friend std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition: Exception.cc:38
zypp::exception_detail::CodeLocation::_func
std::string _func
Definition: Exception.h:55