12#ifndef ZYPP_BASE_EXCEPTION_H
13#define ZYPP_BASE_EXCEPTION_H
22#include <zypp-core/base/Errno.h>
28 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 );
145 class Exception :
public std::exception
151 typedef std::list<std::string>
History;
195 const std::string &
msg()
const
221 void remember( std::exception_ptr old_r );
229 template<
class TContainer>
232 for (
const std::string & el : msgc_r )
236 template<
class TContainer>
239 for ( std::string & el : msgc_r )
278 virtual std::ostream &
dumpOn( std::ostream &
str )
const;
282 static std::string
strErrno(
int errno_r );
284 static std::string
strErrno(
int errno_r,
const std::string & msg_r );
286 static std::string
strErrno(
int errno_r, std::string && msg_r );
293 const char *
const prefix_r );
295 static void log(
const char * typename_r,
const CodeLocation & where_r,
296 const char *
const prefix_r );
303 virtual const char *
what()
const throw()
304 {
return _msg.c_str(); }
315 std::ostream &
operator<<( std::ostream &
str,
const Exception & obj );
318 namespace exception_detail
321 template<
class TExcpt>
322 using EnableIfIsException =
typename std::enable_if< std::is_base_of<Exception,TExcpt>::value,
int>::type;
325 template<
class TExcpt>
326 using EnableIfNotException =
typename std::enable_if< !std::is_base_of<Exception,TExcpt>::value,
int>::type;
330 template<
class TExcpt, EnableIfIsException<TExcpt> = 0>
331 void do_ZYPP_THROW(
const TExcpt & excpt_r,
const CodeLocation & where_r ) __attribute__((noreturn));
332 template<
class TExcpt, EnableIfIsException<TExcpt>>
333 void do_ZYPP_THROW(
const TExcpt & excpt_r,
const CodeLocation & where_r )
335 excpt_r.relocate( where_r );
341 template<
class TExcpt, EnableIfNotException<TExcpt> = 0>
342 void do_ZYPP_THROW(
const TExcpt & excpt_r,
const CodeLocation & where_r ) __attribute__((noreturn));
343 template<
class TExcpt, EnableIfNotException<TExcpt>>
344 void do_ZYPP_THROW(
const TExcpt & excpt_r,
const CodeLocation & where_r )
352 template<
class TExcpt, EnableIfIsException<TExcpt> = 0>
353 void do_ZYPP_CAUGHT(
const TExcpt & excpt_r,
const CodeLocation & where_r )
359 template<
class TExcpt, EnableIfNotException<TExcpt> = 0>
360 void do_ZYPP_CAUGHT(
const TExcpt & excpt_r,
const CodeLocation & where_r )
367 template<
class TExcpt, EnableIfIsException<TExcpt> = 0>
368 void do_ZYPP_RETHROW(
const TExcpt & excpt_r,
const CodeLocation & where_r ) __attribute__((noreturn));
369 template<
class TExcpt, EnableIfIsException<TExcpt>>
370 void do_ZYPP_RETHROW(
const TExcpt & excpt_r,
const CodeLocation & where_r )
373 excpt_r.relocate( where_r );
378 template<
class TExcpt, EnableIfNotException<TExcpt> = 0>
379 void do_ZYPP_RETHROW(
const TExcpt & excpt_r,
const CodeLocation & where_r ) __attribute__((noreturn));
380 template<
class TExcpt, EnableIfNotException<TExcpt>>
381 void do_ZYPP_RETHROW(
const TExcpt & excpt_r,
const CodeLocation & where_r )
388 template<
class TExcpt, EnableIfIsException<TExcpt> = 0>
389 std::exception_ptr
do_ZYPP_EXCPT_PTR(
const TExcpt & excpt_r,
const CodeLocation & where_r );
390 template<
class TExcpt, EnableIfIsException<TExcpt>>
391 std::exception_ptr
do_ZYPP_EXCPT_PTR(
const TExcpt & excpt_r,
const CodeLocation & where_r )
393 excpt_r.relocate( where_r );
395 return std::make_exception_ptr( excpt_r );
399 template<
class TExcpt, EnableIfNotException<TExcpt> = 0>
400 std::exception_ptr
do_ZYPP_EXCPT_PTR(
const TExcpt & excpt_r,
const CodeLocation & where_r );
401 template<
class TExcpt, EnableIfNotException<TExcpt>>
402 std::exception_ptr
do_ZYPP_EXCPT_PTR(
const TExcpt & excpt_r,
const CodeLocation & where_r )
405 return std::make_exception_ptr( excpt_r );
418#define ZYPP_THROW(EXCPT)\
419 ::zypp::exception_detail::do_ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION )
422#define ZYPP_EXCPT_PTR(EXCPT)\
423 ::zypp::exception_detail::do_ZYPP_EXCPT_PTR( EXCPT, ZYPP_EX_CODELOCATION )
426#define ZYPP_CAUGHT(EXCPT)\
427 ::zypp::exception_detail::do_ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION )
430#define ZYPP_RETHROW(EXCPT)\
431 ::zypp::exception_detail::do_ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION )
435#define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
436 ZYPP_THROW( EXCPTTYPE( MSG ) )
439#define ZYPP_THROW_ERRNO(EXCPTTYPE)\
440 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
443#define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\
444 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) )
447#define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\
448 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) )
451#define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\
452 ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) )
static std::string strErrno(int errno_r)
Make a string from errno_r.
virtual ~Exception()
Dtor.
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.
History::size_type HistorySize
void addHistory(const std::string &msg_r)
Add some message text to the history.
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.
std::list< std::string > History
const std::string & msg() const
Return the message string provided to the ctor.
void moveToHistory(TContainer &&msgc_r)
addHistory from string container types (oldest first) moving
HistoryIterator historyEnd() const
Iterator pointing behind the last message.
History::const_iterator HistoryIterator
void addToHistory(const TContainer &msgc_r)
addHistory from string container types (oldest first)
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.
virtual const char * what() const
Return message string.
bool historyEmpty() const
Whether the history list is empty.
exception_detail::CodeLocation CodeLocation
void remember(const Exception &old_r)
Store an other Exception as history.
String related utilities and Regular expression matching.
void do_ZYPP_RETHROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
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.
void do_ZYPP_CAUGHT(const TExcpt &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_THROW( Exception ).
std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
typename std::enable_if< std::is_base_of< Exception, TExcpt >::value, int >::type EnableIfIsException
SFINAE: Hide template signature unless TExcpt is derived from Exception.
void do_ZYPP_THROW(const TExcpt &excpt_r, const CodeLocation &where_r) __attribute__((noreturn))
Helper for ZYPP_THROW( Exception ).
std::exception_ptr do_ZYPP_EXCPT_PTR(const TExcpt &excpt_r, const CodeLocation &where_r)
Helper for ZYPP_EXCPT_PTR( Exception ).
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & operator<<(std::ostream &str, const Exception &obj)
friend std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
std::string asString() const
Location as string.