| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2002 Jürgen Riegel <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This program is free software; you can redistribute it and/or modify * |
| 9 |
* it under the terms of the GNU Library General Public License (LGPL) * |
| 10 |
* as published by the Free Software Foundation; either version 2 of * |
| 11 |
* the License, or (at your option) any later version. * |
| 12 |
* for detail see the LICENCE text file. * |
| 13 |
* * |
| 14 |
* FreeCAD is distributed in the hope that it will be useful, * |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 17 |
* GNU Library General Public License for more details. * |
| 18 |
* * |
| 19 |
* You should have received a copy of the GNU Library General Public * |
| 20 |
* License along with FreeCAD; if not, write to the Free Software * |
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * |
| 22 |
* USA * |
| 23 |
* * |
| 24 |
***************************************************************************/ |
| 25 |
|
| 26 |
#ifndef BASE_EXCEPTION_H |
| 27 |
#define BASE_EXCEPTION_H |
| 28 |
|
| 29 |
#include <FCConfig.h> |
| 30 |
|
| 31 |
#include <csignal> |
| 32 |
#include <source_location> |
| 33 |
#include <string> |
| 34 |
|
| 35 |
#include "BaseClass.h" |
| 36 |
#include "FileInfo.h" |
| 37 |
|
| 38 |
using PyObject = struct _object; // NOLINT |
| 39 |
|
| 40 |
// Remove once all used compilers support this |
| 41 |
#if defined(__cpp_lib_source_location) |
| 42 |
# define HAVE_STD_SOURCE_LOCATION 1 |
| 43 |
#else |
| 44 |
# undef HAVE_STD_SOURCE_LOCATION |
| 45 |
#endif |
| 46 |
// std::source_location is implemented, but buggy in Clang 15 |
| 47 |
#if defined(__clang__) && __clang_major__ <= 15 |
| 48 |
# undef HAVE_STD_SOURCE_LOCATION |
| 49 |
#endif |
| 50 |
|
| 51 |
/// The macros do NOT mark any message for translation |
| 52 |
/// If you want to mark text for translation, use the QT_TRANSLATE_NOOP macro |
| 53 |
/// with the context "Exceptions" and the right throwing macro from below (the one ending with T) |
| 54 |
/// example: |
| 55 |
/// THROWMT(Base::ValueError,QT_TRANSLATE_NOOP("Exceptions","The multiplicity cannot be increased |
| 56 |
/// beyond the degree of the B-Spline.")); |
| 57 |
/// |
| 58 |
/// N.B.: The QT_TRANSLATE_NOOP macro won't translate your string. It will just allow lupdate to |
| 59 |
/// identify that string for translation so that if you ask for a translation (and the translator |
| 60 |
/// have provided one) at that time it gets translated (e.g. in the UI before showing the message |
| 61 |
/// of the exception). |
| 62 |
|
| 63 |
#if defined(HAVE_STD_SOURCE_LOCATION) |
| 64 |
// NOLINTBEGIN(*-macro-usage) |
| 65 |
# define THROWM(exc, msg) Base::setupAndThrowException<exc>((msg), std::source_location::current()); |
| 66 |
# define THROWMT(exc, msg) \ |
| 67 |
Base::setupAndThrowException<exc>((msg), std::source_location::current(), true); |
| 68 |
# define FC_THROWM(exception, msg) \ |
| 69 |
do { \ |
| 70 |
std::stringstream ss; \ |
| 71 |
ss << msg; \ |
| 72 |
THROWM(exception, ss.str()); \ |
| 73 |
} while (0) |
| 74 |
// NOLINTEND(*-macro-usage) |
| 75 |
|
| 76 |
namespace Base |
| 77 |
{ |
| 78 |
template<typename ExceptionType> |
| 79 |
[[noreturn]] void setupAndThrowException( |
| 80 |
const std::string message, |
| 81 |
const std::source_location location, |
| 82 |
const bool translatable = false |
| 83 |
) |
| 84 |
{ |
| 85 |
ExceptionType exception {message}; |
| 86 |
exception.setTranslatable(translatable); |
| 87 |
exception.setDebugInformation(location); |
| 88 |
throw exception; |
| 89 |
} // NOLINT // unreachable |
| 90 |
} // namespace Base |
| 91 |
|
| 92 |
#else // HAVE_STD_SOURCE_LOCATION |
| 93 |
|
| 94 |
# ifdef _MSC_VER |
| 95 |
# define FC_THROW_INFO __FILE__, __LINE__, __FUNCSIG__ |
| 96 |
# elif __GNUC__ |
| 97 |
# define FC_THROW_INFO __FILE__, __LINE__, __PRETTY_FUNCTION__ |
| 98 |
# else |
| 99 |
# define FC_THROW_INFO __FILE__, __LINE__, __func__ |
| 100 |
# endif |
| 101 |
|
| 102 |
# define THROWM(exc, msg) Base::setupAndThrowException<exc>(msg, FC_THROW_INFO); |
| 103 |
# define THROWMT(exc, msg) Base::setupAndThrowException<exc>(msg, FC_THROW_INFO, true); |
| 104 |
# define FC_THROWM(exception, msg) \ |
| 105 |
do { \ |
| 106 |
std::stringstream ss; \ |
| 107 |
ss << msg; \ |
| 108 |
THROWM(exception, ss.str()); \ |
| 109 |
} while (0) |
| 110 |
namespace Base |
| 111 |
{ |
| 112 |
template<typename ExceptionType> |
| 113 |
[[noreturn]] void setupAndThrowException( |
| 114 |
const std::string message, |
| 115 |
const char* file, |
| 116 |
const int line, |
| 117 |
const char* func, |
| 118 |
const bool translatable = false |
| 119 |
) |
| 120 |
{ |
| 121 |
ExceptionType exception {message}; |
| 122 |
exception.setTranslatable(translatable); |
| 123 |
exception.setDebugInformation(file, line, func); |
| 124 |
throw exception; |
| 125 |
} // NOLINT // unreachable |
| 126 |
} // namespace Base |
| 127 |
|
| 128 |
#endif // HAVE_STD_SOURCE_LOCATION |
| 129 |
|
| 130 |
//-------------------------------------------------------------------------------------------------- |
| 131 |
|
| 132 |
template<typename Exception> |
| 133 |
constexpr void THROWM_( |
| 134 |
const std::string& msg, |
| 135 |
const std::source_location location = std::source_location::current() |
| 136 |
) |
| 137 |
{ |
| 138 |
Base::setupAndThrowException<Exception>(msg, location); |
| 139 |
} |
| 140 |
|
| 141 |
template<typename Exception> |
| 142 |
constexpr void THROWMT_( |
| 143 |
const std::string& msg, |
| 144 |
const std::source_location location = std::source_location::current() |
| 145 |
) |
| 146 |
{ |
| 147 |
Base::setupAndThrowException<Exception>(msg, location, true); |
| 148 |
} |
| 149 |
|
| 150 |
template<typename Exception> |
| 151 |
constexpr void FC_THROWM_( |
| 152 |
const std::string& raw_msg, |
| 153 |
const std::source_location location = std::source_location::current() |
| 154 |
) |
| 155 |
{ |
| 156 |
THROWM_<Exception>(raw_msg, location); |
| 157 |
} |
| 158 |
|
| 159 |
namespace Base |
| 160 |
{ |
| 161 |
|
| 162 |
class BaseExport Exception: public BaseClass |
| 163 |
{ |
| 164 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 165 |
|
| 166 |
public: |
| 167 |
explicit Exception(std::string message = "FreeCAD Exception"); |
| 168 |
~Exception() noexcept override = default; |
| 169 |
|
| 170 |
Exception& operator=(const Exception& inst); |
| 171 |
Exception& operator=(Exception&& inst) noexcept; |
| 172 |
|
| 173 |
virtual const char* what() const noexcept; |
| 174 |
virtual void reportException() const; // once only |
| 175 |
|
| 176 |
inline void setMessage(const std::string& message); |
| 177 |
// what may differ from the message given by the user in |
| 178 |
// derived classes |
| 179 |
inline std::string getMessage() const; |
| 180 |
inline std::string getFile() const; |
| 181 |
inline int getLine() const; |
| 182 |
inline std::string getFunction() const; |
| 183 |
inline bool getTranslatable() const; |
| 184 |
inline bool getReported() const; |
| 185 |
inline void setReported(bool reported) const; |
| 186 |
|
| 187 |
#if defined(HAVE_STD_SOURCE_LOCATION) |
| 188 |
inline void setDebugInformation(const std::source_location& location); |
| 189 |
#else |
| 190 |
inline void setDebugInformation(const char* file, int line, const char* func); |
| 191 |
#endif |
| 192 |
|
| 193 |
inline void setTranslatable(bool translatable); |
| 194 |
|
| 195 |
PyObject* getPyObject() override; // exception data |
| 196 |
void setPyObject(PyObject* pydict) override; // set the exception data |
| 197 |
|
| 198 |
virtual PyObject* getPyExceptionType() const; |
| 199 |
virtual void setPyException() const; |
| 200 |
|
| 201 |
protected: |
| 202 |
Exception(const Exception& inst); |
| 203 |
Exception(Exception&& inst) noexcept; |
| 204 |
|
| 205 |
private: |
| 206 |
std::string errorMessage; |
| 207 |
std::string fileName; |
| 208 |
int lineNum {0}; |
| 209 |
std::string functionName; |
| 210 |
bool isTranslatable {false}; |
| 211 |
mutable bool hasBeenReported {false}; |
| 212 |
}; |
| 213 |
|
| 214 |
class BaseExport AbortException: public Exception |
| 215 |
{ |
| 216 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 217 |
|
| 218 |
public: |
| 219 |
explicit AbortException(const std::string& message = "Aborted operation"); |
| 220 |
|
| 221 |
const char* what() const noexcept override; |
| 222 |
PyObject* getPyExceptionType() const override; |
| 223 |
}; |
| 224 |
|
| 225 |
class BaseExport XMLBaseException: public Exception |
| 226 |
{ |
| 227 |
public: |
| 228 |
explicit XMLBaseException(const std::string& message = "XML base exception"); |
| 229 |
|
| 230 |
PyObject* getPyExceptionType() const override; |
| 231 |
}; |
| 232 |
|
| 233 |
class BaseExport XMLParseException: public XMLBaseException |
| 234 |
{ |
| 235 |
public: |
| 236 |
explicit XMLParseException(const std::string& message = "XML parse exception"); |
| 237 |
|
| 238 |
const char* what() const noexcept override; |
| 239 |
PyObject* getPyExceptionType() const override; |
| 240 |
}; |
| 241 |
|
| 242 |
class BaseExport XMLAttributeError: public XMLBaseException |
| 243 |
{ |
| 244 |
public: |
| 245 |
explicit XMLAttributeError(const std::string& message = "XML attribute error"); |
| 246 |
|
| 247 |
const char* what() const noexcept override; |
| 248 |
PyObject* getPyExceptionType() const override; |
| 249 |
}; |
| 250 |
|
| 251 |
class BaseExport FileException: public Exception |
| 252 |
{ |
| 253 |
public: |
| 254 |
explicit FileException( |
| 255 |
const std::string& message = "Unknown file exception happened", |
| 256 |
const std::string& fileName = "" |
| 257 |
); |
| 258 |
FileException(const std::string& message, const FileInfo& File); |
| 259 |
|
| 260 |
const char* what() const noexcept override; |
| 261 |
void reportException() const override; |
| 262 |
std::string getFileName() const; |
| 263 |
PyObject* getPyObject() override; |
| 264 |
|
| 265 |
void setPyObject(PyObject* pydict) override; |
| 266 |
|
| 267 |
PyObject* getPyExceptionType() const override; |
| 268 |
|
| 269 |
private: |
| 270 |
FileInfo file; |
| 271 |
// necessary for what() legacy behaviour as it returns a buffer that |
| 272 |
// can not be of a temporary object to be destroyed at end of what() |
| 273 |
std::string _sErrMsgAndFileName; |
| 274 |
void setFileName(const std::string& fileName); |
| 275 |
}; |
| 276 |
|
| 277 |
class BaseExport FileSystemError: public Exception |
| 278 |
{ |
| 279 |
public: |
| 280 |
explicit FileSystemError(const std::string& message = "File system error"); |
| 281 |
FileSystemError(const FileSystemError&) = default; |
| 282 |
FileSystemError(FileSystemError&&) = default; |
| 283 |
|
| 284 |
~FileSystemError() noexcept override = default; |
| 285 |
FileSystemError& operator=(const FileSystemError&) = default; |
| 286 |
FileSystemError& operator=(FileSystemError&&) = default; |
| 287 |
|
| 288 |
PyObject* getPyExceptionType() const override; |
| 289 |
}; |
| 290 |
|
| 291 |
/** errors in a data structure */ |
| 292 |
class BaseExport BadFormatError: public Exception |
| 293 |
{ |
| 294 |
public: |
| 295 |
explicit BadFormatError(const std::string& message = "Bad format error"); |
| 296 |
BadFormatError(const BadFormatError&) = default; |
| 297 |
BadFormatError(BadFormatError&&) = default; |
| 298 |
|
| 299 |
~BadFormatError() noexcept override = default; |
| 300 |
BadFormatError& operator=(const BadFormatError&) = default; |
| 301 |
BadFormatError& operator=(BadFormatError&&) = default; |
| 302 |
PyObject* getPyExceptionType() const override; |
| 303 |
}; |
| 304 |
|
| 305 |
#if defined(__GNUC__) |
| 306 |
// calling instance of our new handler expects a bad_alloc exception |
| 307 |
class BaseExport MemoryException: public Exception, virtual public std::bad_alloc |
| 308 |
#else |
| 309 |
class BaseExport MemoryException: public Exception |
| 310 |
#endif |
| 311 |
{ |
| 312 |
public: |
| 313 |
explicit MemoryException(const std::string& = "Not enough memory available"); |
| 314 |
|
| 315 |
#if defined(__GNUC__) |
| 316 |
const char* what() const noexcept override; |
| 317 |
#endif |
| 318 |
|
| 319 |
PyObject* getPyExceptionType() const override; |
| 320 |
}; |
| 321 |
|
| 322 |
/** can be used in an own signal handler */ |
| 323 |
class BaseExport AccessViolation: public Exception |
| 324 |
{ |
| 325 |
public: |
| 326 |
explicit AccessViolation(const std::string& message = "Access violation"); |
| 327 |
PyObject* getPyExceptionType() const override; |
| 328 |
}; |
| 329 |
|
| 330 |
/** can be used in an own signal handler */ |
| 331 |
class BaseExport AbnormalProgramTermination: public Exception |
| 332 |
{ |
| 333 |
public: |
| 334 |
explicit AbnormalProgramTermination(const std::string& message = "Abnormal program termination"); |
| 335 |
PyObject* getPyExceptionType() const override; |
| 336 |
}; |
| 337 |
|
| 338 |
class BaseExport UnknownProgramOption: public Exception |
| 339 |
{ |
| 340 |
public: |
| 341 |
explicit UnknownProgramOption(const std::string& message = "Unknown program option"); |
| 342 |
PyObject* getPyExceptionType() const override; |
| 343 |
}; |
| 344 |
|
| 345 |
class BaseExport ProgramInformation: public Exception |
| 346 |
{ |
| 347 |
public: |
| 348 |
explicit ProgramInformation(const std::string& message = "Program information"); |
| 349 |
}; |
| 350 |
|
| 351 |
class BaseExport TypeError: public Exception |
| 352 |
{ |
| 353 |
public: |
| 354 |
explicit TypeError(const std::string& message = "Type error"); |
| 355 |
PyObject* getPyExceptionType() const override; |
| 356 |
}; |
| 357 |
|
| 358 |
class BaseExport ValueError: public Exception |
| 359 |
{ |
| 360 |
public: |
| 361 |
explicit ValueError(const std::string& message = "Value error"); |
| 362 |
PyObject* getPyExceptionType() const override; |
| 363 |
}; |
| 364 |
|
| 365 |
/** sequence subscript is out of range */ |
| 366 |
class BaseExport IndexError: public Exception |
| 367 |
{ |
| 368 |
public: |
| 369 |
explicit IndexError(const std::string& message = "Index error"); |
| 370 |
PyObject* getPyExceptionType() const override; |
| 371 |
}; |
| 372 |
|
| 373 |
class BaseExport NameError: public Exception |
| 374 |
{ |
| 375 |
public: |
| 376 |
explicit NameError(const std::string& message = "Name error"); |
| 377 |
PyObject* getPyExceptionType() const override; |
| 378 |
}; |
| 379 |
|
| 380 |
class BaseExport ImportError: public Exception |
| 381 |
{ |
| 382 |
public: |
| 383 |
explicit ImportError(const std::string& message = "Import error"); |
| 384 |
PyObject* getPyExceptionType() const override; |
| 385 |
}; |
| 386 |
|
| 387 |
class BaseExport AttributeError: public Exception |
| 388 |
{ |
| 389 |
public: |
| 390 |
explicit AttributeError(const std::string& message = "Attribute error"); |
| 391 |
PyObject* getPyExceptionType() const override; |
| 392 |
}; |
| 393 |
|
| 394 |
class BaseExport PropertyError: public AttributeError |
| 395 |
{ |
| 396 |
public: |
| 397 |
explicit PropertyError(const std::string& message = "Property error"); |
| 398 |
PyObject* getPyExceptionType() const override; |
| 399 |
}; |
| 400 |
|
| 401 |
class BaseExport RuntimeError: public Exception |
| 402 |
{ |
| 403 |
public: |
| 404 |
explicit RuntimeError(const std::string& message = "Runtime error"); |
| 405 |
PyObject* getPyExceptionType() const override; |
| 406 |
}; |
| 407 |
|
| 408 |
class BaseExport BadGraphError: public RuntimeError |
| 409 |
{ |
| 410 |
public: |
| 411 |
explicit BadGraphError(const std::string& message = "Bad graph error"); |
| 412 |
PyObject* getPyExceptionType() const override; |
| 413 |
}; |
| 414 |
|
| 415 |
class BaseExport NotImplementedError: public Exception |
| 416 |
{ |
| 417 |
public: |
| 418 |
explicit NotImplementedError(const std::string& message = "Not implemented error"); |
| 419 |
PyObject* getPyExceptionType() const override; |
| 420 |
}; |
| 421 |
|
| 422 |
class BaseExport ZeroDivisionError: public Exception |
| 423 |
{ |
| 424 |
public: |
| 425 |
explicit ZeroDivisionError(const std::string& message = "Zero division error"); |
| 426 |
PyObject* getPyExceptionType() const override; |
| 427 |
}; |
| 428 |
|
| 429 |
class BaseExport ReferenceError: public Exception |
| 430 |
{ |
| 431 |
public: |
| 432 |
explicit ReferenceError(const std::string& message = "Reference error"); |
| 433 |
PyObject* getPyExceptionType() const override; |
| 434 |
}; |
| 435 |
|
| 436 |
class BaseExport ExpressionError: public Exception |
| 437 |
{ |
| 438 |
public: |
| 439 |
explicit ExpressionError(const std::string& message = "Expression error"); |
| 440 |
PyObject* getPyExceptionType() const override; |
| 441 |
}; |
| 442 |
|
| 443 |
class BaseExport ParserError: public Exception |
| 444 |
{ |
| 445 |
public: |
| 446 |
explicit ParserError(const std::string& message = "Parser error"); |
| 447 |
PyObject* getPyExceptionType() const override; |
| 448 |
}; |
| 449 |
|
| 450 |
class BaseExport UnicodeError: public Exception |
| 451 |
{ |
| 452 |
public: |
| 453 |
explicit UnicodeError(const std::string& message = "Unicode error"); |
| 454 |
PyObject* getPyExceptionType() const override; |
| 455 |
}; |
| 456 |
|
| 457 |
class BaseExport OverflowError: public Exception |
| 458 |
{ |
| 459 |
public: |
| 460 |
explicit OverflowError(const std::string& message = "Overflow error"); |
| 461 |
PyObject* getPyExceptionType() const override; |
| 462 |
}; |
| 463 |
|
| 464 |
class BaseExport UnderflowError: public Exception |
| 465 |
{ |
| 466 |
public: |
| 467 |
explicit UnderflowError(const std::string& message = "Underflow error"); |
| 468 |
PyObject* getPyExceptionType() const override; |
| 469 |
}; |
| 470 |
|
| 471 |
class BaseExport UnitsMismatchError: public Exception |
| 472 |
{ |
| 473 |
public: |
| 474 |
explicit UnitsMismatchError(const std::string& message = "Units mismatch error"); |
| 475 |
PyObject* getPyExceptionType() const override; |
| 476 |
}; |
| 477 |
|
| 478 |
class BaseExport CADKernelError: public Exception |
| 479 |
{ |
| 480 |
public: |
| 481 |
explicit CADKernelError(const std::string& message = "CAD kernel error"); |
| 482 |
PyObject* getPyExceptionType() const override; |
| 483 |
}; |
| 484 |
|
| 485 |
class BaseExport RestoreError: public Exception |
| 486 |
{ |
| 487 |
public: |
| 488 |
explicit RestoreError(const std::string& message = "Restore error"); |
| 489 |
PyObject* getPyExceptionType() const override; |
| 490 |
}; |
| 491 |
|
| 492 |
inline void Exception::setMessage(const std::string& message) |
| 493 |
{ |
| 494 |
errorMessage = message; |
| 495 |
} |
| 496 |
|
| 497 |
inline std::string Exception::getMessage() const |
| 498 |
{ |
| 499 |
return errorMessage; |
| 500 |
} |
| 501 |
|
| 502 |
inline std::string Exception::getFile() const |
| 503 |
{ |
| 504 |
return fileName; |
| 505 |
} |
| 506 |
|
| 507 |
inline int Exception::getLine() const |
| 508 |
{ |
| 509 |
return lineNum; |
| 510 |
} |
| 511 |
|
| 512 |
inline std::string Exception::getFunction() const |
| 513 |
{ |
| 514 |
return functionName; |
| 515 |
} |
| 516 |
|
| 517 |
inline bool Exception::getTranslatable() const |
| 518 |
{ |
| 519 |
return isTranslatable; |
| 520 |
} |
| 521 |
|
| 522 |
inline bool Exception::getReported() const |
| 523 |
{ |
| 524 |
return hasBeenReported; |
| 525 |
} |
| 526 |
|
| 527 |
inline void Exception::setReported(const bool reported) const |
| 528 |
{ |
| 529 |
hasBeenReported = reported; |
| 530 |
} |
| 531 |
|
| 532 |
#if defined(HAVE_STD_SOURCE_LOCATION) |
| 533 |
inline void Exception::setDebugInformation(const std::source_location& location) |
| 534 |
{ |
| 535 |
fileName = location.file_name(); |
| 536 |
lineNum = static_cast<int>(location.line()); |
| 537 |
functionName = location.function_name(); |
| 538 |
} |
| 539 |
#else |
| 540 |
inline void Exception::setDebugInformation(const char* file, int line, const char* func) |
| 541 |
{ |
| 542 |
fileName = file; |
| 543 |
lineNum = line; |
| 544 |
functionName = func; |
| 545 |
} |
| 546 |
#endif |
| 547 |
|
| 548 |
inline void Exception::setTranslatable(const bool translatable) |
| 549 |
{ |
| 550 |
isTranslatable = translatable; |
| 551 |
} |
| 552 |
|
| 553 |
#if defined(__GNUC__) && defined(FC_OS_LINUX) |
| 554 |
class SignalException |
| 555 |
{ |
| 556 |
public: |
| 557 |
SignalException(); |
| 558 |
~SignalException(); |
| 559 |
|
| 560 |
private: |
| 561 |
static void throw_signal(int signum); |
| 562 |
|
| 563 |
struct sigaction new_action {}; // NOLINT (keep struct) |
| 564 |
struct sigaction old_action {}; // NOLINT (keep struct) |
| 565 |
bool ok {false}; |
| 566 |
}; |
| 567 |
#endif |
| 568 |
|
| 569 |
} // namespace Base |
| 570 |
|
| 571 |
#endif // BASE_EXCEPTION_H |