Debian Codegraph @

src/Base/Exception.cpp

raw
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 #include <utility>
27
28 #include <FCConfig.h>
29
30 #include "Console.h"
31 #include "PyObjectBase.h"
32
33 #include "Exception.h"
34
35 FC_LOG_LEVEL_INIT("Exception", true, true)
36
37 using namespace Base;
38
39
40 TYPESYSTEM_SOURCE(Base::Exception, Base::BaseClass)
41 Exception::Exception(std::string message)
42 : errorMessage {std::move(message)}
43 {}
44
45 Exception::Exception(const Exception& inst) = default;
46
47 Exception::Exception(Exception&& inst) noexcept = default;
48
49 Exception& Exception::operator=(const Exception& inst)
50 {
51 errorMessage = inst.errorMessage;
52 fileName = inst.fileName;
53 lineNum = inst.lineNum;
54 functionName = inst.functionName;
55 isTranslatable = inst.isTranslatable;
56 return *this;
57 }
58
59 Exception& Exception::operator=(Exception&& inst) noexcept
60 {
61 errorMessage = std::move(inst.errorMessage);
62 fileName = std::move(inst.fileName);
63 lineNum = inst.lineNum;
64 functionName = std::move(inst.functionName);
65 isTranslatable = inst.isTranslatable;
66 return *this;
67 }
68
69 const char* Exception::what() const noexcept
70 {
71 return errorMessage.c_str();
72 }
73
74 void Exception::reportException() const
75 {
76 if (hasBeenReported) {
77 return;
78 }
79
80 std::string msg = errorMessage.empty() ? typeid(*this).name() : errorMessage;
81
82 #ifdef FC_DEBUG
83 if (!functionName.empty()) {
84 msg = functionName + " -- " + msg;
85 }
86 #endif
87
88 _FC_ERR(fileName.c_str(), lineNum, msg);
89 hasBeenReported = true;
90 }
91
92 PyObject* Exception::getPyObject()
93 {
94 Py::Dict edict;
95 edict.setItem("sclassname", Py::String(typeid(*this).name()));
96 edict.setItem("sErrMsg", Py::String(getMessage()));
97 edict.setItem("sfile", Py::String(getFile()));
98 edict.setItem("iline", Py::Long(getLine()));
99 edict.setItem("sfunction", Py::String(getFunction()));
100 edict.setItem("swhat", Py::String(what()));
101 edict.setItem("btranslatable", Py::Boolean(getTranslatable()));
102 edict.setItem("breported", Py::Boolean(hasBeenReported));
103 return new_reference_to(edict);
104 }
105
106 void Exception::setPyObject(PyObject* pydict)
107 {
108 try {
109 if (pydict && Py::_Dict_Check(pydict)) {
110 const Py::Dict edict(pydict);
111 if (edict.hasKey("sfile")) {
112 fileName = Py::String(edict.getItem("sfile"));
113 }
114
115 if (edict.hasKey("sfunction")) {
116 functionName = Py::String(edict.getItem("sfunction"));
117 }
118
119 if (edict.hasKey("sErrMsg")) {
120 errorMessage = Py::String(edict.getItem("sErrMsg"));
121 }
122
123 if (edict.hasKey("iline")) {
124 lineNum = static_cast<int>(Py::Long(edict.getItem("iline")));
125 }
126 if (edict.hasKey("btranslatable")) {
127 isTranslatable = static_cast<bool>(Py::Boolean(edict.getItem("btranslatable")));
128 }
129 if (edict.hasKey("breported")) {
130 hasBeenReported = static_cast<bool>(Py::Boolean(edict.getItem("breported")));
131 }
132 }
133 }
134 catch (Py::Exception& e) {
135 e.clear(); // should never happen
136 }
137 }
138
139 PyObject* Exception::getPyExceptionType() const
140 {
141 return PyExc_FC_GeneralError;
142 }
143
144 void Exception::setPyException() const
145 {
146 PyObject* exc = getPyExceptionType();
147 if (!exc) {
148 exc = PyExc_FC_GeneralError;
149 }
150
151 PyErr_SetString(exc, what());
152 }
153
154 // ---------------------------------------------------------
155
156 TYPESYSTEM_SOURCE(Base::AbortException, Base::Exception)
157
158 AbortException::AbortException(const std::string& message)
159 : Exception(message)
160 {}
161
162 const char* AbortException::what() const noexcept
163 {
164 return Exception::what();
165 }
166
167 PyObject* AbortException::getPyExceptionType() const
168 {
169 return PyExc_FC_FreeCADAbort;
170 }
171
172 // ---------------------------------------------------------
173
174 XMLBaseException::XMLBaseException(const std::string& message)
175 : Exception(message)
176 {}
177
178 PyObject* XMLBaseException::getPyExceptionType() const
179 {
180 return PyExc_FC_XMLBaseException;
181 }
182
183 // ---------------------------------------------------------
184
185 XMLParseException::XMLParseException(const std::string& message)
186 : XMLBaseException(message)
187 {}
188
189 const char* XMLParseException::what() const noexcept
190 {
191 return XMLBaseException::what();
192 }
193
194 PyObject* XMLParseException::getPyExceptionType() const
195 {
196 return PyExc_FC_XMLParseException;
197 }
198
199 // ---------------------------------------------------------
200
201 XMLAttributeError::XMLAttributeError(const std::string& message)
202 : XMLBaseException(message)
203 {}
204
205 const char* XMLAttributeError::what() const noexcept
206 {
207 return XMLBaseException::what();
208 }
209
210 PyObject* XMLAttributeError::getPyExceptionType() const
211 {
212 return PyExc_FC_XMLAttributeError;
213 }
214
215 // ---------------------------------------------------------
216
217 FileException::FileException(const std::string& message, const std::string& fileName)
218 : Exception(message)
219 , file(fileName)
220 {
221 setFileName(fileName);
222 }
223
224 FileException::FileException(const std::string& message, const FileInfo& File)
225 : Exception(message)
226 , file(File)
227 {
228 setFileName(File.filePath());
229 }
230
231 void FileException::setFileName(const std::string& fileName)
232 {
233 file.setFile(fileName);
234 _sErrMsgAndFileName = getMessage();
235 if (!getFile().empty()) {
236 _sErrMsgAndFileName += ": ";
237 _sErrMsgAndFileName += fileName;
238 }
239 }
240
241 std::string FileException::getFileName() const
242 {
243 return file.fileName();
244 }
245
246 const char* FileException::what() const noexcept
247 {
248 return _sErrMsgAndFileName.c_str();
249 }
250
251 void FileException::reportException() const
252 {
253 if (getReported()) {
254 return;
255 }
256 std::string msg = _sErrMsgAndFileName.empty() ? typeid(*this).name() : _sErrMsgAndFileName;
257
258 #ifdef FC_DEBUG
259 if (!getFunction().empty()) {
260 msg = getFunction() + " -- " + msg;
261 }
262 #endif
263
264 _FC_ERR(getFile().c_str(), getLine(), msg);
265 setReported(true);
266 }
267
268 PyObject* FileException::getPyObject()
269 {
270 Py::Dict edict(Exception::getPyObject(), true);
271 edict.setItem("filename", Py::String(this->file.fileName()));
272 return new_reference_to(edict);
273 }
274
275 void FileException::setPyObject(PyObject* pydict)
276 {
277 if (pydict) {
278 Exception::setPyObject(pydict);
279
280 if (const Py::Dict edict(pydict); edict.hasKey("filename")) {
281 setFileName(Py::String(edict.getItem("filename")).as_std_string("utf-8"));
282 }
283 }
284 }
285
286 PyObject* FileException::getPyExceptionType() const
287 {
288 return PyExc_IOError;
289 }
290
291 // ---------------------------------------------------------
292
293 FileSystemError::FileSystemError(const std::string& message)
294 : Exception(message)
295 {}
296
297 PyObject* FileSystemError::getPyExceptionType() const
298 {
299 return PyExc_IOError;
300 }
301
302 // ---------------------------------------------------------
303
304 BadFormatError::BadFormatError(const std::string& message)
305 : Exception(message)
306 {}
307
308 PyObject* BadFormatError::getPyExceptionType() const
309 {
310 return PyExc_FC_BadFormatError;
311 }
312
313 // ---------------------------------------------------------
314
315 MemoryException::MemoryException(const std::string& message)
316 : Exception(message) // NOLINT(*-throw-keyword-missing)
317 {}
318
319 #if defined(__GNUC__)
320 const char* MemoryException::what() const noexcept
321 {
322 return Exception::what(); // from Exception, not std::bad_alloc
323 }
324 #endif
325
326 PyObject* MemoryException::getPyExceptionType() const
327 {
328 return PyExc_MemoryError;
329 }
330
331 // ---------------------------------------------------------
332
333 AccessViolation::AccessViolation(const std::string& message)
334 : Exception(message)
335 {}
336
337 PyObject* AccessViolation::getPyExceptionType() const
338 {
339 return PyExc_OSError;
340 }
341
342 // ---------------------------------------------------------
343
344 AbnormalProgramTermination::AbnormalProgramTermination(const std::string& message)
345 : Exception(message)
346 {}
347
348 PyObject* AbnormalProgramTermination::getPyExceptionType() const
349 {
350 return PyExc_InterruptedError;
351 }
352
353 // ---------------------------------------------------------
354
355 UnknownProgramOption::UnknownProgramOption(const std::string& message)
356 : Exception(message)
357 {}
358
359 PyObject* UnknownProgramOption::getPyExceptionType() const
360 {
361 return PyExc_FC_UnknownProgramOption;
362 }
363
364 // ---------------------------------------------------------
365
366 ProgramInformation::ProgramInformation(const std::string& message)
367 : Exception(message)
368 {}
369
370 // ---------------------------------------------------------
371
372 TypeError::TypeError(const std::string& message)
373 : Exception(message)
374 {}
375
376 PyObject* TypeError::getPyExceptionType() const
377 {
378 return PyExc_TypeError;
379 }
380
381 // ---------------------------------------------------------
382
383 ValueError::ValueError(const std::string& message)
384 : Exception(message)
385 {}
386
387 PyObject* ValueError::getPyExceptionType() const
388 {
389 return PyExc_ValueError;
390 }
391
392 // ---------------------------------------------------------
393
394 IndexError::IndexError(const std::string& message)
395 : Exception(message)
396 {}
397
398 PyObject* IndexError::getPyExceptionType() const
399 {
400 return PyExc_IndexError;
401 }
402
403 // ---------------------------------------------------------
404
405 NameError::NameError(const std::string& message)
406 : Exception(message)
407 {}
408
409 PyObject* NameError::getPyExceptionType() const
410 {
411 return PyExc_NameError;
412 }
413
414 // ---------------------------------------------------------
415
416 ImportError::ImportError(const std::string& message)
417 : Exception(message)
418 {}
419
420 PyObject* ImportError::getPyExceptionType() const
421 {
422 return PyExc_ImportError;
423 }
424
425 // ---------------------------------------------------------
426
427 AttributeError::AttributeError(const std::string& message)
428 : Exception(message)
429 {}
430
431 PyObject* AttributeError::getPyExceptionType() const
432 {
433 return PyExc_AttributeError;
434 }
435
436 // ---------------------------------------------------------
437
438 PropertyError::PropertyError(const std::string& message)
439 : AttributeError(message)
440 {}
441
442 PyObject* PropertyError::getPyExceptionType() const
443 {
444 return PyExc_FC_PropertyError;
445 }
446
447 // ---------------------------------------------------------
448
449 RuntimeError::RuntimeError(const std::string& message)
450 : Exception(message)
451 {}
452
453 PyObject* RuntimeError::getPyExceptionType() const
454 {
455 return PyExc_RuntimeError;
456 }
457
458 // ---------------------------------------------------------
459
460 BadGraphError::BadGraphError(const std::string& message)
461 : RuntimeError(message)
462 {}
463
464 PyObject* BadGraphError::getPyExceptionType() const
465 {
466 return PyExc_FC_BadGraphError;
467 }
468
469 // ---------------------------------------------------------
470
471 NotImplementedError::NotImplementedError(const std::string& message)
472 : Exception(message)
473 {}
474
475 PyObject* NotImplementedError::getPyExceptionType() const
476 {
477 return PyExc_NotImplementedError;
478 }
479
480 // ---------------------------------------------------------
481
482 ZeroDivisionError::ZeroDivisionError(const std::string& message)
483 : Exception(message)
484 {}
485
486 PyObject* ZeroDivisionError::getPyExceptionType() const
487 {
488 return PyExc_ZeroDivisionError;
489 }
490
491 // ---------------------------------------------------------
492
493 ReferenceError::ReferenceError(const std::string& message)
494 : Exception(message)
495 {}
496
497 PyObject* ReferenceError::getPyExceptionType() const
498 {
499 return PyExc_ReferenceError;
500 }
501
502 // ---------------------------------------------------------
503
504 ExpressionError::ExpressionError(const std::string& message)
505 : Exception(message)
506 {}
507
508 PyObject* ExpressionError::getPyExceptionType() const
509 {
510 return PyExc_FC_ExpressionError;
511 }
512
513 // ---------------------------------------------------------
514
515 ParserError::ParserError(const std::string& message)
516 : Exception(message)
517 {}
518
519 PyObject* ParserError::getPyExceptionType() const
520 {
521 return PyExc_FC_ParserError;
522 }
523
524 // ---------------------------------------------------------
525
526 UnicodeError::UnicodeError(const std::string& message)
527 : Exception(message)
528 {}
529
530 PyObject* UnicodeError::getPyExceptionType() const
531 {
532 return PyExc_UnicodeError;
533 }
534
535 // ---------------------------------------------------------
536
537 OverflowError::OverflowError(const std::string& message)
538 : Exception(message)
539 {}
540
541 PyObject* OverflowError::getPyExceptionType() const
542 {
543 return PyExc_OverflowError;
544 }
545
546 // ---------------------------------------------------------
547
548 UnderflowError::UnderflowError(const std::string& message)
549 : Exception(message)
550 {}
551
552 PyObject* UnderflowError::getPyExceptionType() const
553 {
554 return PyExc_ArithmeticError;
555 }
556
557 // ---------------------------------------------------------
558
559 UnitsMismatchError::UnitsMismatchError(const std::string& message)
560 : Exception(message)
561 {}
562
563 PyObject* UnitsMismatchError::getPyExceptionType() const
564 {
565 return PyExc_ArithmeticError;
566 }
567
568 // ---------------------------------------------------------
569
570 CADKernelError::CADKernelError(const std::string& message)
571 : Exception(message)
572 {}
573
574 PyObject* CADKernelError::getPyExceptionType() const
575 {
576 return PyExc_FC_CADKernelError;
577 }
578
579 // ---------------------------------------------------------
580
581 RestoreError::RestoreError(const std::string& message)
582 : Exception(message)
583 {}
584
585 PyObject* RestoreError::getPyExceptionType() const
586 {
587 return PyExc_IOError;
588 }
589
590 // ---------------------------------------------------------
591
592 #if defined(__GNUC__) && defined(FC_OS_LINUX)
593 # include <stdexcept>
594 # include <iostream>
595 # include <csignal>
596
597 SignalException::SignalException()
598 {
599 memset(&new_action, 0, sizeof(new_action));
600 new_action.sa_handler = throw_signal;
601 sigemptyset(&new_action.sa_mask);
602 new_action.sa_flags = 0;
603 ok = (sigaction(SIGSEGV, &new_action, &old_action) < 0);
604 # ifdef _DEBUG
605 std::cout << "Set new signal handler" << std::endl;
606 # endif
607 }
608
609 SignalException::~SignalException()
610 {
611 sigaction(SIGSEGV, &old_action, nullptr);
612 # ifdef _DEBUG
613 std::cout << "Restore old signal handler" << std::endl;
614 # endif
615 }
616
617 void SignalException::throw_signal(const int signum)
618 {
619 std::cerr << "SIGSEGV signal raised: " << signum << std::endl;
620 throw std::runtime_error("throw_signal");
621 }
622 #endif