exception.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "exception.h"
  2. #include "exception_p.h"
  3. #include "exceptioncontext_p.h"
  4. using namespace QtJsonSerializer;
  5. Exception::Exception(const QByteArray &what) :
  6. d{new ExceptionPrivate{what}}
  7. {}
  8. const char *Exception::what() const noexcept
  9. {
  10. return d->what.constData();
  11. }
  12. QByteArray Exception::message() const
  13. {
  14. return d->message;
  15. }
  16. Exception::PropertyTrace Exception::propertyTrace() const
  17. {
  18. return d->trace;
  19. }
  20. void Exception::raise() const
  21. {
  22. throw *this;
  23. }
  24. ExceptionBase *Exception::clone() const
  25. {
  26. auto exc = new Exception(QByteArray());
  27. exc->d = d;
  28. return exc;
  29. }
  30. SerializationException::SerializationException(const QByteArray &what) :
  31. Exception{"Failed to serialize with error: " + what}
  32. {}
  33. void SerializationException::raise() const
  34. {
  35. throw *this;
  36. }
  37. ExceptionBase *SerializationException::clone() const
  38. {
  39. auto exc = new SerializationException(QByteArray());
  40. exc->d = d;
  41. return exc;
  42. }
  43. DeserializationException::DeserializationException(const QByteArray &what) :
  44. Exception{"Failed to deserialize with error: " + what}
  45. {}
  46. void DeserializationException::raise() const
  47. {
  48. throw *this;
  49. }
  50. ExceptionBase *DeserializationException::clone() const
  51. {
  52. auto exc = new DeserializationException(QByteArray());
  53. exc->d = d;
  54. return exc;
  55. }
  56. ExceptionPrivate::ExceptionPrivate(QByteArray msg) :
  57. message{std::move(msg)},
  58. trace{ExceptionContext::currentContext()}
  59. {
  60. //construct the whole trace
  61. what = "what: " + message + "\nProperty Trace:";
  62. if(trace.isEmpty())
  63. what += " <root element>";
  64. else {
  65. for(const auto &p : qAsConst(trace))
  66. what += "\n\t" + p.first + " (Type: " + p.second + ")";
  67. }
  68. }