Medical Imaging Interaction Toolkit  2018.4.99-c4b6bb11
Medical Imaging Interaction Toolkit
berrySafeRunner.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "berrySafeRunner.h"
15 #include "berryCoreException.h"
16 #include "berryMultiStatus.h"
17 
18 #include "internal/berryIRuntimeConstants.h"
19 #include "internal/berryCTKPluginActivator.h"
20 
21 #include <typeinfo>
22 
23 namespace berry
24 {
25 
27 {
28  poco_assert(code);
29  try
30  {
31  code->Run();
32  }
33  catch (const ctkException& e)
34  {
35  HandleException(code, e);
36  }
37  catch (const std::exception& e)
38  {
39  HandleException(code, e);
40  }
41  catch (...)
42  {
43  HandleException(code);
44  }
45 }
46 
47 void SafeRunner::HandleException(ISafeRunnable::Pointer code)
48 {
49  HandleException(code, ctkException("Unknown exception thrown"));
50 }
51 
52 void SafeRunner::HandleException(ISafeRunnable::Pointer code,
53  const std::exception& e)
54 {
55  HandleException(code, ctkException(e.what()));
56 }
57 
58 void SafeRunner::HandleException(ISafeRunnable::Pointer code,
59  const ctkException& e)
60 {
61  try {
62  Q_UNUSED(dynamic_cast<const OperationCanceledException&>(e))
63  }
64  catch (const std::bad_cast& )
65  {
66  // try to obtain the correct plug-in id for the bundle providing the safe runnable
67  QString pluginId = CTKPluginActivator::getPluginId(static_cast<void*>(code.GetPointer()));
68  if (pluginId.isEmpty())
69  pluginId = IRuntimeConstants::PI_RUNTIME();
70  QString message = QString("Problems occurred when invoking code from plug-in: \"%1\".").arg(pluginId);
71  IStatus::Pointer status;
72  try {
73  const CoreException& coreExc = dynamic_cast<const CoreException&>(e);
74  MultiStatus::Pointer multiStatus(new MultiStatus(pluginId, IRuntimeConstants::PLUGIN_ERROR,
75  message, e, BERRY_STATUS_LOC));
76  multiStatus->Merge(coreExc.GetStatus());
77  status = multiStatus;
78  }
79  catch (const std::bad_cast&)
80  {
81  IStatus::Pointer tmpStatus(new Status(IStatus::ERROR_TYPE, pluginId,
82  IRuntimeConstants::PLUGIN_ERROR, message, e, BERRY_STATUS_LOC));
83  status = tmpStatus;
84  }
85  // Make sure user sees the exception: if the log is empty, log the exceptions on stderr
86 // if (!RuntimeLog.isEmpty())
87 // RuntimeLog.log(status);
88 // else
89  qWarning() << e.printStackTrace();
90  }
91  code->HandleException(e);
92 }
93 
94 }
static void Run(ISafeRunnable::Pointer code)
SmartPointer< IStatus > GetStatus() const
#define BERRY_STATUS_LOC
Definition: berryStatus.h:253
ObjectType * GetPointer() const