libzypp 17.36.7

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
26 namespace 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
45 CodeLocation( std::string file_r,
46 std::string func_r,
47 unsigned line_r )
48 : _file(std::move( file_r )), _func(std::move( func_r )), _line( line_r )
49 {}
50
52 std::string asString() const;
53
54 private:
55 std::string _file;
56 std::string _func;
57 unsigned _line;
58 };
59
60
62 //#define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(__FILE__,__FUNCTION__,__LINE__)
63#define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(( *__FILE__ == '/' ? strrchr( __FILE__, '/' ) + 1 : __FILE__ ),__FUNCTION__,__LINE__)
64
66 std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
67
69 } // namespace exception_detail
71
73 //
74 // CLASS NAME : Exception
146 class ZYPP_API Exception : public std::exception
147 {
148 friend std::ostream & operator<<( std::ostream & str, const Exception & obj );
149
150 public:
151 using CodeLocation = exception_detail::CodeLocation;
152 using History = std::list<std::string>;
153 using HistoryIterator = History::const_iterator;
154 using HistorySize = History::size_type;
155
159 Exception();
160
164 Exception( const std::string & msg_r );
166 Exception( std::string && msg_r );
167
172 Exception( const std::string & msg_r, const Exception & history_r );
174 Exception( std::string && msg_r, const Exception & history_r );
176 Exception( const std::string & msg_r, Exception && history_r );
178 Exception( std::string && msg_r, Exception && history_r );
179
181 ~Exception() throw() override;
182
184 const CodeLocation & where() const
185 { return _where; }
186
188 void relocate( const CodeLocation & where_r ) const
189 { _where = where_r; }
190
196 const std::string & msg() const
197 { return _msg; }
198
200 std::string asString() const;
201
205 std::string asUserString() const;
206
207 public:
215
217 void remember( const Exception & old_r );
219 void remember( Exception && old_r );
220
222 void remember( std::exception_ptr old_r );
223
228 void remember( const std::string & msg_r )
229 { addHistory( msg_r ); }
230
231 void remember( std::string && msg_r )
232 { addHistory( std::move(msg_r) ); }
233
235 void addHistory( const std::string & msg_r );
237 void addHistory( std::string && msg_r );
238
240 template<class TContainer>
241 void addToHistory( const TContainer & msgc_r )
242 {
243 for ( const std::string & el : msgc_r )
244 addHistory( el );
245 }
246
247 template<class TContainer>
248 void moveToHistory( TContainer && msgc_r )
249 {
250 for ( std::string & el : msgc_r )
251 addHistory( std::move(el) );
252 }
253
255 HistoryIterator historyBegin() const
256 { return _history.begin(); }
257
259 HistoryIterator historyEnd() const
260 { return _history.end(); }
261
263 bool historyEmpty() const
264 { return _history.empty(); }
265
267 HistorySize historySize() const
268 { return _history.size(); }
269
280 std::string historyAsString() const;
281
283 std::string asUserHistory() const;
285
286 protected:
287
289 virtual std::ostream & dumpOn( std::ostream & str ) const;
290
291 public:
293 static std::string strErrno( int errno_r );
295 static std::string strErrno( int errno_r, std::string msg_r );
296
297 public:
301 static void log( const Exception & excpt_r, const CodeLocation & where_r,
302 const char *const prefix_r );
304 static void log( const char * typename_r, const CodeLocation & where_r,
305 const char *const prefix_r );
306 private:
307 mutable CodeLocation _where;
308 std::string _msg;
309 History _history;
310
312 const char * what() const throw() override
313 { return _msg.c_str(); }
314
319 std::ostream & dumpError( std::ostream & str ) const;
320 };
321
322
324 std::ostream & operator<<( std::ostream & str, const Exception & obj ) ZYPP_API;
325
327 namespace exception_detail
328 {
330 template<class TExcpt>
331 using EnableIfIsException = std::enable_if_t< std::is_base_of_v<Exception,TExcpt>, int>;
332
334 template<class TExcpt>
335 using EnableIfNotException = std::enable_if_t< !std::is_base_of_v<Exception,TExcpt>, int>;
336
337
339 template<class TExcpt, EnableIfIsException<TExcpt> = 0>
340 void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
341 template<class TExcpt, EnableIfIsException<TExcpt>>
342 void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
343 {
344 excpt_r.relocate( where_r );
345 Exception::log( excpt_r, where_r, "THROW: " );
346 throw( excpt_r );
347 }
348
350 template<class TExcpt, EnableIfNotException<TExcpt> = 0>
351 void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
352 template<class TExcpt, EnableIfNotException<TExcpt>>
353 void do_ZYPP_THROW( const TExcpt & excpt_r, const CodeLocation & where_r )
354 {
355 Exception::log( typeid(excpt_r).name(), where_r, "THROW: " );
356 throw( excpt_r );
357 }
358
359
361 template<class TExcpt, EnableIfIsException<TExcpt> = 0>
362 void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
363 {
364 Exception::log( excpt_r, where_r, "CAUGHT: " );
365 }
366
368 template<class TExcpt, EnableIfNotException<TExcpt> = 0>
369 void do_ZYPP_CAUGHT( const TExcpt & excpt_r, const CodeLocation & where_r )
370 {
371 Exception::log( typeid(excpt_r).name(), where_r, "CAUGHT: " );
372 }
373
374
376 template<class TExcpt, EnableIfIsException<TExcpt> = 0>
377 void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
378 template<class TExcpt, EnableIfIsException<TExcpt>>
379 void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
380 {
381 Exception::log( excpt_r, where_r, "RETHROW: " );
382 excpt_r.relocate( where_r );
383 throw;
384 }
385
387 template<class TExcpt, EnableIfNotException<TExcpt> = 0>
388 void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r ) __attribute__((noreturn));
389 template<class TExcpt, EnableIfNotException<TExcpt>>
390 void do_ZYPP_RETHROW( const TExcpt & excpt_r, const CodeLocation & where_r )
391 {
392 Exception::log( excpt_r, where_r, "RETHROW: " );
393 throw;
394 }
395
396 void do_ZYPP_RETHROW( const std::exception_ptr & excpt_r, const CodeLocation & where_r );
397
399 template<class TExcpt>
400 std::exception_ptr do_ZYPP_EXCPT_PTR( TExcpt && excpt_r, const CodeLocation & where_r )
401 {
402 if constexpr( std::is_base_of_v<Exception, std::decay_t<TExcpt>> ) {
403 excpt_r.relocate( where_r );
404 Exception::log( excpt_r, where_r, "THROW (EXCPTR): " );
405 } else {
406 Exception::log( typeid(excpt_r).name(), where_r, "THROW (EXCPTR): " );
407 }
408 return std::make_exception_ptr<std::decay_t<TExcpt>>( std::forward<TExcpt>(excpt_r) );
409 }
410
412 std::exception_ptr do_ZYPP_FWD_EXCPT_PTR( const std::exception_ptr & excpt_r, const CodeLocation & where_r );
413
414
415 } // namespace exception_detail
417
423
424#define ZYPP_THROW(EXCPT)\
425 ::zypp::exception_detail::do_ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION )
426
428#define ZYPP_EXCPT_PTR(EXCPT)\
429 ::zypp::exception_detail::do_ZYPP_EXCPT_PTR( EXCPT, ZYPP_EX_CODELOCATION )
430
432#define ZYPP_FWD_EXCPT(EXCPT)\
433 ::zypp::exception_detail::do_ZYPP_FWD_EXCPT_PTR( EXCPT, ZYPP_EX_CODELOCATION )
434
436#define ZYPP_FWD_CURRENT_EXCPT()\
437 ::zypp::exception_detail::do_ZYPP_FWD_EXCPT_PTR( std::current_exception(), ZYPP_EX_CODELOCATION )
438
440#define ZYPP_CAUGHT(EXCPT)\
441 ::zypp::exception_detail::do_ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION )
442
444#define ZYPP_RETHROW(EXCPT)\
445 ::zypp::exception_detail::do_ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION )
446
447
449#define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
450 ZYPP_THROW( EXCPTTYPE( MSG ) )
451
453#define ZYPP_THROW_ERRNO(EXCPTTYPE)\
454 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
455
457#define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\
458 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) )
459
461#define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\
462 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) )
463
465#define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\
466 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) )
467
468
470} // namespace zypp
472#endif // ZYPP_BASE_EXCEPTION_H
zypp::Exception
Base class for Exception.
Definition Exception.h:147
zypp::Exception::_msg
std::string _msg
Definition Exception.h:308
zypp::Exception::_where
CodeLocation _where
Definition Exception.h:307
zypp::Exception::_history
History _history
Definition Exception.h:309
zypp::Exception::addHistory
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition Exception.cc:176
zypp::Exception::HistoryIterator
History::const_iterator HistoryIterator
Definition Exception.h:153
zypp::Exception::HistorySize
History::size_type HistorySize
Definition Exception.h:154
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:216
zypp::Exception::historyBegin
HistoryIterator historyBegin() const
Iterator pointing to the most recent message.
Definition Exception.h:255
zypp::Exception::what
const char * what() const override
Return message string.
Definition Exception.h:312
zypp::Exception::Exception
Exception()
Default ctor.
Definition Exception.cc:81
zypp::Exception::msg
const std::string & msg() const
Return the message string provided to the ctor.
Definition Exception.h:196
zypp::Exception::remember
void remember(std::string &&msg_r)
Definition Exception.h:231
zypp::Exception::CodeLocation
exception_detail::CodeLocation CodeLocation
Definition Exception.h:151
zypp::Exception::moveToHistory
void moveToHistory(TContainer &&msgc_r)
addHistory from string container types (oldest first) moving
Definition Exception.h:248
zypp::Exception::historyEnd
HistoryIterator historyEnd() const
Iterator pointing behind the last message.
Definition Exception.h:259
zypp::Exception::addToHistory
void addToHistory(const TContainer &msgc_r)
addHistory from string container types (oldest first)
Definition Exception.h:241
zypp::Exception::History
std::list< std::string > History
Definition Exception.h:152
zypp::Exception::relocate
void relocate(const CodeLocation &where_r) const
Exchange location on rethrow.
Definition Exception.h:188
zypp::Exception::historySize
HistorySize historySize() const
The size of the history list.
Definition Exception.h:267
zypp::Exception::where
const CodeLocation & where() const
Return CodeLocation.
Definition Exception.h:184
zypp::Exception::remember
void remember(const std::string &msg_r)
Remembering a plain string is most probably not wanted - we addHistory.
Definition Exception.h:228
zypp::Exception::historyEmpty
bool historyEmpty() const
Whether the history list is empty.
Definition Exception.h:263
std
Definition Arch.h:364
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_CAUGHT
void do_ZYPP_CAUGHT(const TExcpt &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_THROW( Exception ).
Definition Exception.h:362
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, const CodeLocation &where_r)
Helper for ZYPP_FWD_CURRENT_EXCPT().
Definition Exception.cc:60
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:335
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:331
zypp::exception_detail::operator<<
std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition Exception.cc:38
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:342
zypp::exception_detail::do_ZYPP_EXCPT_PTR
std::exception_ptr do_ZYPP_EXCPT_PTR(TExcpt &&excpt_r, const CodeLocation &where_r)
Helper for ZYPP_EXCPT_PTR( Exception ).
Definition Exception.h:400
zypp::log
Definition LogControl.cc:362
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::dumpOn
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
Definition Capability.cc:580
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition SerialNumber.cc:52
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_armv7hnl Arch_armv7nhl ZYPP_API
Definition Arch.h:247
zypp::asString
std::string asString(const Patch::Category &obj)
Definition Patch.cc:122
zypp::exception_detail::CodeLocation
Keep FILE, FUNCTION and LINE.
Definition Exception.h:36
zypp::exception_detail::CodeLocation::operator<<
friend std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
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:55
zypp::exception_detail::CodeLocation::CodeLocation
CodeLocation(std::string file_r, std::string func_r, unsigned line_r)
Ctor.
Definition Exception.h:45
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:57
zypp::exception_detail::CodeLocation::_func
std::string _func
Definition Exception.h:56
ZYPP_API
#define ZYPP_API
Definition Globals.h:69
Errno.h