Skip to content

Commit e376d14

Browse files
authored
Merge pull request #302 from crymp-net/feature/crymp-error-class
Replace std::runtime_error and std::system_error with CryMP_Error
2 parents 4a0e787 + 0c0885b commit e376d14

File tree

5 files changed

+70
-57
lines changed

5 files changed

+70
-57
lines changed

Code/CryMP/Client/MapDownloader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ bool MapDownloader::CheckMapVersion(const std::string_view & mapName, const std:
165165

166166
return file.IsOpen() && file.Read() == mapVersion;
167167
}
168-
catch (const std::runtime_error& error)
168+
catch (const CryMP_Error& error)
169169
{
170170
CryLogAlways("$4[CryMP] [MapDownloader] Failed to read map version: %s", error.what());
171171
return false;
@@ -183,7 +183,7 @@ void MapDownloader::StoreMapVersion(const std::string_view & mapName, const std:
183183
file.Resize(0);
184184
file.Write(mapVersion);
185185
}
186-
catch (const std::runtime_error& error)
186+
catch (const CryMP_Error& error)
187187
{
188188
CryLogAlways("$4[CryMP] [MapDownloader] Failed to store map version: %s", error.what());
189189
}

Code/Launcher/Main.cpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
#include <stdexcept>
2-
#include <string>
1+
#ifdef CRYMP_CONSOLE_APP
2+
#include <cstdio>
3+
#endif
34

5+
#include "Library/StringTools.h" // CryMP_Error
46
#include "Library/WinAPI.h"
57

68
#include "Launcher.h"
@@ -18,19 +20,6 @@ extern "C" __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
1820

1921
////////////////////////////////////////////////////////////////////////////////
2022

21-
static std::string RuntimeErrorToString(const std::runtime_error& error)
22-
{
23-
std::string message = error.what();
24-
std::string::size_type pos = 0;
25-
26-
while ((pos = message.find(": ", pos)) != std::string::npos)
27-
{
28-
message.replace(pos, 2, "\n");
29-
}
30-
31-
return message;
32-
}
33-
3423
#ifdef CRYMP_CONSOLE_APP
3524
int main()
3625
#else
@@ -44,14 +33,13 @@ int __stdcall WinMain(void*, void*, char*, int)
4433
{
4534
launcher.Run();
4635
}
47-
catch (const std::runtime_error& error)
48-
{
49-
WinAPI::ErrorBox(RuntimeErrorToString(error).c_str());
50-
return 1;
51-
}
52-
catch (const std::exception& ex)
36+
catch (const CryMP_Error& error)
5337
{
54-
WinAPI::ErrorBox(ex.what());
38+
#ifdef CRYMP_CONSOLE_APP
39+
std::fprintf(stderr, "%s\n", error.what());
40+
#else
41+
WinAPI::ErrorBox(error.what());
42+
#endif
5543
return 1;
5644
}
5745

Code/Library/StringTools.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ static std::error_code GetSysErrorCodeWithCategory(DWORD code)
7777
return std::error_code(static_cast<int>(code), GetSysErrorCategory(code));
7878
}
7979

