Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CodingAgentContext/AutomationProgramPatterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ while(true){
env.update_stats();
send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH);
throw;
}catch(ScreenshotException& e){ // an exception indicating an error somewhere in the loop
}catch(OperationFailedException& e){ // an exception indicating an error somewhere in the loop
if(...){ // if we can recover from it
// execute recover logic (usually is restarting the game)
continue;
Expand Down
7 changes: 7 additions & 0 deletions Common/Cpp/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
namespace PokemonAutomation{



enum class ErrorReport{
NO_ERROR_REPORT,
SEND_ERROR_REPORT,
};


template <typename ExceptionType, class... Args>
[[noreturn]] void throw_and_log(Logger& logger, Args&&... args){
ExceptionType exception(std::forward<Args>(args)...);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* Fatal Program Exception
*
* From: https://github.com/PokemonAutomation/
*
*/


#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "CommonFramework/Tools/VideoStream.h"
#include "CommonFramework/Tools/ProgramEnvironment.h"
#include "CommonFramework/Notifications/EventNotificationOption.h"
#include "CommonFramework/Notifications/ProgramNotifications.h"
#include "FatalProgramException.h"

//#include <iostream>
//using std::cout;
//using std::endl;

namespace PokemonAutomation{


FatalProgramException::FatalProgramException(
ErrorReport error_report,
std::string message
)
: m_error_report_mode(error_report)
, m_message(message)
{}

FatalProgramException::FatalProgramException(
ErrorReport error_report,
std::string message,
VideoStream& stream
)
: m_error_report_mode(error_report)
, m_message(message)
, m_stream(&stream)
, m_screenshot(stream.video().snapshot().frame)
{}

// Construct exception with message with screenshot and (optionally) console information.
// Use the provided screenshot instead of taking one with the console.
// Store the console information (if provided) for stream history if requested later.
FatalProgramException::FatalProgramException(
ErrorReport error_report,
std::string message,
VideoStream* stream,
ImageRGB32 screenshot
)
: m_error_report_mode(error_report)
, m_message(message)
, m_stream(stream)
, m_screenshot(std::make_shared<ImageRGB32>(std::move(screenshot)))
{}

FatalProgramException::FatalProgramException(
ErrorReport error_report,
std::string message,
VideoStream* stream,
std::shared_ptr<const ImageRGB32> screenshot
)
: m_error_report_mode(error_report)
, m_message(message)
, m_stream(stream)
, m_screenshot(std::move(screenshot))
{}


void FatalProgramException::send_fatal_error_notif_and_telemetry_report(
ProgramEnvironment& env,
EventNotificationOption& notif_settings
){
send_program_fatal_error_notification_and_telemetry_report(
env, &env.logger(), env.program_info(),
notif_settings,
error_report_mode(),
message(),
name(),
*m_screenshot,
&m_stream->history()
);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,72 @@
#ifndef PokemonAutomation_FatalProgramException_H
#define PokemonAutomation_FatalProgramException_H

#include "ScreenshotException.h"
#include <memory>
#include "CommonFramework/ImageTypes/ImageRGB32.h"
#include "Common/Cpp/Exceptions.h"

namespace PokemonAutomation{

class VideoStream;
class ProgramEnvironment;
class EventNotificationOption;
class ImageViewRGB32;
class ImageRGB32;

// A generic exception that should not be caught outside of infra.
class FatalProgramException : public ScreenshotException{
class FatalProgramException : public Exception{
public:
using ScreenshotException::ScreenshotException;
FatalProgramException(ScreenshotException&& e)
: ScreenshotException(
e.m_send_error_report,
std::move(e.m_message),
e.m_stream,
std::move(e.m_screenshot)
)
{}

explicit FatalProgramException(
ErrorReport error_report,
std::string message
);

explicit FatalProgramException(
ErrorReport error_report,
std::string message,
VideoStream& stream
);

// Construct exception with message with screenshot and (optionally) console information.
// Use the provided screenshot instead of taking one with the console.
// Store the console information (if provided) for stream history if requested later.
explicit FatalProgramException(
ErrorReport error_report,
std::string message,
VideoStream* stream,
ImageRGB32 screenshot
);

explicit FatalProgramException(
ErrorReport error_report,
std::string message,
VideoStream* stream,
std::shared_ptr<const ImageRGB32> screenshot
);

ErrorReport error_report_mode() const { return m_error_report_mode; }
virtual const char* name() const override{ return "FatalProgramException"; }
ImageViewRGB32 screenshot_view() const {
if (m_screenshot){
return *m_screenshot;
}else{
return ImageViewRGB32();
}
}
std::shared_ptr<const ImageRGB32> screenshot() const {return m_screenshot;}
VideoStream* video_stream() const{return m_stream;}

void send_fatal_error_notif_and_telemetry_report(
ProgramEnvironment& env,
EventNotificationOption& notif_settings
);

private:
ErrorReport m_error_report_mode;
std::string m_message;
VideoStream* m_stream = nullptr;
std::shared_ptr<const ImageRGB32> m_screenshot;
};


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Operation Failed Exception
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "CommonFramework/Tools/ProgramEnvironment.h"
#include "CommonFramework/Notifications/EventNotificationOption.h"
#include "CommonFramework/Notifications/ProgramNotifications.h"
#include "OperationFailedException.h"

//#include <iostream>
//using std::cout;
//using std::endl;

namespace PokemonAutomation{


OperationFailedException::OperationFailedException(
ErrorReport error_report_mode,
std::string message
)
: m_error_report_mode(error_report_mode)
, m_message(std::move(message))
{}


void OperationFailedException::send_fatal_error_notif_and_telemetry_report(
ProgramEnvironment& env,
EventNotificationOption& notif_settings
){
send_program_fatal_error_notification_and_telemetry_report(
env, &env.logger(), env.program_info(),
notif_settings,
error_report_mode(),
message(),
name()
);
}


void OperationFailedException::send_recoverable_error_notif_and_telemetry_report(
ProgramEnvironment& env,
EventNotificationOption& notif_settings
){
send_program_recoverable_error_notification_and_telemetry_report(
env, &env.logger(), env.program_info(),
notif_settings,
error_report_mode(),
message(),
name()
);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,41 @@
#ifndef PokemonAutomation_OperationFailedException_H
#define PokemonAutomation_OperationFailedException_H

#include <memory>
#include "CommonFramework/Tools/VideoStream.h"
#include "ScreenshotException.h"
#include "Common/Cpp/Exceptions.h"

namespace PokemonAutomation{

class ProgramEnvironment;
class EventNotificationOption;

// Thrown by subroutines if they fail for an in-game reason.
// These include recoverable errors which can be consumed by the program.
class OperationFailedException : public ScreenshotException{
class OperationFailedException : public Exception{
public:
using ScreenshotException::ScreenshotException;

// This is the most common use case. Throw and log exception.
// Include console information for screenshot and stream history.
[[noreturn]] static void fire(
ErrorReport error_report,
std::string message,
VideoStream& stream
){
throw_and_log<OperationFailedException>(
stream.logger(),
error_report,
std::move(message),
stream
);
}
[[noreturn]] static void fire(
ErrorReport error_report,
std::string message,
VideoStream& stream,
std::shared_ptr<const ImageRGB32> screenshot
){
throw_and_log<OperationFailedException>(
stream.logger(),
error_report,
std::move(message),
&stream,
std::move(screenshot)
);
}
OperationFailedException(
ErrorReport error_report_mode,
std::string message
);

ErrorReport error_report_mode() const { return m_error_report_mode; };

virtual const char* name() const override{ return "OperationFailedException"; }
virtual std::string message() const override{ return m_message; }

virtual void send_recoverable_error_notif_and_telemetry_report(
ProgramEnvironment& env,
EventNotificationOption& notif_settings
);

virtual void send_fatal_error_notif_and_telemetry_report(
ProgramEnvironment& env,
EventNotificationOption& notif_settings
);


private:
ErrorReport m_error_report_mode;
std::string m_message;
};


Expand Down
Loading
Loading