-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathExceptions.h
More file actions
176 lines (135 loc) · 5.33 KB
/
Exceptions.h
File metadata and controls
176 lines (135 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* Exceptions
*
* From: https://github.com/PokemonAutomation/
*
*/
#ifndef PokemonAutomation_Exceptions_H
#define PokemonAutomation_Exceptions_H
#include <utility>
#include "Common/Compiler.h"
#include "Logging/AbstractLogger.h"
namespace PokemonAutomation{
template <typename ExceptionType, class... Args>
[[noreturn]] void throw_and_log(Logger& logger, Args&&... args){
ExceptionType exception(std::forward<Args>(args)...);
exception.log(logger);
throw exception;
}
// Definitions:
// Catch: To catch the exception using a try-catch.
// Consume: To catch the exception and not rethrow it.
// Base class. Don't use this directly. This is for the infra to catch everything.
class Exception{
public:
virtual ~Exception() = default;
virtual void log(Logger& logger) const;
virtual const char* name() const = 0;
virtual std::string message() const;
virtual std::string to_str() const;
};
// Thrown when the user stops the program.
// - This should not be consumed except by the infra.
// - Non-infra are allowed to catch and rethrow this exception.
class ProgramCancelledException : public Exception{
public:
virtual const char* name() const override{ return "ProgramCancelledException"; }
};
// Thrown by BotBase connections when a command is issued while the connection
// is in a state that isn't accepting commands.
// - This should not be consumed except by the infra.
// - Non-infra are allowed to catch and rethrow this exception.
class InvalidConnectionStateException : public Exception{
public:
InvalidConnectionStateException(std::string message) : m_message(std::move(message)) {}
virtual const char* name() const override{ return "InvalidConnectionStateException"; }
virtual std::string message() const override{ return m_message; }
protected:
std::string m_message;
};
// Thrown when a local operation is cancelled.
// This can be caught by local handlers that do async-cancel.
// If this propagates up to the infra, it is considered an error.
class OperationCancelledException : public Exception{
public:
virtual const char* name() const override{ return "OperationCancelledException"; }
};
class ParseException : public Exception{
public:
ParseException() = default;
ParseException(std::string message);
virtual const char* name() const override{ return "ParseException"; }
virtual std::string message() const override{ return m_message; }
protected:
std::string m_message;
};
class FileException : public Exception{
public:
FileException(Logger* logger, const char* location, std::string message, std::string file);
virtual const char* name() const override{ return "FileException"; }
virtual std::string message() const override;
private:
const char* m_location;
std::string m_message;
std::string m_file;
};
class ConnectionException : public Exception{
public:
ConnectionException(Logger* logger, std::string message);
virtual const char* name() const override{ return "ConnectionException"; }
virtual std::string message() const override{ return m_message; }
private:
std::string m_message;
};
class SerialProtocolException : public Exception{
public:
SerialProtocolException(Logger& logger, const char* location, std::string message);
virtual const char* name() const override{ return "SerialProtocolException"; }
virtual std::string message() const override;
private:
// const char* m_location;
std::string m_message;
};
// These are thrown for logic errors. They are always bugs.
class InternalProgramError : public Exception{
public:
// location: use `PA_CURRENT_FUNCTION`
InternalProgramError(Logger* logger, const char* location, std::string message);
virtual const char* name() const override{ return "InternalProgramError"; }
virtual std::string message() const override;
private:
const char* m_location;
std::string m_message;
};
// These are thrown for failed system errors. They are not necessarily bugs.
class InternalSystemError : public Exception{
public:
InternalSystemError(Logger* logger, const char* location, std::string message);
virtual const char* name() const override{ return "InternalSystemError"; }
virtual std::string message() const override;
private:
const char* m_location;
std::string m_message;
};
class UserSetupError : public Exception{
public:
UserSetupError(Logger& logger, std::string message);
virtual const char* name() const override{ return "UserSetupError"; }
virtual std::string message() const override;
private:
std::string m_message;
};
// These are thrown if failed to create an ML model session. They
// are usually due to unable to load ML model files or not enough GPU
// memory.
class MLModelSessionCreationError : public Exception{
public:
// If logger is not nullptr, call logger to write error message in the constructor.
// Otherwise, write error message to std::cerr.
MLModelSessionCreationError(Logger* logger, std::string model_path);
virtual const char* name() const override{ return "MLModelCreationError"; }
virtual std::string message() const override;
private:
std::string m_model_path;
};
}
#endif