12#ifndef ZYPP_BASE_EXCEPTION_H
13#define ZYPP_BASE_EXCEPTION_H
23#include <zypp-core/base/Errno.h>
29 namespace exception_detail
63#define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(( *__FILE__ == '/' ? strrchr( __FILE__, '/' ) + 1 : __FILE__ ),__FUNCTION__,__LINE__)
66 std::ostream &
operator<<( std::ostream &
str,
const CodeLocation & obj );
146 class Exception :
public std::exception
152 using History = std::list<std::string>;
196 const std::string &
msg()
const
222 void remember( std::exception_ptr old_r );
228 void remember(
const std::string & msg_r )
231 void remember( std::string && msg_r )
240 template<
class TContainer>
243 for (
const std::string & el : msgc_r )
247 template<
class TContainer>
250 for ( std::string & el : msgc_r )
289 virtual std::ostream &
dumpOn( std::ostream &
str )
const;
293 static std::string
strErrno(
int errno_r );
295 static std::string
strErrno(
int errno_r, std::string msg_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 );
312 const char *
what()
const throw()
override
313 {
return _msg.c_str(); }
324 std::ostream &
operator<<( std::ostream &
str,
const Exception & obj );
327 namespace exception_detail
330 template<
class TExcpt>
334 template<
class TExcpt>
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 )
344 excpt_r.relocate( where_r );
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 )
361 template<
class TExcpt, EnableIfIsException<TExcpt> = 0>
362 void do_ZYPP_CAUGHT(
const TExcpt & excpt_r,
const CodeLocation & where_r )
368 template<
class TExcpt, EnableIfNotException<TExcpt> = 0>
369 void do_ZYPP_CAUGHT(
const TExcpt & excpt_r,
const CodeLocation & where_r )
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 )
382 excpt_r.relocate( where_r );
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 )
396 void do_ZYPP_RETHROW(
const std::exception_ptr & excpt_r,
const CodeLocation & where_r );
399 template<
class TExcpt, EnableIfIsException<TExcpt> = 0>
400 std::exception_ptr
do_ZYPP_EXCPT_PTR( TExcpt && excpt_r,
const CodeLocation & where_r );
401 template<
class TExcpt, EnableIfIsException<TExcpt>>
402 std::exception_ptr
do_ZYPP_EXCPT_PTR( TExcpt && excpt_r,
const CodeLocation & where_r )
404 excpt_r.relocate( where_r );
406 return std::make_exception_ptr( std::forward<TExcpt>(excpt_r) );
410 template<
class TExcpt, EnableIfNotException<TExcpt> = 0>
411 std::exception_ptr
do_ZYPP_EXCPT_PTR( TExcpt && excpt_r,
const CodeLocation & where_r );
412 template<
class TExcpt, EnableIfNotException<TExcpt>>
413 std::exception_ptr
do_ZYPP_EXCPT_PTR( TExcpt && excpt_r,
const CodeLocation & where_r )
416 return std::make_exception_ptr( std::forward<TExcpt>(excpt_r) );
429#define ZYPP_THROW(EXCPT)\
430 ::zypp::exception_detail::do_ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION )
433#define ZYPP_EXCPT_PTR(EXCPT)\
434 ::zypp::exception_detail::do_ZYPP_EXCPT_PTR( EXCPT, ZYPP_EX_CODELOCATION )
437#define ZYPP_CAUGHT(EXCPT)\
438 ::zypp::exception_detail::do_ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION )
441#define ZYPP_RETHROW(EXCPT)\
442 ::zypp::exception_detail::do_ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION )
446#define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
447 ZYPP_THROW( EXCPTTYPE( MSG ) )
450#define ZYPP_THROW_ERRNO(EXCPTTYPE)\
451 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
454#define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\
455 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) )
458#define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\
459 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) )
462#define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\
463 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) )
static std::string strErrno(int errno_r)
Make a string from errno_r.
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
std::ostream & dumpError(std::ostream &str) const
Called by std::ostream & operator<<.
std::string asUserString() const
Translated error message as string suitable for the user.
void addHistory(const std::string &msg_r)
Add some message text to the history.
History::const_iterator HistoryIterator
History::size_type HistorySize
std::string historyAsString() const
The history as string.
virtual std::ostream & dumpOn(std::ostream &str) const
Overload this to print a proper error message.
std::string asString() const
Error message provided by dumpOn as string.
friend std::ostream & operator<<(std::ostream &str, const Exception &obj)
static void log(const Exception &excpt_r, const CodeLocation &where_r, const char *const prefix_r)
Drop a logline on throw, catch or rethrow.
HistoryIterator historyBegin() const
Iterator pointing to the most recent message.
const char * what() const override
Return message string.
~Exception() override
Dtor.
const std::string & msg() const
Return the message string provided to the ctor.
exception_detail::CodeLocation CodeLocation
void moveToHistory(TContainer &&msgc_r)
addHistory from string container types (oldest first) moving
HistoryIterator historyEnd() const
Iterator pointing behind the last message.
void addToHistory(const TContainer &msgc_r)
addHistory from string container types (oldest first)
std::list< std::string > History
void relocate(const CodeLocation &where_r) const
Exchange location on rethrow.
HistorySize historySize() const
The size of the history list.
const CodeLocation & where() const
Return CodeLocation.
bool historyEmpty() const
Whether the history list is empty.
void remember(const Exception &old_r)
Store an other Exception as history.
typename enable_if< B, T >::type enable_if_t
String related utilities and Regular expression matching.
void do_ZYPP_CAUGHT(const TExcpt &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_THROW( Exception ).
void do_ZYPP_RETHROW(const std::exception_ptr &excpt_r, const CodeLocation &where_r)
std::exception_ptr do_ZYPP_EXCPT_PTR(TExcpt &&excpt_r, const CodeLocation &where_r)
Helper for ZYPP_EXCPT_PTR( Exception ).
std::enable_if_t< !std::is_base_of_v< Exception, TExcpt >, int > EnableIfNotException
SFINAE: Hide template signature if TExcpt is derived from Exception.
std::enable_if_t< std::is_base_of_v< Exception, TExcpt >, int > EnableIfIsException
SFINAE: Hide template signature unless TExcpt is derived from Exception.
std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
void do_ZYPP_THROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW( Exception ).
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
friend std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
std::string asString() const
Location as string.