Skip to content

Commit 9dcdb0c

Browse files
committed
Refactor exceptions.
1 parent 063dcf8 commit 9dcdb0c

File tree

133 files changed

+1172
-2062
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+1172
-2062
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,17 +310,12 @@ file(GLOB MAIN_SOURCES
310310
Source/CommonFramework/ErrorReports/ProgramDumper.cpp
311311
Source/CommonFramework/ErrorReports/ProgramDumper.h
312312
Source/CommonFramework/ErrorReports/ProgramDumper_Windows.tpp
313-
Source/CommonFramework/Exceptions/FatalProgramException.cpp
314313
Source/CommonFramework/Exceptions/FatalProgramException.h
315-
Source/CommonFramework/Exceptions/OliveActionFailedException.cpp
316-
Source/CommonFramework/Exceptions/OliveActionFailedException.h
317-
Source/CommonFramework/Exceptions/OperationFailedException.cpp
318314
Source/CommonFramework/Exceptions/OperationFailedException.h
319315
Source/CommonFramework/Exceptions/ProgramFinishedException.cpp
320316
Source/CommonFramework/Exceptions/ProgramFinishedException.h
321317
Source/CommonFramework/Exceptions/ScreenshotException.cpp
322318
Source/CommonFramework/Exceptions/ScreenshotException.h
323-
Source/CommonFramework/Exceptions/UnexpectedBattleException.cpp
324319
Source/CommonFramework/Exceptions/UnexpectedBattleException.h
325320
Source/CommonFramework/GlobalServices.cpp
326321
Source/CommonFramework/GlobalServices.h
@@ -1508,6 +1503,7 @@ file(GLOB MAIN_SOURCES
15081503
Source/PokemonSV/Programs/AutoStory/PokemonSV_MenuOption.h
15091504
Source/PokemonSV/Programs/AutoStory/PokemonSV_MenuOptionDatabase.cpp
15101505
Source/PokemonSV/Programs/AutoStory/PokemonSV_MenuOptionDatabase.h
1506+
Source/PokemonSV/Programs/AutoStory/PokemonSV_OliveActionFailedException.h
15111507
Source/PokemonSV/Programs/Battles/PokemonSV_BasicCatcher.cpp
15121508
Source/PokemonSV/Programs/Battles/PokemonSV_BasicCatcher.h
15131509
Source/PokemonSV/Programs/Battles/PokemonSV_Battles.cpp

SerialPrograms/SerialPrograms.pro

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,8 @@ SOURCES += \
179179
Source/CommonFramework/Environment/SystemSleep.cpp \
180180
Source/CommonFramework/ErrorReports/ErrorReports.cpp \
181181
Source/CommonFramework/ErrorReports/ProgramDumper.cpp \
182-
Source/CommonFramework/Exceptions/FatalProgramException.cpp \
183-
Source/CommonFramework/Exceptions/OliveActionFailedException.cpp \
184-
Source/CommonFramework/Exceptions/OperationFailedException.cpp \
185182
Source/CommonFramework/Exceptions/ProgramFinishedException.cpp \
186183
Source/CommonFramework/Exceptions/ScreenshotException.cpp \
187-
Source/CommonFramework/Exceptions/UnexpectedBattleException.cpp \
188184
Source/CommonFramework/GlobalServices.cpp \
189185
Source/CommonFramework/GlobalSettingsPanel.cpp \
190186
Source/CommonFramework/Globals.cpp \
@@ -730,7 +726,7 @@ SOURCES += \
730726
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_06.cpp \
731727
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_07.cpp \
732728
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_08.cpp \
733-
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_09.cpp \
729+
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_09.cpp \
734730
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_10.cpp \
735731
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_11.cpp \
736732
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_12.cpp \
@@ -1263,7 +1259,6 @@ HEADERS += \
12631259
Source/CommonFramework/ErrorReports/ProgramDumper.h \
12641260
Source/CommonFramework/ErrorReports/ProgramDumper_Windows.tpp \
12651261
Source/CommonFramework/Exceptions/FatalProgramException.h \
1266-
Source/CommonFramework/Exceptions/OliveActionFailedException.h \
12671262
Source/CommonFramework/Exceptions/OperationFailedException.h \
12681263
Source/CommonFramework/Exceptions/ProgramFinishedException.h \
12691264
Source/CommonFramework/Exceptions/ScreenshotException.h \
@@ -1471,7 +1466,6 @@ HEADERS += \
14711466
Source/Kernels/BinaryMatrix/Kernels_SparseBinaryMatrixCore.h \
14721467
Source/Kernels/BinaryMatrix/Kernels_SparseBinaryMatrixCore.tpp \
14731468
Source/Kernels/ImageFilters/Kernels_ImageFilter_Basic.h \
1474-
Source/Kernels/ImageFilters/Kernels_ImageFilter_Green_Default.h \
14751469
Source/Kernels/ImageScaleBrightness/Kernels_ImageScaleBrightness.h \
14761470
Source/Kernels/ImageStats/Kernels_ImagePixelSumSqr.h \
14771471
Source/Kernels/ImageStats/Kernels_ImagePixelSumSqrDev.h \
@@ -1807,7 +1801,7 @@ HEADERS += \
18071801
Source/PokemonSV/Inference/PokemonSV_ClothingTopDetector.h \
18081802
Source/PokemonSV/Inference/PokemonSV_ESPEmotionDetector.h \
18091803
Source/PokemonSV/Inference/PokemonSV_MainMenuDetector.h \
1810-
Source/PokemonSV/Inference/PokemonSV_MenuOptionReader.h \
1804+
Source/PokemonSV/Inference/PokemonSV_MenuOptionReader.h \
18111805
Source/PokemonSV/Inference/PokemonSV_MoneyReader.h \
18121806
Source/PokemonSV/Inference/PokemonSV_PokemonMovesReader.h \
18131807
Source/PokemonSV/Inference/PokemonSV_PokePortalDetector.h \
@@ -1855,7 +1849,7 @@ HEADERS += \
18551849
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_06.h \
18561850
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_07.h \
18571851
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_08.h \
1858-
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_09.h \
1852+
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_09.h \
18591853
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_10.h \
18601854
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_11.h \
18611855
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_12.h \
@@ -1875,6 +1869,7 @@ HEADERS += \
18751869
Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h \
18761870
Source/PokemonSV/Programs/AutoStory/PokemonSV_MenuOption.h \
18771871
Source/PokemonSV/Programs/AutoStory/PokemonSV_MenuOptionDatabase.h \
1872+
Source/PokemonSV/Programs/AutoStory/PokemonSV_OliveActionFailedException.h \
18781873
Source/PokemonSV/Programs/Battles/PokemonSV_BasicCatcher.h \
18791874
Source/PokemonSV/Programs/Battles/PokemonSV_Battles.h \
18801875
Source/PokemonSV/Programs/Battles/PokemonSV_SinglesBattler.h \

SerialPrograms/Source/CommonFramework/Exceptions/FatalProgramException.cpp

Lines changed: 0 additions & 25 deletions
This file was deleted.

SerialPrograms/Source/CommonFramework/Exceptions/FatalProgramException.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ namespace PokemonAutomation{
1616
class FatalProgramException : public ScreenshotException{
1717
public:
1818
using ScreenshotException::ScreenshotException;
19-
FatalProgramException(ScreenshotException&& e);
19+
FatalProgramException(ScreenshotException&& e)
20+
: ScreenshotException(
21+
e.m_send_error_report,
22+
std::move(e.m_message),
23+
e.m_console,
24+
std::move(e.m_screenshot)
25+
)
26+
{}
2027

2128
virtual const char* name() const override{ return "FatalProgramException"; }
2229
};

SerialPrograms/Source/CommonFramework/Exceptions/OliveActionFailedException.cpp

Lines changed: 0 additions & 34 deletions
This file was deleted.

SerialPrograms/Source/CommonFramework/Exceptions/OperationFailedException.cpp

Lines changed: 0 additions & 40 deletions
This file was deleted.

SerialPrograms/Source/CommonFramework/Exceptions/OperationFailedException.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,37 @@
77
#ifndef PokemonAutomation_OperationFailedException_H
88
#define PokemonAutomation_OperationFailedException_H
99

10-
#include <memory>
10+
#include "CommonFramework/Tools/ConsoleHandle.h"
1111
#include "ScreenshotException.h"
1212

1313
namespace PokemonAutomation{
1414

15-
class FatalProgramException;
16-
1715

1816
// Thrown by subroutines if they fail for an in-game reason.
1917
// These include recoverable errors which can be consumed by the program.
2018
class OperationFailedException : public ScreenshotException{
2119
public:
2220
using ScreenshotException::ScreenshotException;
23-
explicit OperationFailedException(ErrorReport error_report, Logger& logger, std::string message);
24-
explicit OperationFailedException(ErrorReport error_report, Logger& logger, std::string message, std::shared_ptr<const ImageRGB32> screenshot);
25-
explicit OperationFailedException(ErrorReport error_report, ConsoleHandle& console, std::string message, bool take_screenshot);
2621

27-
virtual const char* name() const override{ return "OperationFailedException"; }
22+
// This is the most common use case. Throw and log exception.
23+
// Include console information for screenshot and stream history.
24+
[[noreturn]] static void fire(
25+
ConsoleHandle& console,
26+
ErrorReport error_report,
27+
std::string message
28+
){
29+
throw_and_log<OperationFailedException>(console, error_report, std::move(message), console);
30+
}
31+
[[noreturn]] static void fire(
32+
ConsoleHandle& console,
33+
ErrorReport error_report,
34+
std::string message,
35+
std::shared_ptr<const ImageRGB32> screenshot
36+
){
37+
throw_and_log<OperationFailedException>(console, error_report, std::move(message), &console, std::move(screenshot));
38+
}
2839

40+
virtual const char* name() const override{ return "OperationFailedException"; }
2941
};
3042

3143

SerialPrograms/Source/CommonFramework/Exceptions/ProgramFinishedException.cpp

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
*/
66

7+
#include "CommonFramework/ImageTypes/ImageRGB32.h"
78
#include "CommonFramework/Notifications/ProgramNotifications.h"
89
#include "CommonFramework/Tools/ProgramEnvironment.h"
910
#include "CommonFramework/Tools/ConsoleHandle.h"
@@ -13,41 +14,36 @@ namespace PokemonAutomation{
1314

1415

1516
ProgramFinishedException::ProgramFinishedException(){}
16-
ProgramFinishedException::ProgramFinishedException(Logger& logger, std::string message)
17+
ProgramFinishedException::ProgramFinishedException(std::string message)
1718
: ScreenshotException(ErrorReport::NO_ERROR_REPORT, std::move(message))
18-
{
19-
logger.log(std::string(ProgramFinishedException::name()) + ": " + m_message, COLOR_BLUE);
19+
{}
20+
21+
22+
ProgramFinishedException::ProgramFinishedException(
23+
std::string message,
24+
ConsoleHandle& console
25+
)
26+
: ScreenshotException(ErrorReport::NO_ERROR_REPORT, std::move(message), console)
27+
{}
28+
ProgramFinishedException::ProgramFinishedException(
29+
ErrorReport error_report,
30+
std::string message,
31+
ConsoleHandle* console,
32+
ImageRGB32 screenshot
33+
)
34+
: ScreenshotException(ErrorReport::NO_ERROR_REPORT, std::move(message), console, std::move(screenshot))
35+
{}
36+
ProgramFinishedException::ProgramFinishedException(
37+
std::string message,
38+
ConsoleHandle* console,
39+
std::shared_ptr<const ImageRGB32> screenshot
40+
)
41+
: ScreenshotException(ErrorReport::NO_ERROR_REPORT, std::move(message), console, std::move(screenshot))
42+
{}
43+
44+
void ProgramFinishedException::log(Logger& logger) const{
45+
logger.log(std::string(name()) + ": " + message(), COLOR_BLUE);
2046
}
21-
ProgramFinishedException::ProgramFinishedException(Logger& logger, std::string message, std::shared_ptr<const ImageRGB32> screenshot)
22-
: ScreenshotException(ErrorReport::NO_ERROR_REPORT, std::move(message), std::move(screenshot))
23-
{
24-
logger.log(std::string(ProgramFinishedException::name()) + " (with screenshot): " + m_message, COLOR_BLUE);
25-
}
26-
ProgramFinishedException::ProgramFinishedException(ConsoleHandle& console, std::string message, bool take_screenshot)
27-
: ScreenshotException(ErrorReport::NO_ERROR_REPORT, console, std::move(message), take_screenshot)
28-
{
29-
if (take_screenshot){
30-
console.log(std::string(ProgramFinishedException::name()) + " (take screenshot): " + m_message, COLOR_BLUE);
31-
}else{
32-
console.log(std::string(ProgramFinishedException::name()) + " (no screenshot): " + m_message, COLOR_BLUE);
33-
}
34-
}
35-
36-
37-
void ProgramFinishedException::send_notification(ProgramEnvironment& env, EventNotificationOption& notification) const{
38-
std::vector<std::pair<std::string, std::string>> embeds;
39-
if (!m_message.empty()){
40-
embeds.emplace_back(std::pair<std::string, std::string>("Message:", m_message));
41-
}
42-
send_program_notification(
43-
env, notification,
44-
COLOR_GREEN,
45-
"Program Finished",
46-
std::move(embeds), "",
47-
screenshot()
48-
);
49-
}
50-
5147

5248

5349

SerialPrograms/Source/CommonFramework/Exceptions/ProgramFinishedException.h

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,34 @@ class ConsoleHandle;
2727
class ProgramFinishedException : public ScreenshotException{
2828
public:
2929
ProgramFinishedException();
30-
explicit ProgramFinishedException(Logger& logger, std::string message);
31-
explicit ProgramFinishedException(Logger& logger, std::string message, std::shared_ptr<const ImageRGB32> screenshot);
32-
explicit ProgramFinishedException(ConsoleHandle& console, std::string message, bool take_screenshot);
30+
explicit ProgramFinishedException(std::string message);
31+
32+
// Construct exception with message and console information.
33+
// This will take a screenshot and store the console if the stream history if requested later.
34+
explicit ProgramFinishedException(
35+
std::string message,
36+
ConsoleHandle& console
37+
);
38+
39+
// Construct exception with message with screenshot and (optionally) console information.
40+
// Use the provided screenshot instead of taking one with the console.
41+
// Store the console information (if provided) for stream history if requested later.
42+
explicit ProgramFinishedException(
43+
ErrorReport error_report,
44+
std::string message,
45+
ConsoleHandle* console,
46+
ImageRGB32 screenshot
47+
);
48+
explicit ProgramFinishedException(
49+
std::string message,
50+
ConsoleHandle* console,
51+
std::shared_ptr<const ImageRGB32> screenshot
52+
);
53+
3354

3455
public:
56+
virtual void log(Logger& logger) const override;
3557
virtual const char* name() const override{ return "ProgramFinishedException"; }
36-
virtual std::string message() const override{ return m_message; }
37-
38-
virtual void send_notification(ProgramEnvironment& env, EventNotificationOption& notification) const override;
3958
};
4059

4160

0 commit comments

Comments
 (0)