aboutsummaryrefslogtreecommitdiff
path: root/c/data.h
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-09-03 15:28:56 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-09-03 15:28:56 +0200
commit6ae7e24675cff4ff6b808c3024f45083f35ced97 (patch)
tree2a47273f325d8367db2a8669691273f02ca83eb9 /c/data.h
parent2cd22c671c67deaf2c1fcb659e3262bf57552557 (diff)
downloadEpisodeBrowser-6ae7e24675cff4ff6b808c3024f45083f35ced97.tar.gz
Improve error handling.
Diffstat (limited to 'c/data.h')
-rw-r--r--c/data.h20
1 files changed, 13 insertions, 7 deletions
diff --git a/c/data.h b/c/data.h
index 2c287a2..bbc01d2 100644
--- a/c/data.h
+++ b/c/data.h
@@ -5,9 +5,12 @@
#include <windows.h>
#include <libxml/xmlerror.h>
+#include "err.h"
#include "util.h"
#include "win32.h"
+using namespace std::string_literals;
+
struct XmlError : public std::exception
{
const char* msg;
@@ -102,6 +105,7 @@ struct FileView
Unique<HANDLE, CloseHandle> hf;
Unique<HANDLE, CloseHandle> hm;
Unique<T*, FreeView> view;
+ const wchar_t* filename;
size_t c;
static FileView Initialized(const wchar_t* filename, size_t c, size_t cInit = 0)
@@ -118,7 +122,7 @@ struct FileView
return {filename, c};
}
- FileView(const wchar_t* filename, size_t c) : c(c)
+ FileView(const wchar_t* filename, size_t c) : filename(filename), c(c)
{
hf = CreateFile(filename, GENERIC_READ|GENERIC_WRITE,
0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
@@ -127,9 +131,9 @@ struct FileView
hf = CreateFile(filename, GENERIC_READ|GENERIC_WRITE,
0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hf.Bad(INVALID_HANDLE_VALUE))
- throw Win32Error();
+ throw Err(WINDOWS, L"File "s + filename + L" could not be created: %s");
} else
- throw Win32Error();
+ throw Err(WINDOWS, L"File "s + filename + L" could not be opened: %s");
}
LARGE_INTEGER cbMap;
@@ -137,11 +141,11 @@ struct FileView
hm = CreateFileMapping(hf.v, nullptr, PAGE_READWRITE,
cbMap.HighPart, cbMap.LowPart, nullptr);
if (hm.Bad(0))
- throw Win32Error();
+ throw Err(WINDOWS, L"File mapping for "s + filename + L" could not be created: %s");
view = reinterpret_cast<T*>(MapViewOfFile(hm.v, FILE_MAP_ALL_ACCESS, 0, 0, 0));
if (view.Bad(0))
- throw Win32Error();
+ throw Err(WINDOWS, L"View for "s + filename + L"could not be mapped: %s");
}
/* Access element by index, performing bounds check and, if
@@ -150,7 +154,8 @@ struct FileView
T& At(size_t i)
{
if (i >= c)
- throw std::out_of_range("index larger than buffer");
+ throw Err(GENERIC, L"Element in "s + filename +
+ L" could not be accessed: Index larger than buffer.");
T& t = view.v[i];
@@ -159,7 +164,8 @@ struct FileView
T t_;
memcpy(&t, &t_, sizeof(T));
} else if (t.version != Version<T>)
- throw std::runtime_error("invalid struct version");
+ throw Err(GENERIC, L"Element in "s + filename +
+ L" could not be accessed: Invalid format version.");
}
/* TODO: Use a custom exception type that informs the