Преглед на файлове

Improved error and exception handling.

Sascha Zelzer преди 11 години
родител
ревизия
7c964dbf0e

+ 4 - 4
Libs/XNAT/Core/ctkXnatAPI.cpp

@@ -65,7 +65,7 @@ void ctkXnatAPI::parseResponse(qRestResult* restResult, const QByteArray& respon
     {
     // Some operations return an XML description of an object.
     // E.g. GET query for a specific subject.
-    restResult->setError(QString("Bad data: ") + response);
+    restResult->setError(qRestAPI::ResponseParseError, QString("Bad data: ") + response);
     }
   else if (response.startsWith("<?xml "))
     {
@@ -89,14 +89,14 @@ void ctkXnatAPI::parseResponse(qRestResult* restResult, const QByteArray& respon
     }
   else
     {
-    restResult->setError(QString("Bad data: ") + response);
+    restResult->setError(qRestAPI::ResponseParseError, QString("Bad data: ") + response);
     }
 
   restResult->setResult(result);
 }
 
 // --------------------------------------------------------------------------
-QList<QVariantMap> ctkXnatAPI::parseXmlResponse(qRestResult* restResult, const QByteArray& response)
+QList<QVariantMap> ctkXnatAPI::parseXmlResponse(qRestResult* /*restResult*/, const QByteArray& /*response*/)
 {
   QList<QVariantMap> result;
   return result;
@@ -117,7 +117,7 @@ QList<QVariantMap> ctkXnatAPI::parseJsonResponse(qRestResult* restResult, const
     {
     if (!data.toString().isEmpty())
       {
-      restResult->setError(QString("Bad data: ") + data.toString());
+      restResult->setError(qRestAPI::ResponseParseError, QString("Bad data: ") + data.toString());
       }
     }
   if (data.isArray())

+ 24 - 2
Libs/XNAT/Core/ctkXnatConnection.cpp

@@ -54,8 +54,30 @@ public:
   ctkXnatAPI::RawHeaders rawHeaders;
 
   ctkXnatServer* server;
+
+  void throwXnatException(const QString& msg);
 };
 
+void ctkXnatConnectionPrivate::throwXnatException(const QString& msg)
+{
+  QString errorMsg = msg.trimmed();
+  if (!errorMsg.isEmpty())
+  {
+    errorMsg.append(' ');
+  }
+  errorMsg.append(xnat->errorString());
+
+  switch (xnat->error())
+  {
+  case qRestAPI::TimeoutError:
+    throw ctkXnatTimeoutException(errorMsg);
+  case qRestAPI::ResponseParseError:
+    throw ctkXnatResponseParseError(errorMsg);
+  default:
+    throw ctkRuntimeException(errorMsg);
+  }
+}
+
 // ctkXnatConnection class
 
 ctkXnatConnection::ctkXnatConnection()
@@ -415,7 +437,7 @@ void ctkXnatConnection::save(ctkXnatObject* object)
 
   if (!result || !result->error().isNull())
   {
-    throw ctkXnatException("Error occurred while creating the data.");
+    d->throwXnatException("Error occurred while creating the data.");
   }
 
   const QList<QVariantMap>& maps = result->results();
@@ -438,7 +460,7 @@ void ctkXnatConnection::remove(ctkXnatObject* object)
 
   if (!success)
   {
-    throw ctkXnatException("Error occurred while removing the data.");
+    d->throwXnatException("Error occurred while removing the data.");
   }
 }
 

+ 1 - 2
Libs/XNAT/Core/ctkXnatConnectionFactory.cpp

@@ -22,7 +22,6 @@
 #include "ctkXnatConnectionFactory.h"
 
 #include "ctkXnatConnection.h"
-#include "ctkXnatException.h"
 #include "ctkXnatObject.h"
 #include "ctkXnatServer.h"
 
@@ -40,7 +39,7 @@ ctkXnatConnection* ctkXnatConnectionFactory::makeConnection(const QString& url,
   {
     testConnection(connection);
   }
-  catch (ctkXnatException& e)
+  catch (...)
   {
     delete connection;
     throw;

+ 3 - 9
Libs/XNAT/Core/ctkXnatException.cpp

@@ -21,12 +21,6 @@
 
 #include "ctkXnatException.h"
 
-ctkXnatException::ctkXnatException(const std::string& message)
-: message(message.c_str())
-{
-}
-
-const char* ctkXnatException::what() const throw()
-{
-  return message;
-}
+CTK_IMPLEMENT_EXCEPTION(ctkXnatTimeoutException, ctkRuntimeException, "ctkXnatTimeoutException")
+CTK_IMPLEMENT_EXCEPTION(ctkXnatSessionExpiredException, ctkRuntimeException, "ctkXnatSessionExpiredException")
+CTK_IMPLEMENT_EXCEPTION(ctkXnatResponseParseError, ctkRuntimeException, "ctkXnatResponseParseError")

+ 4 - 12
Libs/XNAT/Core/ctkXnatException.h

@@ -24,18 +24,10 @@
 
 #include "ctkXNATCoreExport.h"
 
-#include <exception>
-#include <string>
+#include "ctkException.h"
 
-class CTK_XNAT_CORE_EXPORT ctkXnatException : public std::exception
-{
-public:
-  ctkXnatException(const std::string& message);
-
-  virtual const char* what() const throw();
-
-private:
-  const char* message;
-};
+CTK_DECLARE_EXCEPTION(CTK_XNAT_CORE_EXPORT, ctkXnatTimeoutException, ctkRuntimeException)
+CTK_DECLARE_EXCEPTION(CTK_XNAT_CORE_EXPORT, ctkXnatSessionExpiredException, ctkRuntimeException)
+CTK_DECLARE_EXCEPTION(CTK_XNAT_CORE_EXPORT, ctkXnatResponseParseError, ctkRuntimeException)
 
 #endif

+ 1 - 0
Libs/XNAT/Core/target_libraries.cmake

@@ -5,6 +5,7 @@
 #
 
 set(target_libraries
+  CTKCore
   qRestAPI_LIBRARIES
   QT_LIBRARIES
   QtScript

+ 2 - 2
Libs/XNAT/Widgets/ctkXnatLoginDialog.cpp

@@ -29,7 +29,7 @@
 
 #include <ctkXnatConnection.h>
 #include <ctkXnatConnectionFactory.h>
-#include <ctkXnatException.h>
+#include <ctkException.h>
 #include "ctkXnatLoginProfile.h"
 #include "ctkXnatSettings.h"
 
@@ -193,7 +193,7 @@ void ctkXnatLoginDialog::accept()
                                         password.toAscii().constData());
     d->Connection->setProfileName(ui->edtProfileName->text());
     }
-  catch (ctkXnatException& e)
+  catch (const ctkException& e)
     {
     QMessageBox::warning(this, tr("Invalid Login Error"), tr(e.what()));
     ui->edtServerUri->selectAll();

+ 0 - 1
Libs/XNAT/Widgets/ctkXnatTreeModel.cpp

@@ -21,7 +21,6 @@
 
 #include "ctkXnatTreeModel.h"
 
-#include "ctkXnatException.h"
 #include "ctkXnatObject.h"
 #include "ctkXnatSubject.h"