libstorage-ng
Loading...
Searching...
No Matches
Exception.h
1/*
2 * Copyright (c) [2014-2015] Novell, Inc.
3 * Copyright (c) [2016-2018] 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
40namespace 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 = LogLevel::ERROR);
121
126 Exception(const std::string & msg, LogLevel log_level = LogLevel::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 { return _msg; }
151
155 LogLevel log_level() const { return _log_level; }
156
160 void setMsg(const std::string& msg) { _msg = msg; }
161
165 std::string asString() const;
166
170 static std::string strErrno( int errno_r );
171
175 static std::string strErrno( int errno_r, const std::string & msg );
176
181 static void log( const Exception & exception,
182 const CodeLocation & location,
183 const char * const prefix );
184
190 virtual const char* what() const noexcept override { return _msg.c_str(); }
191
192 protected:
193
197 virtual std::ostream& dumpOn(std::ostream& str) const;
198
199
200 private:
201 friend std::ostream& operator<<(std::ostream& str, const Exception& obj);
202
203
204 mutable CodeLocation _where;
205 std::string _msg;
206 LogLevel _log_level;
207
212 std::ostream & dumpError( std::ostream & str ) const;
213
214 }; // class Exception
215
216
220 std::ostream & operator<<( std::ostream & str, const Exception & obj );
221
222
228 {
229 public:
231 : Exception("Null pointer", LogLevel::ERROR)
232 {}
233
234 virtual ~NullPointerException() noexcept
235 {}
236 };
237
238
244 {
245 public:
246 UnsupportedException(const std::string& msg) : Exception(msg) {}
247 virtual ~UnsupportedException() noexcept {}
248 };
249
250
256 {
257 public:
258 LogicException(const std::string& msg) : Exception(msg) {}
259 virtual ~LogicException() noexcept {}
260 };
261
262
268 {
269 public:
271 : Exception("Out of memory", LogLevel::ERROR)
272 {}
273
274 virtual ~OutOfMemoryException() noexcept
275 {}
276
277 };
278
279
284 {
285 public:
295 int validMin,
296 int validMax,
297 const std::string & msg = "" )
298 : Exception( msg )
299 , _invalidIndex( invalidIndex )
300 , _validMin( validMin )
301 , _validMax( validMax )
302 {}
303
304 virtual ~IndexOutOfRangeException() noexcept
305 {}
306
310 int invalidIndex() const { return _invalidIndex; }
311
315 int validMin() const { return _validMin; }
316
320 int validMax() const { return _validMax; }
321
322 protected:
323
328 virtual std::ostream& dumpOn(std::ostream& str) const override
329 {
330 std::string prefix = msg();
331
332 if ( prefix.empty() )
333 prefix = "Index out of range";
334
335 return str << prefix << ": " << _invalidIndex
336 << " valid: " << _validMin << " .. " << _validMax
337 << std::endl;
338 }
339
340 private:
341
342 int _invalidIndex;
343 int _validMin;
344 int _validMax;
345 };
346
347
352 {
353 public:
354
355 OverflowException() : Exception("overflow") {}
356 virtual ~OverflowException() noexcept {}
357
358 };
359
360
364 class IOException : public Exception
365 {
366 public:
367
368 IOException(const std::string& msg) : Exception(msg) {}
369 virtual ~IOException() noexcept {}
370
371 };
372
373
379 {
380 public:
381
389 ParseException( const std::string & msg,
390 const std::string & seen,
391 const std::string & expected ):
392 Exception( msg ),
393 _seen( seen ),
394 _expected( expected )
395 {}
396
400 virtual ~ParseException() noexcept
401 {}
402
406 const std::string & seen() const { return _seen; }
407
411 const std::string & expected() const { return _expected; }
412
417 virtual std::ostream& dumpOn(std::ostream& str) const override
418 {
419 std::string prefix = "Parse error";
420
421 if ( ! msg().empty() )
422 prefix += ": ";
423
424 return str << prefix << msg()
425 << "; expected: \"" << _expected
426 << "\" seen: \"" << _seen << "\""
427 << std::endl;
428 }
429
430 private:
431
432 std::string _seen;
433 std::string _expected;
434 };
435
436
437 // TODO It should be possible to catch all exception derived from
438 // Exception except of Aborted, see code in activate_* and
439 // error_callback. Or use some other method to abort function with
440 // callbacks.
441
442 class Aborted : public Exception
443 {
444 public:
445
446 Aborted(const std::string& message) : Exception(message) {}
447 virtual ~Aborted() noexcept {}
448
449 };
450
451}
452
453#endif
Definition Exception.h:443
Helper class for UI exceptions: Store BASE_FILE, FUNCTION and LINE.
Definition Exception.h:48
const std::string & file() const
Returns the source file name where the exception occurred.
Definition Exception.h:70
const std::string & func() const
Returns the name of the function where the exception occurred.
Definition Exception.h:75
friend std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Stream output.
std::string asString() const
Returns the location in normalized string format.
CodeLocation()
Default constructor.
Definition Exception.h:63
CodeLocation(const std::string &file_r, const std::string &func_r, int line_r)
Constructor.
Definition Exception.h:54
int line() const
Returns the source line number where the exception occurred.
Definition Exception.h:80
Base class for storage exceptions.
Definition Exception.h:114
static void log(const Exception &exception, const CodeLocation &location, const char *const prefix)
Drop a log line on throw, catch or rethrow.
const CodeLocation & where() const
Return CodeLocation.
Definition Exception.h:136
static std::string strErrno(int errno_r)
Make a string from errno_r.
void setMsg(const std::string &msg)
Set a new message string.
Definition Exception.h:160
Exception(LogLevel log_level=LogLevel::ERROR)
Default constructor.
friend std::ostream & operator<<(std::ostream &str, const Exception &obj)
Exception stream output.
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.
Exception(const std::string &msg, LogLevel log_level=LogLevel::ERROR)
Constructor taking a message.
const std::string & msg() const
Return the message string provided to the constructor.
Definition Exception.h:150
static std::string strErrno(int errno_r, const std::string &msg)
Make a string from errno_r and msg_r.
virtual const char * what() const noexcept override
Return message string.
Definition Exception.h:190
void relocate(const CodeLocation &newLocation) const
Exchange location on rethrow.
Definition Exception.h:142
virtual ~Exception() noexcept
Destructor.
Exception class for IO errors.
Definition Exception.h:365
Exception class for "index out of range".
Definition Exception.h:284
int validMax() const
Return the valid maximum index.
Definition Exception.h:320
IndexOutOfRangeException(int invalidIndex, int validMin, int validMax, const std::string &msg="")
Constructor.
Definition Exception.h:294
virtual std::ostream & dumpOn(std::ostream &str) const override
Write proper error message with all relevant data.
Definition Exception.h:328
int validMin() const
Return the valid minimum index.
Definition Exception.h:315
int invalidIndex() const
Return the offending index value.
Definition Exception.h:310
Exception class for faulty logic within the program.
Definition Exception.h:256
Exception class for generic null pointer exceptions.
Definition Exception.h:228
Exception class for "out of memory".
Definition Exception.h:268
Exception class for "overflow".
Definition Exception.h:352
Exception class for parse errors, e.g.
Definition Exception.h:379
virtual ~ParseException() noexcept
Destructor.
Definition Exception.h:400
ParseException(const std::string &msg, const std::string &seen, const std::string &expected)
Constructor.
Definition Exception.h:389
const std::string & expected() const
Short textual description of what the parser expected.
Definition Exception.h:411
virtual std::ostream & dumpOn(std::ostream &str) const override
Write proper error message with all relevant data.
Definition Exception.h:417
const std::string & seen() const
The offending line that caused the parse error.
Definition Exception.h:406
Exception class for unsupported features and operations.
Definition Exception.h:244
The storage namespace.
Definition Actiongraph.h:40
std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
CodeLocation stream output.
LogLevel
Enum with log levels.
Definition Logger.h:37