libstorage-ng
Exception.h
1 /*
2  * Copyright (c) [2014-2015] Novell, Inc.
3  * Copyright (c) [2016-2026] SUSE LLC
4  *
5  * All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License as published
9  * by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, contact Novell, Inc.
18  *
19  * To contact Novell about this file by physical or electronic mail, you may
20  * find current contact information at www.novell.com.
21  */
22 
23 
24 #ifndef STORAGE_EXCEPTION_H
25 #define STORAGE_EXCEPTION_H
26 
27 
28 /*
29  * Large parts stolen from libyui/YUIException.h
30  */
31 
32 
33 #include <stdexcept>
34 #include <string>
35 #include <ostream>
36 
37 #include "storage/Utils/Logger.h"
38 #include "storage/Utils/Swig.h"
39 
40 
41 namespace storage
42 {
43 
49  {
50  public:
51 
56  CodeLocation( const std::string & file_r,
57  const std::string & func_r,
58  int line_r )
59  : _file(file_r), _func(func_r), _line(line_r)
60  {}
61 
66  : _file(), _func(), _line(0)
67  {}
68 
72  const std::string& file() const { return _file; }
73 
77  const std::string& func() const { return _func; }
78 
82  int line() const { return _line; }
83 
87  std::string as_string() const;
88 
89  std::string asString() const ST_DEPRECATED;
90 
94  friend std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
95 
96  private:
97 
98  std::string _file;
99  std::string _func;
100  int _line;
101 
102  };
103 
104 
108  std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
109 
110 
118  class Exception : public std::exception
119  {
120  public:
121 
126  Exception(LogLevel log_level = LogLevel::ERROR);
127 
132  Exception(const std::string & msg, LogLevel log_level = LogLevel::ERROR);
133 
137  virtual ~Exception() noexcept;
138 
142  const CodeLocation & where() const
143  { return _where; }
144 
148  void relocate( const CodeLocation & newLocation ) const
149  { _where = newLocation; }
150 
156  const std::string& msg() const { return _msg; }
157 
161  LogLevel log_level() const { return _log_level; }
162 
166  void setMsg(const std::string& msg) { _msg = msg; }
167 
171  std::string as_string() const;
172 
173  std::string asString() const ST_DEPRECATED;
174 
178  static std::string strErrno( int errno_r );
179 
183  static std::string strErrno( int errno_r, const std::string & msg );
184 
189  static void log( const Exception & exception,
190  const CodeLocation & location,
191  const char * const prefix );
192 
198  virtual const char* what() const noexcept override { return _msg.c_str(); }
199 
200  protected:
201 
205  virtual std::ostream& dumpOn(std::ostream& str) const;
206 
207  private:
208 
209  friend std::ostream& operator<<(std::ostream& str, const Exception& obj);
210 
211  mutable CodeLocation _where;
212  std::string _msg;
213  LogLevel _log_level;
214 
219  std::ostream & dumpError( std::ostream & str ) const;
220 
221  };
222 
223 
227  std::ostream & operator<<( std::ostream & str, const Exception & obj );
228 
229 
235  {
236  public:
237 
239  : Exception("Null pointer", LogLevel::ERROR)
240  {}
241 
242  virtual ~NullPointerException() noexcept
243  {}
244 
245  };
246 
247 
253  {
254  public:
255 
256  UnsupportedException(const std::string& msg) : Exception(msg) {}
257  virtual ~UnsupportedException() noexcept {}
258 
259  };
260 
261 
266  class LogicException : public Exception
267  {
268  public:
269 
270  LogicException(const std::string& msg) : Exception(msg) {}
271  virtual ~LogicException() noexcept {}
272 
273  };
274 
275 
281  {
282  public:
283 
285  : Exception("Out of memory", LogLevel::ERROR)
286  {}
287 
288  virtual ~OutOfMemoryException() noexcept
289  {}
290 
291  };
292 
293 
298  {
299  public:
300 
309  IndexOutOfRangeException( int invalidIndex,
310  int validMin,
311  int validMax,
312  const std::string & msg = "" )
313  : Exception( msg )
314  , _invalidIndex( invalidIndex )
315  , _validMin( validMin )
316  , _validMax( validMax )
317  {}
318 
319  virtual ~IndexOutOfRangeException() noexcept
320  {}
321 
325  int invalid_index() const { return _invalidIndex; }
326 
327  int invalidIndex() const ST_DEPRECATED { return _invalidIndex; }
328 
332  int valid_min() const { return _validMin; }
333 
334  int validMin() const ST_DEPRECATED { return _validMin; }
335 
339  int valid_max() const { return _validMax; }
340 
341  int validMax() const ST_DEPRECATED { return _validMax; }
342 
343  protected:
344 
349  virtual std::ostream& dumpOn(std::ostream& str) const override
350  {
351  std::string prefix = msg();
352 
353  if ( prefix.empty() )
354  prefix = "Index out of range";
355 
356  return str << prefix << ": " << _invalidIndex
357  << " valid: " << _validMin << " .. " << _validMax
358  << std::endl;
359  }
360 
361  private:
362 
363  int _invalidIndex;
364  int _validMin;
365  int _validMax;
366 
367  };
368 
369 
374  {
375  public:
376 
377  OverflowException() : Exception("overflow") {}
378  virtual ~OverflowException() noexcept {}
379 
380  };
381 
382 
386  class IOException : public Exception
387  {
388  public:
389 
390  IOException(const std::string& msg) : Exception(msg) {}
391  virtual ~IOException() noexcept {}
392 
393  };
394 
395 
400  class ParseException : public Exception
401  {
402  public:
403 
411  ParseException( const std::string & msg,
412  const std::string & seen,
413  const std::string & expected ):
414  Exception( msg ),
415  _seen( seen ),
416  _expected( expected )
417  {}
418 
422  virtual ~ParseException() noexcept
423  {}
424 
428  const std::string & seen() const { return _seen; }
429 
433  const std::string & expected() const { return _expected; }
434 
439  virtual std::ostream& dumpOn(std::ostream& str) const override
440  {
441  std::string prefix = "Parse error";
442 
443  if ( ! msg().empty() )
444  prefix += ": ";
445 
446  return str << prefix << msg()
447  << "; expected: \"" << _expected
448  << "\" seen: \"" << _seen << "\""
449  << std::endl;
450  }
451 
452  private:
453 
454  std::string _seen;
455  std::string _expected;
456 
457  };
458 
459 
460  // TODO It should be possible to catch all exception derived from
461  // Exception except of Aborted, see code in activate_* and
462  // error_callback. Or use some other method to abort function with
463  // callbacks.
464 
465  class Aborted : public Exception
466  {
467  public:
468 
469  Aborted(const std::string& message) : Exception(message) {}
470  virtual ~Aborted() noexcept {}
471 
472  };
473 
474 }
475 
476 #endif
CodeLocation()
Default constructor.
Definition: Exception.h:65
virtual std::ostream & dumpOn(std::ostream &str) const override
Write proper error message with all relevant data.
Definition: Exception.h:439
Exception class for "index out of range".
Definition: Exception.h:297
Exception class for "overflow".
Definition: Exception.h:373
const std::string & msg() const
Return the message string provided to the constructor.
Definition: Exception.h:156
int invalid_index() const
Return the offending index value.
Definition: Exception.h:325
virtual std::ostream & dumpOn(std::ostream &str) const override
Write proper error message with all relevant data.
Definition: Exception.h:349
std::string as_string() const
Returns the location in normalized string format.
const std::string & seen() const
The offending line that caused the parse error.
Definition: Exception.h:428
Exception class for "out of memory".
Definition: Exception.h:280
Exception class for faulty logic within the program.
Definition: Exception.h:266
friend std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Stream output.
Helper class for UI exceptions: Store BASE_FILE, FUNCTION and LINE.
Definition: Exception.h:48
ParseException(const std::string &msg, const std::string &seen, const std::string &expected)
Constructor.
Definition: Exception.h:411
Exception class for parse errors, e.g.
Definition: Exception.h:400
int valid_max() const
Return the valid maximum index.
Definition: Exception.h:339
Definition: Exception.h:465
const std::string & func() const
Returns the name of the function where the exception occurred.
Definition: Exception.h:77
const CodeLocation & where() const
Return CodeLocation.
Definition: Exception.h:142
void setMsg(const std::string &msg)
Set a new message string.
Definition: Exception.h:166
Exception class for generic null pointer exceptions.
Definition: Exception.h:234
int valid_min() const
Return the valid minimum index.
Definition: Exception.h:332
IndexOutOfRangeException(int invalidIndex, int validMin, int validMax, const std::string &msg="")
Constructor.
Definition: Exception.h:309
LogLevel
Enum with log levels.
Definition: Logger.h:37
Base class for storage exceptions.
Definition: Exception.h:118
int line() const
Returns the source line number where the exception occurred.
Definition: Exception.h:82
The storage namespace.
Definition: Actiongraph.h:39
void relocate(const CodeLocation &newLocation) const
Exchange location on rethrow.
Definition: Exception.h:148
const std::string & file() const
Returns the source file name where the exception occurred.
Definition: Exception.h:72
virtual ~ParseException() noexcept
Destructor.
Definition: Exception.h:422
Exception class for unsupported features and operations.
Definition: Exception.h:252
Exception class for IO errors.
Definition: Exception.h:386
const std::string & expected() const
Short textual description of what the parser expected.
Definition: Exception.h:433
CodeLocation(const std::string &file_r, const std::string &func_r, int line_r)
Constructor.
Definition: Exception.h:56