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