12 #ifndef ZYPP_BASE_EXCEPTION_H 13 #define ZYPP_BASE_EXCEPTION_H 20 #include <type_traits> 22 #include <zypp-core/base/Errno.h> 27 namespace exception_detail
45 const std::string & func_r,
62 #define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(( *__FILE__ == '/' ? strrchr( __FILE__, '/' ) + 1 : __FILE__ ),__FUNCTION__,__LINE__) 65 std::ostream &
operator<<( std::ostream &
str,
const CodeLocation & obj );
195 const std::string &
msg()
const 221 void remember( std::exception_ptr old_r );
239 template<
class TContainer>
242 for (
const std::string & el : msgc_r )
246 template<
class TContainer>
249 for ( std::string & el : msgc_r )
288 virtual std::ostream &
dumpOn( std::ostream &
str )
const;
292 static std::string
strErrno(
int errno_r );
294 static std::string
strErrno(
int errno_r,
const std::string & msg_r );
296 static std::string
strErrno(
int errno_r, std::string && msg_r );
303 const char *
const prefix_r );
305 static void log(
const char * typename_r,
const CodeLocation & where_r,
306 const char *
const prefix_r );
313 virtual const char *
what()
const throw()
314 {
return _msg.c_str(); }
325 std::ostream &
operator<<( std::ostream &
str,
const Exception & obj );
328 namespace exception_detail
331 template<
class TExcpt>
332 using EnableIfIsException =
typename std::enable_if< std::is_base_of<Exception,TExcpt>::value,
int>::type;
335 template<
class TExcpt>
340 template<
class TExcpt, EnableIfIsException<TExcpt> = 0>
342 template<
class TExcpt, EnableIfIsException<TExcpt>>
345 excpt_r.relocate( where_r );
351 template<
class TExcpt, EnableIfNotException<TExcpt> = 0>
353 template<
class TExcpt, EnableIfNotException<TExcpt>>
354 void do_ZYPP_THROW(
const TExcpt & excpt_r,
const CodeLocation & where_r )
362 template<
class TExcpt, EnableIfIsException<TExcpt> = 0>
369 template<
class TExcpt, EnableIfNotException<TExcpt> = 0>
370 void do_ZYPP_CAUGHT(
const TExcpt & excpt_r,
const CodeLocation & where_r )
377 template<
class TExcpt, EnableIfIsException<TExcpt> = 0>
379 template<
class TExcpt, EnableIfIsException<TExcpt>>
383 excpt_r.relocate( where_r );
388 template<
class TExcpt, EnableIfNotException<TExcpt> = 0>
390 template<
class TExcpt, EnableIfNotException<TExcpt>>
391 void do_ZYPP_RETHROW(
const TExcpt & excpt_r,
const CodeLocation & where_r )
398 template<
class TExcpt, EnableIfIsException<TExcpt> = 0>
399 std::exception_ptr
do_ZYPP_EXCPT_PTR(
const TExcpt & excpt_r,
const CodeLocation & where_r );
400 template<
class TExcpt, EnableIfIsException<TExcpt>>
403 excpt_r.relocate( where_r );
405 return std::make_exception_ptr( excpt_r );
409 template<
class TExcpt, EnableIfNotException<TExcpt> = 0>
410 std::exception_ptr
do_ZYPP_EXCPT_PTR(
const TExcpt & excpt_r,
const CodeLocation & where_r );
411 template<
class TExcpt, EnableIfNotException<TExcpt>>
412 std::exception_ptr
do_ZYPP_EXCPT_PTR(
const TExcpt & excpt_r,
const CodeLocation & where_r )
415 return std::make_exception_ptr( excpt_r );
428 #define ZYPP_THROW(EXCPT)\ 429 ::zypp::exception_detail::do_ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION ) 432 #define ZYPP_EXCPT_PTR(EXCPT)\ 433 ::zypp::exception_detail::do_ZYPP_EXCPT_PTR( EXCPT, ZYPP_EX_CODELOCATION ) 436 #define ZYPP_CAUGHT(EXCPT)\ 437 ::zypp::exception_detail::do_ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION ) 440 #define ZYPP_RETHROW(EXCPT)\ 441 ::zypp::exception_detail::do_ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION ) 445 #define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\ 446 ZYPP_THROW( EXCPTTYPE( MSG ) ) 449 #define ZYPP_THROW_ERRNO(EXCPTTYPE)\ 450 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) ) 453 #define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\ 454 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) ) 457 #define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\ 458 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) ) 461 #define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\ 462 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) ) 468 #endif // ZYPP_BASE_EXCEPTION_H HistoryIterator historyEnd() const
Iterator pointing behind the last message.
void addToHistory(const TContainer &msgc_r)
addHistory from string container types (oldest first)
void do_ZYPP_CAUGHT(const TExcpt &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_THROW( Exception ).
typename std::enable_if< !std::is_base_of< Exception, TExcpt >::value, int >::type EnableIfNotException
SFINAE: Hide template signature if TExcpt is derived from Exception.
std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
virtual std::ostream & dumpOn(std::ostream &str) const
Overload this to print a proper error message.
static std::string strErrno(int errno_r)
Make a string from errno_r.
void addHistory(const std::string &msg_r)
Add some message text to the history.
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
void do_ZYPP_RETHROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW( Exception ).
virtual const char * what() const
Return message string.
HistorySize historySize() const
The size of the history list.
void remember(const Exception &old_r)
Store an other Exception as history.
static void log(const Exception &excpt_r, const CodeLocation &where_r, const char *const prefix_r)
Drop a logline on throw, catch or rethrow.
void moveToHistory(TContainer &&msgc_r)
addHistory from string container types (oldest first) moving
HistoryIterator historyBegin() const
Iterator pointing to the most recent message.
std::list< std::string > History
std::string asString() const
Error message provided by dumpOn as string.
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
std::string asUserString() const
Translated error message as string suitable for the user.
friend std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
const CodeLocation & where() const
Return CodeLocation.
typename std::enable_if< std::is_base_of< Exception, TExcpt >::value, int >::type EnableIfIsException
SFINAE: Hide template signature unless TExcpt is derived from Exception.
bool historyEmpty() const
Whether the history list is empty.
std::string historyAsString() const
The history as string.
virtual ~Exception()
Dtor.
void remember(const std::string &msg_r)
Remembering a plain string is most probably not wanted - we addHistory.
CodeLocation(const std::string &file_r, const std::string &func_r, unsigned line_r)
Ctor.
std::exception_ptr do_ZYPP_EXCPT_PTR(const TExcpt &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_EXCPT_PTR( Exception ).
friend std::ostream & operator<<(std::ostream &str, const Exception &obj)
void remember(std::string &&msg_r)
Base class for Exception.
History::size_type HistorySize
exception_detail::CodeLocation CodeLocation
void do_ZYPP_THROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW( Exception ).
std::ostream & dumpError(std::ostream &str) const
Called by std::ostream & operator<<.
Keep FILE, FUNCTION and LINE.
void relocate(const CodeLocation &where_r) const
Exchange location on rethrow.
Easy-to use interface to the ZYPP dependency resolver.
History::const_iterator HistoryIterator
std::string asString() const
Location as string.
const std::string & msg() const
Return the message string provided to the ctor.