Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
berrySafeRunnable.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 "berrySafeRunnable.h"
14 
15 #include <typeinfo>
16 
17 #include <berryLog.h>
19 
20 namespace berry
21 {
22 
23 bool SafeRunnable::ignoreErrors = false;
24 ISafeRunnableRunner::Pointer SafeRunnable::runner;
25 
26 class DefaultSafeRunnableRunner: public ISafeRunnableRunner
27 {
28 public:
29 
30  void Run(ISafeRunnable::Pointer code) override
31  {
32  try
33  {
34  code->Run();
35  }
36  catch (const ctkException& e)
37  {
38  HandleException(code, e);
39  }
40  catch (const std::exception& e)
41  {
42  HandleException(code, e);
43  }
44  catch (...)
45  {
46  HandleException(code);
47  }
48  }
49 
50 private:
51 
52  void HandleException(ISafeRunnable::Pointer code)
53  {
54  HandleException(code, ctkException("Unknown exception thrown"));
55  }
56 
57  void HandleException(ISafeRunnable::Pointer code,
58  const std::exception& e)
59  {
60  HandleException(code, ctkException(e.what()));
61  }
62 
63  void HandleException(ISafeRunnable::Pointer code, const ctkException& e)
64  {
65  try
66  {
67  static_cast<void>(dynamic_cast<const OperationCanceledException&> (e));
68  }
69  catch (const std::bad_cast&)
70  {
71  // TODO logging
72  try
73  {
74  // Policy.getLog()
75  // .log(
76  // new Status(IStatus.ERROR, Policy.JFACE,
77  // IStatus.ERROR,
78  // "Exception occurred", e)); //$NON-NLS-1$
79  qDebug() << e.printStackTrace();
80  }
81  catch (...)
82  {
83  //e.printStackTrace();
84  BERRY_ERROR << "Exception occurred" << std::endl;
85  }
86  }
87  code->HandleException(e);
88  }
89 };
90 
91 SmartPointer<ISafeRunnableRunner> SafeRunnable::CreateDefaultRunner()
92 {
93  ISafeRunnableRunner::Pointer runner(new DefaultSafeRunnableRunner());
94  return runner;
95 }
96 
97 SafeRunnable::SafeRunnable(const QString& message) :
98  message(message)
99 {
100 
101 }
102 
103 void SafeRunnable::HandleException(const ctkException& /*e*/)
104 {
105  // Workaround to avoid interactive error dialogs during
106  // automated testing
107  if (ignoreErrors)
108  return;
109 
110  if (message.isEmpty())
111  message = "An error has occurred. See error log for more details.";
112 
113  // TODO status bar
114  // Policy.getStatusHandler().show(
115  // new Status(IStatus.ERROR, Policy.JFACE, message, e),
116  // JFaceResources.getString("SafeRunnable.errorMessage")); //$NON-NLS-1$
117  BERRY_ERROR << message << std::endl;
118 }
119 
121 {
122  return ignoreErrors;
123 }
124 
126 {
127  ignoreErrors = flag;
128 }
129 
131 {
132  if (!runner)
133  {
134  runner = CreateDefaultRunner();
135  }
136  return runner;
137 }
138 
140 {
141  SafeRunnable::runner = runner;
142 }
143 
145 {
146  GetRunner()->Run(runnable);
147 }
148 
149 }
static bool GetIgnoreErrors()
berry::SmartPointer< Self > Pointer
static ISafeRunnableRunner::Pointer GetRunner()
virtual void Run()=0
Implements transparent reference counting.
#define BERRY_ERROR
Definition: berryLog.h:22
static void SetIgnoreErrors(bool flag)
berry::SmartPointer< Self > Pointer
Definition: berryObject.h:82
SafeRunnable(const QString &message="")
void Run(berry::IWorkbenchPartSite::Pointer workbenchPartSite, mitk::DataStorage::Pointer dataStorage)
static void SetRunner(ISafeRunnableRunner::Pointer runner)
void HandleException(const ctkException &e) override