libstorage-ng
Exception.h
1 /*
2  * Copyright (c) [2014-2015] Novell, Inc.
3  * Copyright (c) 2016 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 
39 
40 namespace storage
41 {
42 
48  {
49  public:
54  CodeLocation( const std::string & file_r,
55  const std::string & func_r,
56  int line_r )
57  : _file(file_r), _func(func_r), _line(line_r)
58  {}
59 
64  : _file(), _func(), _line(0)
65  {}
66 
70  const std::string& file() const { return _file; }
71 
75  const std::string& func() const { return _func; }
76 
80  int line() const { return _line; }
81 
85  std::string asString() const;
86 
90  friend std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
91 
92  private:
93  std::string _file;
94  std::string _func;
95  int _line;
96 
97  }; // CodeLocation
98 
99 
103  std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
104 
105 
113  class Exception : public std::exception
114  {
115  public:
120  Exception(LogLevel log_level = ERROR);
121 
126  Exception(const std::string & msg, LogLevel log_level = ERROR);
127 
131  virtual ~Exception() noexcept;
132 
136  const CodeLocation & where() const
137  { return _where; }
138 
142  void relocate( const CodeLocation & newLocation ) const
143  { _where = newLocation; }
144 
150  const std::string & msg() const
151  { return _msg; }
152 
156  LogLevel log_level() const { return _log_level; }
157 
161  void setMsg( const std::string & msg )
162  { _msg = msg; }
163 
167  std::string asString() const;
168 
172  static std::string strErrno( int errno_r );
173 
177  static std::string strErrno( int errno_r, const std::string & msg );
178 
183  static void log( const Exception & exception,
184  const CodeLocation & location,
185  const char * const prefix );
186 
192  virtual const char * what() const noexcept
193  { return _msg.c_str(); }
194 
195  protected:
196 
200  virtual std::ostream & dumpOn( std::ostream & str ) const;
201 
202 
203  private:
204  friend std::ostream & operator<<( std::ostream & str,
205  const Exception & obj );
206 
207 
208  mutable CodeLocation _where;
209  std::string _msg;
210  LogLevel _log_level;
211 
216  std::ostream & dumpError( std::ostream & str ) const;
217 
218  }; // class Exception
219 
220 
224  std::ostream & operator<<( std::ostream & str, const Exception & obj );
225 
226 
232  {
233  public:
235  : Exception( "Null pointer", ERROR )
236  {}
237 
238  virtual ~NullPointerException() noexcept
239  {}
240  };
241 
242 
248  {
249  public:
250  NotImplementedException(const std::string& msg) : Exception(msg) {}
251  virtual ~NotImplementedException() noexcept {}
252  };
253 
254 
259  class LogicException : public Exception
260  {
261  public:
262  LogicException(const std::string& msg) : Exception(msg) {}
263  virtual ~LogicException() noexcept {}
264  };
265 
266 
272  {
273  public:
275  : Exception( "Out of memory", ERROR )
276  {}
277 
278  virtual ~OutOfMemoryException() noexcept
279  {}
280 
281  };
282 
283 
288  {
289  public:
298  IndexOutOfRangeException( int invalidIndex,
299  int validMin,
300  int validMax,
301  const std::string & msg = "" )
302  : Exception( msg )
303  , _invalidIndex( invalidIndex )
304  , _validMin( validMin )
305  , _validMax( validMax )
306  {}
307 
308  virtual ~IndexOutOfRangeException() noexcept
309  {}
310 
314  int invalidIndex() const { return _invalidIndex; }
315 
319  int validMin() const { return _validMin; }
320 
324  int validMax() const { return _validMax; }
325 
326  protected:
327 
332  virtual std::ostream & dumpOn( std::ostream & str ) const
333  {
334  std::string prefix = msg();
335 
336  if ( prefix.empty() )
337  prefix = "Index out of range";
338 
339  return str << prefix << ": " << _invalidIndex
340  << " valid: " << _validMin << " .. " << _validMax
341  << std::endl;
342  }
343 
344  private:
345 
346  int _invalidIndex;
347  int _validMin;
348  int _validMax;
349  };
350 
351 
356  {
357  public:
358 
359  OverflowException() : Exception("overflow") {}
360  virtual ~OverflowException() noexcept {}
361 
362  };
363 
364 
369  class ParseException: public Exception
370  {
371  public:
372 
380  ParseException( const std::string & msg,
381  const std::string & seen,
382  const std::string & expected ):
383  Exception( msg ),
384  _seen( seen ),
385  _expected( expected )
386  {}
387 
391  virtual ~ParseException() noexcept
392  {}
393 
397  const std::string & seen() const { return _seen; }
398 
402  const std::string & expected() const { return _expected; }
403 
408  virtual std::ostream & dumpOn( std::ostream & str ) const
409  {
410  std::string prefix = "Parse error";
411 
412  if ( ! msg().empty() )
413  prefix += ": ";
414 
415  return str << prefix << msg()
416  << "; expected: \"" << _expected
417  << "\" seen: \"" << _seen << "\""
418  << std::endl;
419  }
420 
421  private:
422 
423  std::string _seen;
424  std::string _expected;
425  };
426 
427 }
428 
429 #endif
Exception class for features not implemented.
Definition: Exception.h:247
CodeLocation()
Default constructor.
Definition: Exception.h:63
Exception class for "index out of range".
Definition: Exception.h:287
Exception class for "overflow".
Definition: Exception.h:355
int validMax() const
Return the valid maximum index.
Definition: Exception.h:324
const std::string & msg() const
Return the message string provided to the constructor.
Definition: Exception.h:150
const std::string & seen() const
The offending line that caused the parse error.
Definition: Exception.h:397
Exception class for "out of memory".
Definition: Exception.h:271
virtual std::ostream & dumpOn(std::ostream &str) const
Write proper error message with all relevant data.
Definition: Exception.h:408
Exception class for faulty logic within the program.
Definition: Exception.h:259
std::string asString() const
Returns the location in normalized string format.
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:47
ParseException(const std::string &msg, const std::string &seen, const std::string &expected)
Constructor.
Definition: Exception.h:380
Exception class for parse errors, e.g.
Definition: Exception.h:369
virtual const char * what() const noexcept
Return message string.
Definition: Exception.h:192
const std::string & func() const
Returns the name of the function where the exception occured.
Definition: Exception.h:75
const CodeLocation & where() const
Return CodeLocation.
Definition: Exception.h:136
void setMsg(const std::string &msg)
Set a new message string.
Definition: Exception.h:161
int validMin() const
Return the valid minimum index.
Definition: Exception.h:319
Exception class for generic null pointer exceptions.
Definition: Exception.h:231
IndexOutOfRangeException(int invalidIndex, int validMin, int validMax, const std::string &msg="")
Constructor.
Definition: Exception.h:298
Base class for storage exceptions.
Definition: Exception.h:113
int line() const
Returns the source line number where the exception occured.
Definition: Exception.h:80
The storage namespace.
Definition: Actiongraph.h:36
void relocate(const CodeLocation &newLocation) const
Exchange location on rethrow.
Definition: Exception.h:142
virtual std::ostream & dumpOn(std::ostream &str) const
Write proper error message with all relevant data.
Definition: Exception.h:332
const std::string & file() const
Returns the source file name where the exception occured.
Definition: Exception.h:70
virtual ~ParseException() noexcept
Destructor.
Definition: Exception.h:391
int invalidIndex() const
Return the offending index value.
Definition: Exception.h:314
const std::string & expected() const
Short textual description of what the parser expected.
Definition: Exception.h:402
CodeLocation(const std::string &file_r, const std::string &func_r, int line_r)
Constructor.
Definition: Exception.h:54