-
Notifications
You must be signed in to change notification settings - Fork 5
/
Exception.cpp
88 lines (69 loc) · 1.79 KB
/
Exception.cpp
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
#include "Exception.hpp"
#include "Strings.hpp"
#if defined(___INANITY_PLATFORM_WINDOWS)
#include "platform/windows.hpp"
#elif defined(___INANITY_PLATFORM_POSIX)
#include <errno.h>
// для strerror
#include <cstring>
#else
#error Unknown platform
#endif
BEGIN_INANITY
Exception::Exception(const String& message) : message(message)
{
}
Exception::Exception(const String& message, ptr<Exception> innerException) : message(message), innerException(innerException)
{
}
String Exception::GetMessageText() const
{
return message;
}
ptr<Exception> Exception::GetInnerException() const
{
return innerException;
}
void Exception::PrintStack(std::ostream& stream) const
{
stream << message;
if(innerException)
{
stream << " BECAUSE ";
innerException->PrintStack(stream);
}
}
ptr<Exception> Exception::SystemError()
{
#if defined(___INANITY_PLATFORM_WINDOWS)
int errorCode = GetLastError();
#elif defined(___INANITY_PLATFORM_POSIX)
int errorCode = errno;
#else
#error Unknown platform
#endif
return SystemError(errorCode);
}
ptr<Exception> Exception::SystemError(int errorCode)
{
#if defined(___INANITY_PLATFORM_WINDOWS)
#if defined(___INANITY_PLATFORM_XBOX)
wchar_t buffer[1024];
if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, errorCode, 0, (wchar_t*)&buffer, sizeof(buffer) / sizeof(buffer[0]), 0))
#else
wchar_t* buffer;
if(!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, errorCode, 0, (wchar_t*)&buffer, 0, 0))
#endif
return NEW(Exception("Unknown system error"));
ptr<Exception> exception = NEW(Exception(Strings::Unicode2UTF8(buffer)));
#if !defined(___INANITY_PLATFORM_XBOX)
LocalFree(buffer);
#endif
return exception;
#elif defined(___INANITY_PLATFORM_POSIX)
return NEW(Exception(strerror(errorCode)));
#else
#error Unknown platform
#endif
}
END_INANITY