libzypp 17.36.7

Exception.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <sstream>
14
15#include <zypp-core/base/Logger.h>
16#include <zypp-core/base/LogTools.h>
17#include <zypp-core/base/Gettext.h>
18#include <zypp-core/base/StringV.h>
19#include <zypp-core/base/Exception.h>
20
21 using std::endl;
22
24 namespace zypp
25{
27 namespace exception_detail
28 {
29
30 std::string CodeLocation::asString() const
31 {
32 return str::form( "%s(%s):%u",
33 _file.c_str(),
34 _func.c_str(),
35 _line );
36 }
37
38 std::ostream & operator<<( std::ostream & str, const CodeLocation & obj )
39 { return str << obj.asString(); }
40
41 void do_ZYPP_RETHROW(const std::exception_ptr &excpt_r, const CodeLocation &where_r)
42 {
43 if ( !excpt_r )
44 return;
45
46 try {
47 std::rethrow_exception (excpt_r);
48 } catch ( const zypp::Exception &e ) {
49 Exception::log( e, where_r, "RETHROW: " );
50 throw;
51 } catch ( const std::exception & e ) {
52 Exception::log( typeid(e).name(), where_r, "RETHROW: " );
53 throw;
54 } catch (...) {
55 Exception::log( "Unknown Exception", where_r, "RETHROW: " );
56 throw;
57 }
58 }
59
60 std::exception_ptr do_ZYPP_FWD_EXCPT_PTR( const std::exception_ptr & excpt_r, const CodeLocation & where_r )
61 {
62 try {
63 std::rethrow_exception( excpt_r );
64 } catch ( zypp::Exception &e ) {
65 e.relocate( where_r );
66 Exception::log( e, where_r, "RETHROW (FWD) EXCPTR: " );
67 return std::current_exception();
68 } catch ( const std::exception & e ) {
69 Exception::log( typeid(e).name(), where_r, "RETHROW (FWD) EXCPTR: " );
70 return std::current_exception();
71 } catch (...) {
72 Exception::log( "Unknown Exception", where_r, "RETHROW (FWD) EXCPTR: " );
73 return std::current_exception();
74 }
75 }
76
78 } // namespace exception_detail
79
80
81 Exception::Exception()
82 {}
83
84 Exception::Exception( const std::string & msg_r )
85 : _msg( msg_r )
86 {}
87
88 Exception::Exception( std::string && msg_r )
89 : _msg( std::move(msg_r) )
90 {}
91
92 Exception::Exception( const std::string & msg_r, const Exception & history_r )
93 : _msg( msg_r )
94 { remember( history_r ); }
95
96 Exception::Exception( std::string && msg_r, const Exception & history_r )
97 : _msg( std::move(msg_r) )
98 { remember( history_r ); }
99
100 Exception::Exception( const std::string & msg_r, Exception && history_r )
101 : _msg( msg_r )
102 { remember( std::move(history_r) ); }
103
104 Exception::Exception( std::string && msg_r, Exception && history_r )
105 : _msg( std::move(msg_r) )
106 { remember( std::move(history_r) ); }
107
108 Exception::~Exception() throw()
109 {}
110
111 std::string Exception::asString() const
112 {
113 std::ostringstream str;
114 dumpOn( str );
115 return str.str();
116 }
117
118 std::string Exception::asUserString() const
119 {
120 std::ostringstream str;
121 dumpOn( str );
122 // call gettext to translate the message. This will
123 // not work if dumpOn() uses composed messages.
124 return _(str.str().c_str());
125 }
126
127 std::string Exception::asUserHistory() const
128 {
129 if ( historyEmpty() )
130 return asUserString();
131
132 std::string ret( asUserString() );
133 if ( ret.empty() )
134 return historyAsString();
135
136 ret += '\n';
137 ret += historyAsString();
138 return ret;
139 }
140
141 void Exception::remember( const Exception & old_r )
142 {
143 if ( &old_r != this ) // no self-remember
144 {
145 History newh( old_r._history.begin(), old_r._history.end() );
146 newh.push_front( old_r.asUserString() );
147 _history.swap( newh );
148 }
149 }
150
151 void Exception::remember( Exception && old_r )
152 {
153 if ( &old_r != this ) // no self-remember
154 {
155 History & newh( old_r._history ); // stealing it
156 newh.push_front( old_r.asUserString() );
157 _history.swap( newh );
158 }
159 }
160
161 void Exception::remember( std::exception_ptr old_r )
162 {
163 try {
164 if (old_r) {
165 std::rethrow_exception(std::move(old_r));
166 }
167 } catch( const Exception& e ) {
168 remember( e );
169 } catch ( const std::exception& e ) {
170 addHistory( e.what() );
171 } catch ( ... ) {
172 addHistory( "Remembered unknown exception" );
173 }
174 }
175
176 void Exception::addHistory( const std::string & msg_r )
177 { _history.push_front( msg_r ); }
178
179 void Exception::addHistory( std::string && msg_r )
180 { _history.push_front( std::move(msg_r) ); }
181
182 std::string Exception::historyAsString() const
183 {
184 std::ostringstream ret;
185 if ( not _history.empty() ) {
186 ret << _("History:");
187 for ( const std::string & entry : _history ) {
188 strv::split( entry, "\n", [&ret]( std::string_view line_r, unsigned idx, bool last_r ) -> void {
189 if ( not ( last_r && line_r.empty() ) )
190 ret << endl << (idx==0?" - ":" ") << line_r;
191 });
192 }
193 }
194 return ret.str();
195 }
196
197 std::ostream & Exception::dumpOn( std::ostream & str ) const
198 { return str << _msg; }
199
200 std::ostream & Exception::dumpError( std::ostream & str ) const
201 { return dumpOn( str << _where << ": " ); }
202
203 std::ostream & operator<<( std::ostream & str, const Exception & obj )
204 { return obj.dumpError( str ); }
205
206
207 std::string Exception::strErrno( int errno_r )
208 { return str::strerror( errno_r ); }
209
210 std::string Exception::strErrno( int errno_r, std::string msg_r )
211 {
212 msg_r += ": ";
213 return msg_r += strErrno( errno_r );
214 }
215
216 void Exception::log( const Exception & excpt_r, const CodeLocation & where_r,
217 const char *const prefix_r )
218 {
219 INT << where_r << " " << prefix_r << " " << excpt_r.asUserHistory() << endl;
220 }
221
222 void Exception::log( const char * typename_r, const CodeLocation & where_r,
223 const char *const prefix_r )
224 {
225 INT << where_r << " " << prefix_r << " exception of type " << typename_r << endl;
226 }
227
228} // namespace zypp
zypp::Exception
Base class for Exception.
Definition Exception.h:147
zypp::Exception::_msg
std::string _msg
Definition Exception.h:308
zypp::Exception::strErrno
static std::string strErrno(int errno_r)
Make a string from errno_r.
Definition Exception.cc:207
zypp::Exception::asUserHistory
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition Exception.cc:127
zypp::Exception::dumpError
std::ostream & dumpError(std::ostream &str) const
Called by std::ostream & operator<<.
Definition Exception.cc:200
zypp::Exception::asUserString
std::string asUserString() const
Translated error message as string suitable for the user.
Definition Exception.cc:118
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::historyAsString
std::string historyAsString() const
The history as string.
Definition Exception.cc:182
zypp::Exception::dumpOn
virtual std::ostream & dumpOn(std::ostream &str) const
Overload this to print a proper error message.
Definition Exception.cc:197
zypp::Exception::asString
std::string asString() const
Error message provided by dumpOn as string.
Definition Exception.cc:111
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::what
const char * what() const override
Return message string.
Definition Exception.h:312
zypp::Exception::~Exception
~Exception() override
Dtor.
Definition Exception.cc:108
zypp::Exception::Exception
Exception()
Default ctor.
Definition Exception.cc:81
zypp::Exception::CodeLocation
exception_detail::CodeLocation CodeLocation
Definition Exception.h:151
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::historyEmpty
bool historyEmpty() const
Whether the history list is empty.
Definition Exception.h:263
zypp::Exception::remember
void remember(const Exception &old_r)
Store an other Exception as history.
Definition Exception.cc:141
std
Definition Arch.h:364
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, const CodeLocation &where_r)
Helper for ZYPP_FWD_CURRENT_EXCPT().
Definition Exception.cc:60
zypp::exception_detail::operator<<
std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition Exception.cc:38
zypp::str::strerror
std::string strerror(int errno_r)
Return string describing the error_r code.
Definition String.cc:54
zypp::str::form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:37
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition SerialNumber.cc:52
zypp::exception_detail::CodeLocation
Keep FILE, FUNCTION and LINE.
Definition Exception.h:36
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::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
Exception.h
Gettext.h
_
#define _(MSG)
Definition Gettext.h:39
LogTools.h
Logger.h
INT
#define INT
Definition Logger.h:104
StringV.h