Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
berrySafeRunner.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 BlueBerry Platform
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "berrySafeRunner.h"
19 #include "berryCoreException.h"
20 #include "berryMultiStatus.h"
21 
22 #include "internal/berryIRuntimeConstants.h"
23 #include "internal/berryCTKPluginActivator.h"
24 
25 #include <typeinfo>
26 
27 namespace berry
28 {
29 
31 {
32  poco_assert(code);
33  try
34  {
35  code->Run();
36  }
37  catch (const ctkException& e)
38  {
39  HandleException(code, e);
40  }
41  catch (const std::exception& e)
42  {
43  HandleException(code, e);
44  }
45  catch (...)
46  {
47  HandleException(code);
48  }
49 }
50 
51 void SafeRunner::HandleException(ISafeRunnable::Pointer code)
52 {
53  HandleException(code, ctkException("Unknown exception thrown"));
54 }
55 
56 void SafeRunner::HandleException(ISafeRunnable::Pointer code,
57  const std::exception& e)
58 {
59  HandleException(code, ctkException(e.what()));
60 }
61 
62 void SafeRunner::HandleException(ISafeRunnable::Pointer code,
63  const ctkException& e)
64 {
65  try {
66  dynamic_cast<const OperationCanceledException&>(e);
67  }
68  catch (const std::bad_cast& )
69  {
70  // try to obtain the correct plug-in id for the bundle providing the safe runnable
71  QString pluginId = CTKPluginActivator::getPluginId(static_cast<void*>(code.GetPointer()));
72  if (pluginId.isEmpty())
73  pluginId = IRuntimeConstants::PI_RUNTIME();
74  QString message = QString("Problems occurred when invoking code from plug-in: \"%1\".").arg(pluginId);
75  IStatus::Pointer status;
76  try {
77  const CoreException& coreExc = dynamic_cast<const CoreException&>(e);
78  MultiStatus::Pointer multiStatus(new MultiStatus(pluginId, IRuntimeConstants::PLUGIN_ERROR,
79  message, e, BERRY_STATUS_LOC));
80  multiStatus->Merge(coreExc.GetStatus());
81  status = multiStatus;
82  }
83  catch (const std::bad_cast&)
84  {
85  IStatus::Pointer tmpStatus(new Status(IStatus::ERROR_TYPE, pluginId,
86  IRuntimeConstants::PLUGIN_ERROR, message, e, BERRY_STATUS_LOC));
87  status = tmpStatus;
88  }
89  // Make sure user sees the exception: if the log is empty, log the exceptions on stderr
90 // if (!RuntimeLog.isEmpty())
91 // RuntimeLog.log(status);
92 // else
93  qWarning() << e.printStackTrace();
94  }
95  code->HandleException(e);
96 }
97 
98 }
berry::SmartPointer< Self > Pointer
Definition: berryIStatus.h:58
static void Run(ISafeRunnable::Pointer code)
berry::SmartPointer< Self > Pointer
#define BERRY_STATUS_LOC
Definition: berryStatus.h:257
berry::SmartPointer< Self > Pointer