Audacity  3.0.3
AudacityException.cpp
Go to the documentation of this file.
1 /*!********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  @file AudacityException.cpp
6  @brief Implements AudacityException and related
7 
8  Paul Licameli
9 
10 ***********************************************************************/
11 
12 #include "AudacityException.h"
13 
14 #include <wx/atomic.h>
15 
16 #include "BasicUI.h"
17 
19 {
20 }
21 
23  std::exception_ptr pException,
24  std::function<void(AudacityException*)> delayedHandler)
25 {
27  pException = std::move(pException), delayedHandler = std::move(delayedHandler)
28  ] {
29  try {
30  std::rethrow_exception(pException);
31  }
32  catch( AudacityException &e )
33  { delayedHandler( &e ); }
34  } );
35 }
36 
37 wxAtomicInt sOutstandingMessages {};
38 
40  ExceptionType exceptionType_, const TranslatableString& caption_)
41  : caption { caption_ }
42  , exceptionType { exceptionType_ }
43 {
44  if (!caption.empty())
45  wxAtomicInc( sOutstandingMessages );
46  else
47  // invalidate me
48  moved = true;
49 }
50 
51 // The class needs a copy constructor to be throwable
52 // (or will need it, by C++14 rules). But the copy
53 // needs to act like a move constructor. There must be unique responsibility
54 // for each exception thrown to decrease the global count when it is handled.
56 {
57  caption = that.caption;
58  moved = that.moved;
59  helpUrl = that.helpUrl;
61  that.moved = true;
62 }
63 
65 {
66  if (!moved)
67  // If exceptions are used properly, you should never reach this,
68  // because moved should become true earlier in the object's lifetime.
69  wxAtomicDec( sOutstandingMessages );
70 }
71 
73 {
74 }
75 
77 {
78  return message;
79 }
80 
81 // This is meant to be invoked via wxEvtHandler::CallAfter
83 {
84  if (!moved) {
85  // This test prevents accumulation of multiple messages between idle
86  // times of the main even loop. Only the last queued exception
87  // displays its message. We assume that multiple messages have a
88  // common cause such as exhaustion of disk space so that the others
89  // give the user no useful added information.
90 
91  using namespace BasicUI;
92  if ( wxAtomicDec( sOutstandingMessages ) == 0 ) {
94  && ErrorHelpUrl().IsEmpty()) {
95  // We show BadEnvironment and BadUserAction in a similar way
97  ErrorMessage(),
100  .IconStyle(Icon::Error) );
101  }
102  else {
103  using namespace BasicUI;
105  ? ErrorDialogType::ModalErrorReport : ErrorDialogType::ModalError;
106  ShowErrorDialog( {},
108  ErrorMessage(),
109  ErrorHelpUrl(),
110  ErrorDialogOptions{ type } );
111  }
112  }
113 
114  moved = true;
115  }
116 }
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
TranslatableString::empty
bool empty() const
Definition: TranslatableString.h:72
BasicUI::MessageBoxOptions
Definition: BasicUI.h:91
MessageBoxException::~MessageBoxException
~MessageBoxException() override
Definition: AudacityException.cpp:64
SimpleMessageBoxException::message
TranslatableString message
Stored message.
Definition: AudacityException.h:118
SimpleMessageBoxException::ErrorMessage
virtual TranslatableString ErrorMessage() const override
Format the error message for this exception.
Definition: AudacityException.cpp:76
BasicUI::ShowErrorDialog
void ShowErrorDialog(const WindowPlacement &placement, const TranslatableString &dlogTitle, const TranslatableString &message, const ManualPageID &helpPage, const ErrorDialogOptions &options={})
Show an error dialog with a link to the manual for further help.
Definition: BasicUI.h:233
BasicUI::ShowMessageBox
MessageBoxResult ShowMessageBox(const TranslatableString &message, MessageBoxOptions options={})
Show a modal message box with either Ok or Yes and No, and optionally Cancel.
Definition: BasicUI.h:248
ExceptionType
ExceptionType
A type of an exception.
Definition: AudacityException.h:22
MessageBoxException::exceptionType
ExceptionType exceptionType
Exception type.
Definition: AudacityException.h:85
BasicUI::CallAfter
void CallAfter(Action action)
Schedule an action to be done later, and in the main thread.
Definition: BasicUI.cpp:38
AudacityException::EnqueueAction
static void EnqueueAction(std::exception_ptr pException, std::function< void(AudacityException *)> delayedHandler)
Definition: AudacityException.cpp:22
MessageBoxException::ErrorHelpUrl
virtual wxString ErrorHelpUrl() const
Definition: AudacityException.h:81
MessageBoxException::ErrorMessage
virtual TranslatableString ErrorMessage() const =0
Format the error message for this exception.
AudacityException
Base class for exceptions specially processed by the application.
Definition: AudacityException.h:33
AudacityException::~AudacityException
virtual ~AudacityException()=0
Definition: AudacityException.cpp:18
MessageBoxException::DelayedHandlerAction
void DelayedHandlerAction() final
Do not allow subclasses to change behavior, except by overriding ErrorMessage().
Definition: AudacityException.cpp:82
AudacityException.h
Declare abstract class AudacityException, some often-used subclasses, and GuardedCall.
BasicUI::ErrorDialogOptions
Options for variations of error dialogs; the default is for modal dialogs.
Definition: BasicUI.h:49
BasicUI.h
Toolkit-neutral facade for basic user interface services.
SimpleMessageBoxException::~SimpleMessageBoxException
~SimpleMessageBoxException() override
Definition: AudacityException.cpp:72
BasicUI
Definition: Export.h:39
BasicUI::MessageBoxOptions::Caption
MessageBoxOptions && Caption(TranslatableString caption_) &&
Definition: BasicUI.h:98
MessageBoxException
Abstract AudacityException subclass displays a message, specified by further subclass.
Definition: AudacityException.h:62
MessageBoxException::helpUrl
wxString helpUrl
Definition: AudacityException.h:89
ExceptionType::Internal
@ Internal
Indicates internal failure from Audacity.
BasicUI::DefaultCaption
TranslatableString DefaultCaption()
"Message", suitably translated
Definition: BasicUI.cpp:70
sOutstandingMessages
wxAtomicInt sOutstandingMessages
Definition: AudacityException.cpp:37
MessageBoxException::caption
TranslatableString caption
Stored caption.
Definition: AudacityException.h:81
MessageBoxException::moved
bool moved
Whether *this has been the source of a copy.
Definition: AudacityException.h:87
MessageBoxException::MessageBoxException
MessageBoxException(ExceptionType exceptionType, const TranslatableString &caption)
If default-constructed with empty caption, it makes no message box.
Definition: AudacityException.cpp:39