/usr/include/snapper/Exception.h is in libsnapper-dev 0.4.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | /*
* Copyright (c) [2011-2014] Novell, Inc.
* Copyright (c) [2015] SUSE LLC
*
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, contact Novell, Inc.
*
* To contact Novell about this file by physical or electronic mail, you may
* find current contact information at www.novell.com.
*/
#ifndef SNAPPER_EXCEPTION_H
#define SNAPPER_EXCEPTION_H
#include <exception>
#include <string>
namespace snapper
{
//
// Macros for application use
//
/**
* Usage summary:
*
* Use SN_THROW to throw exceptions.
* Use SN_CAUGHT If you caught an exceptions in order to handle it.
* Use SN_RETHROW to rethrow a caught exception.
*
* The use of these macros is not mandatory. But SN_THROW and SN_RETHROW
* will adjust the code location information stored in the exception. All
* three macros will drop a line in the log file.
*
* 43 try
* 44 {
* 45 try
* 46 {
* 47 SN_THROW(Exception("Something bad happened."));
* 48 }
* 49 catch (const Exception& exception)
* 50 {
* 51 SN_RETHROW(exception);
* 52 }
* 53 }
* 54 catch (const Exception& exception)
* 55 {
* 56 SN_CAUGHT(exception);
* 57 }
*
* The above produces the following log lines:
*
* Main.cc(main):47 THROW: Main.cc(main):47: Something bad happened.
* Main.cc(main):51 RETHROW: Main.cc(main):47: Something bad happened.
* Main.cc(main):56 CAUGHT: Main.cc(main):51: Something bad happened.
**/
/**
* Create CodeLocation object storing the current location.
**/
#define SN_EXCEPTION_CODE_LOCATION \
CodeLocation(__FILE__, __FUNCTION__, __LINE__)
/**
* Drops a log line and throws the Exception.
**/
#define SN_THROW(EXCEPTION) \
_SN_THROW((EXCEPTION), SN_EXCEPTION_CODE_LOCATION)
/**
* Drops a log line telling the Exception was caught and handled.
**/
#define SN_CAUGHT(EXCEPTION) \
_SN_CAUGHT((EXCEPTION), SN_EXCEPTION_CODE_LOCATION)
/**
* Drops a log line and rethrows, updating the CodeLocation.
**/
#define SN_RETHROW(EXCEPTION) \
_SN_RETHROW((EXCEPTION), SN_EXCEPTION_CODE_LOCATION)
/**
* Throw Exception built from a message string.
**/
#define SN_THROW_MSG(EXCEPTION_TYPE, MSG) \
SN_THROW(EXCEPTION_TYPE(MSG))
/**
* Throw Exception built from errno.
**/
#define SN_THROW_ERRNO(EXCEPTION_TYPE) \
SN_THROW(EXCEPTION_TYPE(Exception::strErrno(errno)))
/**
* Throw Exception built from errno provided as argument.
**/
#define SN_THROW_ERRNO1(EXCEPTION_TYPE, ERRNO) \
SN_THROW(EXCEPTION_TYPE(Exception::strErrno(ERRNO)))
/**
* Throw Exception built from errno and a message string.
**/
#define SN_THROW_ERRNO_MSG(EXCEPTION_TYPE, MSG) \
SN_THROW(EXCEPTION_TYPE(Exception::strErrno(errno, MSG)))
/**
* Throw Exception built from errno provided as argument and a message
* string.
**/
#define SN_THROW_ERRNO_MSG1(EXCEPTION_TYPE, ERRNO, MSG) \
SN_THROW(EXCEPTION_TYPE(Exception::strErrno(ERRNO, MSG)))
/**
* Helper class for UI exceptions: Store _FILE_, _FUNCTION_ and _LINE_.
* Construct this using the SN_EXCEPTION_CODE_LOCATION macro.
**/
class CodeLocation
{
public:
/**
* Constructor.
* Commonly called using the SN_EXCEPTION_CODE_LOCATION macro.
**/
CodeLocation(const std::string& file_r, const std::string& func_r, int line_r)
: _file(file_r), _func(func_r), _line(line_r) {}
/**
* Default constructor.
***/
CodeLocation()
: _line(0) {}
/**
* Returns the source file name where the exception occured.
**/
const std::string& file() const { return _file; }
/**
* Returns the name of the function where the exception occured.
**/
const std::string& func() const { return _func; }
/**
* Returns the source line number where the exception occured.
**/
int line() const { return _line; }
/**
* Returns the location in normalized string format.
**/
std::string asString() const;
/**
* Stream output
**/
friend std::ostream& operator<<(std::ostream& str, const CodeLocation& obj);
private:
std::string _file;
std::string _func;
int _line;
};
/**
* CodeLocation stream output
**/
std::ostream& operator<<(std::ostream& str, const CodeLocation& obj);
/**
* Base class for snapper exceptions.
*
* Exception offers to store a message string passed to the constructor.
* Derived classes may provide additional information.
* Overload dumpOn to provide a proper error text.
**/
class Exception : public std::exception
{
public:
/**
* Default constructor.
* Use SN_THROW to throw exceptions.
**/
Exception();
/**
* Constructor taking a message.
* Use SN_THROW to throw exceptions.
**/
Exception(const std::string& msg);
/**
* Destructor.
**/
virtual ~Exception() throw();
/**
* Return CodeLocation.
**/
const CodeLocation& where() const { return _where; }
/**
* Exchange location on rethrow.
**/
void relocate(const CodeLocation& newLocation) const { _where = newLocation; }
/**
* Return the message string provided to the constructor.
* Note: This is not neccessarily the complete error message.
* The whole error message is provided by asString or dumpOn.
**/
const std::string& msg() const { return _msg; }
/**
* Set a new message string.
**/
void setMsg(const std::string& msg) { _msg = msg; }
/**
* Error message provided by dumpOn as string.
**/
std::string asString() const;
/**
* Make a string from errno_r.
**/
static std::string strErrno(int errno_r);
/**
* Make a string from errno_r and msg_r.
**/
static std::string strErrno(int errno_r, const std::string& msg);
/**
* Drop a log line on throw, catch or rethrow.
* Used by SN_THROW macros.
**/
static void log(const Exception& exception, const CodeLocation& location,
const char* const prefix);
/**
* Return message string.
*
* Reimplemented from std::exception.
**/
virtual const char* what() const throw() { return _msg.c_str(); }
protected:
/**
* Overload this to print a proper error message.
**/
virtual std::ostream& dumpOn(std::ostream& str) const;
private:
friend std::ostream& operator<<(std::ostream& str, const Exception& obj);
mutable CodeLocation _where;
std::string _msg;
/**
* Called by std::ostream& operator<<() .
* Prints CodeLocation and the error message provided by dumpOn.
**/
std::ostream& dumpError(std::ostream& str) const;
};
/**
* Exception stream output
**/
std::ostream& operator<<(std::ostream& str, const Exception& obj);
//
// Helper templates
//
/**
* Helper for SN_THROW()
**/
template<class _Exception>
void _SN_THROW(const _Exception& exception, const CodeLocation& where)
{
exception.relocate(where);
Exception::log(exception, where, "THROW:");
throw exception;
}
/**
* Helper for SN_CAUGHT()
**/
template<class _Exception>
void _SN_CAUGHT(const _Exception& exception, const CodeLocation& where)
{
Exception::log(exception, where, "CAUGHT:");
}
/**
* Helper for SN_RETHROW()
**/
template<class _Exception>
void _SN_RETHROW(const _Exception& exception, const CodeLocation& where)
{
Exception::log(exception, where, "RETHROW:");
exception.relocate(where);
throw;
}
struct FileNotFoundException : public Exception
{
explicit FileNotFoundException() : Exception("file not found") {}
};
struct IllegalSnapshotException : public Exception
{
explicit IllegalSnapshotException() : Exception("illegal snapshot") {}
};
struct BadAllocException : public Exception
{
explicit BadAllocException() : Exception("bad alloc") {}
};
struct LogicErrorException : public Exception
{
explicit LogicErrorException() : Exception("logic error") {}
};
struct IOErrorException : public Exception
{
explicit IOErrorException(const std::string& msg) : Exception(msg) {}
};
struct AclException : public IOErrorException
{
explicit AclException() : IOErrorException("ACL error") {}
};
struct ProgramNotInstalledException : public Exception
{
explicit ProgramNotInstalledException(const std::string& msg) : Exception(msg) {}
};
struct XAttributesException : public Exception
{
explicit XAttributesException() : Exception("XAttributes error") {}
};
struct InvalidUserException : public Exception
{
explicit InvalidUserException() : Exception("invalid user") {}
};
struct InvalidGroupException : public Exception
{
explicit InvalidGroupException() : Exception("invalid group") {}
};
}
#endif
|