libzypp  17.30.2
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 
42  } // namespace exception_detail
44 
46  {}
47 
48  Exception::Exception( const std::string & msg_r )
49  : _msg( msg_r )
50  {}
51 
52  Exception::Exception( std::string && msg_r )
53  : _msg( std::move(msg_r) )
54  {}
55 
56  Exception::Exception( const std::string & msg_r, const Exception & history_r )
57  : _msg( msg_r )
58  { remember( history_r ); }
59 
60  Exception::Exception( std::string && msg_r, const Exception & history_r )
61  : _msg( std::move(msg_r) )
62  { remember( history_r ); }
63 
64  Exception::Exception( const std::string & msg_r, Exception && history_r )
65  : _msg( msg_r )
66  { remember( std::move(history_r) ); }
67 
68  Exception::Exception( std::string && msg_r, Exception && history_r )
69  : _msg( std::move(msg_r) )
70  { remember( std::move(history_r) ); }
71 
73  {}
74 
75  std::string Exception::asString() const
76  {
77  std::ostringstream str;
78  dumpOn( str );
79  return str.str();
80  }
81 
82  std::string Exception::asUserString() const
83  {
84  std::ostringstream str;
85  dumpOn( str );
86  // call gettext to translate the message. This will
87  // not work if dumpOn() uses composed messages.
88  return _(str.str().c_str());
89  }
90 
91  std::string Exception::asUserHistory() const
92  {
93  if ( historyEmpty() )
94  return asUserString();
95 
96  std::string ret( asUserString() );
97  if ( ret.empty() )
98  return historyAsString();
99 
100  ret += '\n';
101  ret += historyAsString();
102  return ret;
103  }
104 
105  void Exception::remember( const Exception & old_r )
106  {
107  if ( &old_r != this ) // no self-remember
108  {
109  History newh( old_r._history.begin(), old_r._history.end() );
110  newh.push_front( old_r.asUserString() );
111  _history.swap( newh );
112  }
113  }
114 
116  {
117  if ( &old_r != this ) // no self-remember
118  {
119  History & newh( old_r._history ); // stealing it
120  newh.push_front( old_r.asUserString() );
121  _history.swap( newh );
122  }
123  }
124 
125  void Exception::remember( std::exception_ptr old_r )
126  {
127  try {
128  if (old_r) {
129  std::rethrow_exception(old_r);
130  }
131  } catch( const Exception& e ) {
132  remember( e );
133  } catch ( const std::exception& e ) {
134  addHistory( e.what() );
135  } catch ( ... ) {
136  addHistory( "Remembered unknown exception" );
137  }
138  }
139 
140  void Exception::addHistory( const std::string & msg_r )
141  { _history.push_front( msg_r ); }
142 
143  void Exception::addHistory( std::string && msg_r )
144  { _history.push_front( std::move(msg_r) ); }
145 
146  std::string Exception::historyAsString() const
147  {
148  std::ostringstream ret;
149  if ( not _history.empty() ) {
150  ret << _("History:") << endl;
151  for ( const std::string & entry : _history ) {
152  strv::split( entry, "\n", [&ret]( std::string_view line_r, unsigned idx, bool last_r ) -> void {
153  if ( not ( last_r && line_r.empty() ) )
154  ret << (idx==0?" - ":" ") << line_r << endl;
155  });
156  }
157  }
158  return ret.str();
159  }
160 
161  std::ostream & Exception::dumpOn( std::ostream & str ) const
162  { return str << _msg; }
163 
164  std::ostream & Exception::dumpError( std::ostream & str ) const
165  { return dumpOn( str << _where << ": " ); }
166 
167  std::ostream & operator<<( std::ostream & str, const Exception & obj )
168  { return obj.dumpError( str ); }
169 
170 
171  std::string Exception::strErrno( int errno_r )
172  { return str::strerror( errno_r ); }
173 
174  std::string Exception::strErrno( int errno_r, const std::string & msg_r )
175  { return strErrno( errno_r, std::string(msg_r) ); }
176 
177  std::string Exception::strErrno( int errno_r, std::string && msg_r )
178  {
179  msg_r += ": ";
180  return msg_r += strErrno( errno_r );
181  }
182 
183  void Exception::log( const Exception & excpt_r, const CodeLocation & where_r,
184  const char *const prefix_r )
185  {
186  INT << where_r << " " << prefix_r << " " << excpt_r.asUserHistory() << endl;
187  }
188 
189  void Exception::log( const char * typename_r, const CodeLocation & where_r,
190  const char *const prefix_r )
191  {
192  INT << where_r << " " << prefix_r << " exception of type " << typename_r << endl;
193  }
195 } // namespace zypp
Base class for Exception.
Definition: Exception.h:146
std::string _msg
Definition: Exception.h:309
static std::string strErrno(int errno_r)
Make a string from errno_r.
Definition: Exception.cc:171
virtual ~Exception()
Dtor.
Definition: Exception.cc:72
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition: Exception.cc:91
std::ostream & dumpError(std::ostream &str) const
Called by std::ostream & operator<<.
Definition: Exception.cc:164
std::string asUserString() const
Translated error message as string suitable for the user.
Definition: Exception.cc:82
CodeLocation _where
Definition: Exception.h:308
History _history
Definition: Exception.h:310
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition: Exception.cc:140
std::string historyAsString() const
The history as string.
Definition: Exception.cc:146
virtual std::ostream & dumpOn(std::ostream &str) const
Overload this to print a proper error message.
Definition: Exception.cc:161
std::string asString() const
Error message provided by dumpOn as string.
Definition: Exception.cc:75
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:183
std::list< std::string > History
Definition: Exception.h:151
Exception()
Default ctor.
Definition: Exception.cc:45
virtual const char * what() const
Return message string.
Definition: Exception.h:313
bool historyEmpty() const
Whether the history list is empty.
Definition: Exception.h:262
void remember(const Exception &old_r)
Store an other Exception as history.
Definition: Exception.cc:105
Definition: Arch.h:352
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition: Exception.cc:38
std::string strerror(int errno_r)
Return string describing the error_r code.
Definition: String.cc:53
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:36
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", const Trim trim_r=NO_TRIM)
Split line_r into words.
Definition: String.h:531
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
Keep FILE, FUNCTION and LINE.
Definition: Exception.h:35
std::string asString() const
Location as string.
Definition: Exception.cc:30
#define _(MSG)
Definition: Gettext.h:37
#define INT
Definition: Logger.h:100