80+
CryMP_Error::CryMP_Error(const std::string& message, std::error_code code)
81+
: m_what(message), m_message(message), m_code(code)
82+
{
83+
if (code)
84+
{
85+
m_what += "\n\nError ";
86+
m_what += std::to_string(code.value());
87+
m_what += ": ";
88+
m_what += code.message();
89+
}
90+
}
91+
8092
std::string StringTools::Format(const char* format, ...)
8193
{
8294
va_list args;
@@ -202,63 +214,51 @@ std::size_t StringTools::FormatToV(char* buffer, std::size_t bufferSize, const c
202214
return length;
203215
}
204216

205-
std::runtime_error StringTools::ErrorFormat(const char* format, ...)
217+
CryMP_Error StringTools::ErrorFormat(const char* format, ...)
206218
{
207219
va_list args;
208220
va_start(args, format);
209-
std::runtime_error error = ErrorFormatV(format, args);
221+
CryMP_Error error = ErrorFormatV(format, args);
210222
va_end(args);
211223

212224
return error;
213225
}
214226

215-
std::runtime_error StringTools::ErrorFormatV(const char* format, va_list args)
227+
CryMP_Error StringTools::ErrorFormatV(const char* format, va_list args)
216228
{
217-
const std::string message = FormatV(format, args);
218-
219-
return std::runtime_error(message);
229+
return CryMP_Error(FormatV(format, args));
220230
}
221231

222-
std::system_error StringTools::SysErrorFormat(const char* format, ...)
232+
CryMP_Error StringTools::SysErrorFormat(const char* format, ...)
223233
{
224234
va_list args;
225235
va_start(args, format);
226-
std::system_error error = SysErrorFormatV(format, args);
236+
CryMP_Error error = SysErrorFormatV(format, args);
227237
va_end(args);
228238

229239
return error;
230240
}
231241

232-
std::system_error StringTools::SysErrorFormatV(const char* format, va_list args)
242+
CryMP_Error StringTools::SysErrorFormatV(const char* format, va_list args)
233243
{
234244
const DWORD code = ::GetLastError();
235245

236-
std::string message = FormatV(format, args);
237-
238-
message += ": Error code ";
239-
message += std::to_string(code);
240-
241-
return std::system_error(GetSysErrorCodeWithCategory(code), message);
246+
return CryMP_Error(FormatV(format, args), GetSysErrorCodeWithCategory(code));
242247
}
243248

244-
std::system_error StringTools::SysErrorErrnoFormat(const char* format, ...)
249+
CryMP_Error StringTools::SysErrorErrnoFormat(const char* format, ...)
245250
{
246251
va_list args;
247252
va_start(args, format);
248-
std::system_error error = SysErrorErrnoFormatV(format, args);
253+
CryMP_Error error = SysErrorErrnoFormatV(format, args);
249254
va_end(args);
250255

251256
return error;
252257
}
253258

254-
std::system_error StringTools::SysErrorErrnoFormatV(const char* format, va_list args)
259+
CryMP_Error StringTools::SysErrorErrnoFormatV(const char* format, va_list args)
255260
{
256261
const int code = errno;
257262

258-
std::string message = FormatV(format, args);
259-
260-
message += ": Error code ";
261-
message += std::to_string(code);
262-
263-
return std::system_error(code, std::generic_category(), message);
263+
return CryMP_Error(FormatV(format, args), std::error_code(code, std::generic_category()));
264264
}

Code/Library/StringTools.h

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,37 @@
22

33
#include <cstdarg>
44
#include <cstddef>
5-
#include <stdexcept>
5+
#include <exception>
66
#include <string>
77
#include <string_view>
8-
#include <system_error>
8+
#include <system_error> // std::error_code
99
#include <type_traits>
1010

11+
class CryMP_Error : public std::exception
12+
{
13+
std::string m_what;
14+
std::string m_message;
15+
std::error_code m_code;
16+
17+
public:
18+
explicit CryMP_Error(const std::string& message, std::error_code code = {});
19+
20+
const char* what() const noexcept override
21+
{
22+
return m_what.c_str();
23+
}
24+
25+
const std::string& message() const noexcept
26+
{
27+
return m_message;
28+
}
29+
30+
const std::error_code& code() const noexcept
31+
{
32+
return m_code;
33+
}
34+
};
35+
1136
extern "C" __declspec(dllimport) int __stdcall MultiByteToWideChar(
1237
unsigned int codepage,
1338
unsigned long flags,
@@ -348,12 +373,12 @@ namespace StringTools
348373
std::size_t FormatTo(char* buffer, std::size_t bufferSize, const char* format, ...);
349374
std::size_t FormatToV(char* buffer, std::size_t bufferSize, const char* format, va_list args);
350375

351-
std::runtime_error ErrorFormat(const char* format, ...);
352-
std::runtime_error ErrorFormatV(const char* format, va_list args);
376+
CryMP_Error ErrorFormat(const char* format, ...);
377+
CryMP_Error ErrorFormatV(const char* format, va_list args);
353378

354-
std::system_error SysErrorFormat(const char* format, ...);
355-
std::system_error SysErrorFormatV(const char* format, va_list args);
379+
CryMP_Error SysErrorFormat(const char* format, ...);
380+
CryMP_Error SysErrorFormatV(const char* format, va_list args);
356381

357-
std::system_error SysErrorErrnoFormat(const char* format, ...);
358-
std::system_error SysErrorErrnoFormatV(const char* format, va_list args);
382+
CryMP_Error SysErrorErrnoFormat(const char* format, ...);
383+
CryMP_Error SysErrorErrnoFormatV(const char* format, va_list args);
359384
}

Code/Library/WinAPI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ namespace WinAPI
272272
using HTTPRequestReader = std::function<size_t(void*,size_t)>; // buffer, buffer size, returns data length
273273
using HTTPRequestCallback = std::function<void(uint64_t,const HTTPRequestReader&)>; // content length, reader
274274

275-
// blocking, returns HTTP status code, throws std::system_error
275+
// blocking, returns HTTP status code, throws CryMP_Error
276276
int HTTPRequest(
277277
const std::string_view & method,
278278
const std::string_view & url,

0 commit comments

Comments
 (0)