Audacity 3.2.0
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
37wxAtomicInt 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
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}
wxAtomicInt sOutstandingMessages
Declare abstract class AudacityException, some often-used subclasses, and GuardedCall.
ExceptionType
A type of an exception.
@ Internal
Indicates internal failure from Audacity.
Toolkit-neutral facade for basic user interface services.
Base class for exceptions specially processed by the application.
static void EnqueueAction(std::exception_ptr pException, std::function< void(AudacityException *)> delayedHandler)
virtual ~AudacityException()=0
Abstract AudacityException subclass displays a message, specified by further subclass.
bool moved
Whether *this has been the source of a copy.
virtual TranslatableString ErrorMessage() const =0
Format the error message for this exception.
void DelayedHandlerAction() final
Do not allow subclasses to change behavior, except by overriding ErrorMessage().
virtual wxString ErrorHelpUrl() const
MessageBoxException(ExceptionType exceptionType, const TranslatableString &caption)
If default-constructed with empty caption, it makes no message box.
TranslatableString caption
Stored caption.
ExceptionType exceptionType
Exception type.
virtual TranslatableString ErrorMessage() const override
Format the error message for this exception.
TranslatableString message
Stored message.
Holds a msgid for the translation catalog; may also bind format arguments.
TranslatableString DefaultCaption()
"Message", suitably translated
Definition: BasicUI.cpp:253
void CallAfter(Action action)
Schedule an action to be done later, and in the main thread.
Definition: BasicUI.cpp:208
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:264
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:279
Options for variations of error dialogs; the default is for modal dialogs.
Definition: BasicUI.h:52
MessageBoxOptions && Caption(TranslatableString caption_) &&
Definition: BasicUI.h:101