From 6ae7e24675cff4ff6b808c3024f45083f35ced97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sat, 3 Sep 2022 15:28:56 +0200 Subject: Improve error handling. --- c/data.h | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'c/data.h') 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 #include +#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 hf; Unique hm; Unique 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(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) - 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 -- cgit v1.2.3