libzypp: zypp-core/base/Exception.h Source File

libzypp 17.38.14

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#include <utility>
22
23#include <zypp-core/base/Errno.h>
24
26namespace zypp
27{
29 namespace exception_detail
30 {
31
35 struct CodeLocation
36 {
37 friend std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
38
40 CodeLocation()
41 : _line( 0 )
42 {}
43
44 ~CodeLocation() = default;
45 CodeLocation(const CodeLocation &) = default;
46 CodeLocation(CodeLocation &&) = default;
47 CodeLocation &operator=(const CodeLocation &) = default;
48 CodeLocation &operator=(CodeLocation &&) = default;
49
51 CodeLocation( std::string file_r,
52 std::string func_r,
53 unsigned line_r )
54 : _file(std::move( file_r )), _func(std::move( func_r )), _line( line_r )
55 {}
56
58 std::string asString() const;
59
60 private:
61 std::string _file;
62 std::string _func;
63 unsigned _line;
64 };
65
66
68 //#define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(__FILE__,__FUNCTION__,__LINE__)
69#define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(( *__FILE__ == '/' ? strrchr( __FILE__, '/' ) + 1 : __FILE__ ),__FUNCTION__,__LINE__)
70
72 std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
73
75 } // namespace exception_detail
77
79 //
80 // CLASS NAME : Exception
152 class ZYPP_API Exception : public std::exception
153 {
154 friend std::ostream & operator<<( std::ostream & str, const Exception & obj );
155
156 public:
157 using CodeLocation = exception_detail::CodeLocation;
158 using History = std::list<std::string>;
159 using HistoryIterator = History::const_iterator;
160 using HistorySize = History::size_type;
161
165 Exception();
166
170 Exception( const std::string & msg_r );
172 Exception( std::string && msg_r );
173
178 Exception( const std::string & msg_r, const Exception & history_r );
180 Exception( std::string && msg_r, const Exception & history_r );
182 Exception( const std::string & msg_r, Exception && history_r );
184 Exception( std::string && msg_r, Exception && history_r );
185
187 ~Exception() throw() override;
188
190 const CodeLocation & where() const
191 { return _where; }
192
194 void relocate( const CodeLocation & where_r ) const
195 { _where = where_r; }
196
198 void relocate( CodeLocation &&where_r ) const
199 { _where = std::move(where_r); }
200
206 const std::string & msg() const
207 { return _msg; }
208
210 std::string asString() const;
211
215 std::string asUserString() const;
216
217 public:
225
227 void remember( const Exception & old_r );
229 void remember( Exception && old_r );
230
232 void remember( std::exception_ptr old_r );
233
238 void remember( const std::string & msg_r )
239 { addHistory( msg_r ); }
240
241 void remember( std::string && msg_r )
242 { addHistory( std::move(msg_r) ); }
243
245 void addHistory( const std::string & msg_r );
247 void addHistory( std::string && msg_r );
248
250 template<class TContainer>
251 void addToHistory( const TContainer & msgc_r )
252 {
253 for ( const std::string & el : msgc_r )
254 addHistory( el );
255 }
256
257 template<class TContainer>
258 void moveToHistory( TContainer && msgc_r )
259 {
260 for ( std::string & el : msgc_r )
261 addHistory( std::move(el) );
262 }
263
265 HistoryIterator historyBegin() const
266 { return _history.begin(); }
267
269 HistoryIterator historyEnd() const
270 { return _history.end(); }
271
273 bool historyEmpty() const
274 { return _history.empty(); }
275
277 HistorySize historySize() const
278 { return _history.size(); }
279
290 std::string historyAsString() const;
291
293 std::string asUserHistory() const;
295
296 protected:
297
299 virtual std::ostream & dumpOn( std::ostream & str ) const;
300
301 public:
303 static std::string strErrno( int errno_r );
305 static std::string strErrno( int errno_r, std::string msg_r );
306
307 public:
311 static void log( const Exception & excpt_r, const CodeLocation & where_r,
312 const char *const prefix_r );
314 static void log( const char * typename_r, const CodeLocation & where_r,
315 const char *const prefix_r );
316 private:
317 mutable CodeLocation _where;
318 std::string _msg;
319 History _history;
320
322 const char * what() const throw() override
323 { return _msg.c_str(); }
324
329 std::ostream & dumpError( std::ostream & str ) const;
330 };
331
332
334 std::ostream & operator<<( std::ostream & str, const Exception & obj ) ZYPP_API;
335
337 std::ostream & operator<<( std::ostream & str, const std::exception_ptr &excptPtr ) ZYPP_API;
338
340 namespace exception_detail
341 {
343 template<class TExcpt>
344 using EnableIfIsException = std::enable_if_t< std::is_base_of_v<Exception,TExcpt>, int>;
345
347 template<class TExcpt>
348 using EnableIfNotException = std::enable_if_t< !std::is_base_of_v<Exception,TExcpt>, int>;
349
350
352 template<class TExcpt, EnableIfIsException<TExcpt> = 0>
353 void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
354 template<class TExcpt, EnableIfIsException<TExcpt>>
355 void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
356 {
357 excpt_r.relocate( where_r );
358 Exception::log( excpt_r, where_r, "THROW: " );
359 throw( excpt_r );
360 }
361
362 template<class TExcpt, EnableIfIsException<TExcpt> = 0>
363 void do_ZYPP_THROW( const TExcpt & excpt_r, CodeLocation && where_r ) __attribute__((noreturn));
364 template<class TExcpt, EnableIfIsException<TExcpt>>
365 void do_ZYPP_THROW( const TExcpt & excpt_r, CodeLocation && where_r )
366 {
367 Exception::log( excpt_r, where_r, "THROW: " );
368 excpt_r.relocate(std::move(where_r) );
369 throw( excpt_r );
370 }
371
373 template<class TExcpt, EnableIfNotException<TExcpt> = 0>
374 void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
375 template<class TExcpt, EnableIfNotException<TExcpt>>
376 void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
377 {
378 Exception::log( typeid(excpt_r).name(), where_r, "THROW: " );
379 throw( excpt_r );
380 }
381
383 template<class TExcpt, EnableIfIsException<TExcpt> = 0>
384 void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
385 {
386 Exception::log( excpt_r, where_r, "CAUGHT: " );
387 }
388
390 template<class TExcpt, EnableIfNotException<TExcpt> = 0>
391 void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
392 {
393 Exception::log( typeid(excpt_r).name(), where_r, "CAUGHT: " );
394 }
395
397 void do_ZYPP_CAUGHT ( const std::exception_ptr & excpt_r, CodeLocation &&where_r );
398
399
401 template<class TExcpt, EnableIfIsException<TExcpt> = 0>
402 void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
403 template<class TExcpt, EnableIfIsException<TExcpt>>
404 void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
405 {
406 Exception::log( excpt_r, where_r, "RETHROW: " );
407 excpt_r.relocate( where_r );
408 throw;
409 }
410
411 template<class TExcpt, EnableIfIsException<TExcpt> = 0>
412 void do_ZYPP_RETHROW( const TExcpt & excpt_r, CodeLocation && where_r ) __attribute__((noreturn));
413 template<class TExcpt, EnableIfIsException<TExcpt>>
414 void do_ZYPP_RETHROW( const TExcpt & excpt_r, CodeLocation && where_r )
415 {
416 Exception::log( excpt_r, where_r, "RETHROW: " );
417 excpt_r.relocate( std::move(where_r) );
418 throw;
419 }
420
422 template<class TExcpt, EnableIfNotException<TExcpt> = 0>
423 void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
424 template<class TExcpt, EnableIfNotException<TExcpt>>
425 void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
426 {
427 Exception::log( excpt_r, where_r, "RETHROW: " );
428 throw;
429 }
430
431 void do_ZYPP_RETHROW( const std::exception_ptr & excpt_r, const CodeLocation & where_r );
432
434 template<class TExcpt>
435 std::exception_ptr do_ZYPP_EXCPT_PTR( TExcpt && excpt_r, CodeLocation && where_r )
436 {
437 if constexpr( std::is_base_of_v<Exception, std::decay_t<TExcpt>> ) {
438 Exception::log( excpt_r, where_r, "THROW (EXCPTR): " );
439 excpt_r.relocate( std::move(where_r) );
440 } else {
441 Exception::log( typeid(excpt_r).name(), where_r, "THROW (EXCPTR): " );
442 }
443 return std::make_exception_ptr<std::decay_t<TExcpt>>( std::forward<TExcpt>(excpt_r) );
444 }
445
447 std::exception_ptr do_ZYPP_FWD_EXCPT_PTR( const std::exception_ptr & excpt_r, CodeLocation &&where_r );
448
449
450 } // namespace exception_detail
452
458
459#define ZYPP_THROW(EXCPT)\
460 ::zypp::exception_detail::do_ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION )
461
463#define ZYPP_EXCPT_PTR(EXCPT)\
464 ::zypp::exception_detail::do_ZYPP_EXCPT_PTR( EXCPT, ZYPP_EX_CODELOCATION )
465
467#define ZYPP_FWD_EXCPT(EXCPT)\
468 ::zypp::exception_detail::do_ZYPP_FWD_EXCPT_PTR( EXCPT, ZYPP_EX_CODELOCATION )
469
471#define ZYPP_FWD_CURRENT_EXCPT()\
472 ::zypp::exception_detail::do_ZYPP_FWD_EXCPT_PTR( std::current_exception(), ZYPP_EX_CODELOCATION )
473
475#define ZYPP_CAUGHT(EXCPT)\
476 ::zypp::exception_detail::do_ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION )
477
479#define ZYPP_RETHROW(EXCPT)\
480 ::zypp::exception_detail::do_ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION )
481
482
484#define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
485 ZYPP_THROW( EXCPTTYPE( MSG ) )
486
488#define ZYPP_THROW_ERRNO(EXCPTTYPE)\
489 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
490
492#define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\
493 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) )
494
496#define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\
497 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) )
498
500#define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\
501 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) )
502
503
505} // namespace zypp
507#endif // ZYPP_BASE_EXCEPTION_H
Errno.h
ZYPP_API
#define ZYPP_API
Definition Globals.h:69
operator<<
std::ostream & operator<<(std::ostream &str, const zypp::sat::detail::CDataiterator *obj)
relates: zypp::sat::LookupAttr::iterator Stream output of the underlying iterator for debug.
Definition LookupAttr.cc:808
zypp::Exception
Base class for Exception.
Definition Exception.h:153
zypp::Exception::relocate
void relocate(CodeLocation &&where_r) const
Exchange location on rethrow.
Definition Exception.h:198
zypp::Exception::_msg
std::string _msg
Definition Exception.h:318
zypp::Exception::_where
CodeLocation _where
Definition Exception.h:317
zypp::Exception::_history
History _history
Definition Exception.h:319
zypp::Exception::addHistory
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition Exception.cc:189
zypp::Exception::HistoryIterator
History::const_iterator HistoryIterator
Definition Exception.h:159
zypp::Exception::HistorySize
History::size_type HistorySize
Definition Exception.h:160
zypp::Exception::operator<<
friend std::ostream & operator<<(std::ostream &str, const Exception &obj)
relates: Exception Stream output
Definition Exception.cc:216
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:242
zypp::Exception::historyBegin
HistoryIterator historyBegin() const
Iterator pointing to the most recent message.
Definition Exception.h:265
zypp::Exception::what
const char * what() const override
Return message string.
Definition Exception.h:322
zypp::Exception::Exception
Exception()
Default ctor.
Definition Exception.cc:94
zypp::Exception::msg
const std::string & msg() const
Return the message string provided to the ctor.
Definition Exception.h:206
zypp::Exception::remember
void remember(std::string &&msg_r)
Definition Exception.h:241
zypp::Exception::CodeLocation
exception_detail::CodeLocation CodeLocation
Definition Exception.h:157
zypp::Exception::moveToHistory
void moveToHistory(TContainer &&msgc_r)
addHistory from string container types (oldest first) moving
Definition Exception.h:258
zypp::Exception::historyEnd
HistoryIterator historyEnd() const
Iterator pointing behind the last message.
Definition Exception.h:269
zypp::Exception::addToHistory
void addToHistory(const TContainer &msgc_r)
addHistory from string container types (oldest first)
Definition Exception.h:251
zypp::Exception::History
std::list< std::string > History
Definition Exception.h:158
zypp::Exception::relocate
void relocate(const CodeLocation &where_r) const
Exchange location on rethrow.
Definition Exception.h:194
zypp::Exception::historySize
HistorySize historySize() const
The size of the history list.
Definition Exception.h:277
zypp::Exception::where
const CodeLocation & where() const
Return CodeLocation.
Definition Exception.h:190
zypp::Exception::remember
void remember(const std::string &msg_r)
Remembering a plain string is most probably not wanted - we addHistory.
Definition Exception.h:238
zypp::Exception::historyEmpty
bool historyEmpty() const
Whether the history list is empty.
Definition Exception.h:273
std
Definition ansi.h:855
std::enable_if_t
typename enable_if< B, T >::type enable_if_t
Definition TypeTraits.h:45
std::is_base_of_v
constexpr bool is_base_of_v
Definition TypeTraits.h:27
str
String related utilities and Regular expression matching.
zypp::exception_detail
Definition Exception.cc:28
zypp::exception_detail::do_ZYPP_RETHROW
void do_ZYPP_RETHROW(const std::exception_ptr &excpt_r, const CodeLocation &where_r)
Definition Exception.cc:41
zypp::exception_detail::do_ZYPP_FWD_EXCPT_PTR
std::exception_ptr do_ZYPP_FWD_EXCPT_PTR(const std::exception_ptr &excpt_r, CodeLocation &&where_r)
Helper for ZYPP_FWD_CURRENT_EXCPT().
Definition Exception.cc:60
zypp::exception_detail::do_ZYPP_CAUGHT
void do_ZYPP_CAUGHT(const std::exception_ptr &excpt_r, CodeLocation &&where_r)
Helper for std::exception_ptr.
Definition Exception.cc:77
zypp::exception_detail::do_ZYPP_EXCPT_PTR
std::exception_ptr do_ZYPP_EXCPT_PTR(TExcpt &&excpt_r, CodeLocation &&where_r)
Helper for ZYPP_EXCPT_PTR( Exception ).
Definition Exception.h:435
zypp::exception_detail::EnableIfNotException
std::enable_if_t< !std::is_base_of_v< Exception, TExcpt >, int > EnableIfNotException
SFINAE: Hide template signature if TExcpt is derived from Exception.
Definition Exception.h:348
zypp::exception_detail::EnableIfIsException
std::enable_if_t< std::is_base_of_v< Exception, TExcpt >, int > EnableIfIsException
SFINAE: Hide template signature unless TExcpt is derived from Exception.
Definition Exception.h:344
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:355
zypp::log
Definition LogControl.cc:368
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::asUserString
std::string asUserString(VendorSupportOption opt)
converts the support option to a name intended to be printed to the user.
Definition VendorSupportOptions.cc:9
zypp::ZYPP_API
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition Arch.h:173
zypp::dumpOn
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
relates: Capability Detailed stream output
Definition Capability.cc:589
zypp::asString
std::string asString(const Patch::Category &obj)
relates: Patch::Category string representation.
Definition Patch.cc:122
zypp::exception_detail::CodeLocation
Keep FILE, FUNCTION and LINE.
Definition Exception.h:36
zypp::exception_detail::CodeLocation::operator=
CodeLocation & operator=(const CodeLocation &)=default
zypp::exception_detail::CodeLocation::~CodeLocation
~CodeLocation()=default
zypp::exception_detail::CodeLocation::operator<<
friend std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
relates: CodeLocation Stream output
Definition Exception.cc:38
zypp::exception_detail::CodeLocation::CodeLocation
CodeLocation()
Ctor.
Definition Exception.h:40
zypp::exception_detail::CodeLocation::_file
std::string _file
Definition Exception.h:61
zypp::exception_detail::CodeLocation::CodeLocation
CodeLocation(std::string file_r, std::string func_r, unsigned line_r)
Ctor.
Definition Exception.h:51
zypp::exception_detail::CodeLocation::_line
unsigned _line
Definition Exception.h:63
zypp::exception_detail::CodeLocation::CodeLocation
CodeLocation(const CodeLocation &)=default
zypp::exception_detail::CodeLocation::_func
std::string _func
Definition Exception.h:62
zypp::exception_detail::CodeLocation::operator=
CodeLocation & operator=(CodeLocation &&)=default
zypp::exception_detail::CodeLocation::CodeLocation
CodeLocation(CodeLocation &&)=default