Wrap exceptions in std::exception to verbose output on exception. (#1316)

This commit is contained in:
Ivan Stepanov 2023-05-04 19:56:27 +03:00 committed by GitHub
parent d3e8093e9b
commit 34d9f22f44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,6 +14,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <stdexcept>
#ifdef __has_include #ifdef __has_include
#if __has_include(<unistd.h>) #if __has_include(<unistd.h>)
@ -74,7 +75,7 @@ struct llama_file {
llama_file(const char * fname, const char * mode) { llama_file(const char * fname, const char * mode) {
fp = std::fopen(fname, mode); fp = std::fopen(fname, mode);
if (fp == NULL) { if (fp == NULL) {
throw format("failed to open %s: %s", fname, std::strerror(errno)); throw std::runtime_error(format("failed to open %s: %s", fname, strerror(errno)));
} }
seek(0, SEEK_END); seek(0, SEEK_END);
size = tell(); size = tell();
@ -107,10 +108,10 @@ struct llama_file {
errno = 0; errno = 0;
std::size_t ret = std::fread(ptr, size, 1, fp); std::size_t ret = std::fread(ptr, size, 1, fp);
if (ferror(fp)) { if (ferror(fp)) {
throw format("read error: %s", strerror(errno)); throw std::runtime_error(format("read error: %s", strerror(errno)));
} }
if (ret != 1) { if (ret != 1) {
throw std::string("unexpectedly reached end of file"); throw std::runtime_error(std::string("unexpectedly reached end of file"));
} }
} }
@ -133,7 +134,7 @@ struct llama_file {
errno = 0; errno = 0;
size_t ret = std::fwrite(ptr, size, 1, fp); size_t ret = std::fwrite(ptr, size, 1, fp);
if (ret != 1) { if (ret != 1) {
throw format("write error: %s", strerror(errno)); throw std::runtime_error(format("write error: %s", strerror(errno)));
} }
} }
@ -180,7 +181,7 @@ struct llama_mmap {
#endif #endif
addr = mmap(NULL, file->size, PROT_READ, flags, fd, 0); addr = mmap(NULL, file->size, PROT_READ, flags, fd, 0);
if (addr == MAP_FAILED) { if (addr == MAP_FAILED) {
throw format("mmap failed: %s", strerror(errno)); throw std::runtime_error(format("mmap failed: %s", strerror(errno)));
} }
if (prefetch) { if (prefetch) {
@ -207,7 +208,7 @@ struct llama_mmap {
DWORD error = GetLastError(); DWORD error = GetLastError();
if (hMapping == NULL) { if (hMapping == NULL) {
throw format("CreateFileMappingA failed: %s", llama_format_win_err(error).c_str()); throw std::runtime_error(format("CreateFileMappingA failed: %s", llama_format_win_err(error).c_str()));
} }
addr = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0); addr = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
@ -215,7 +216,7 @@ struct llama_mmap {
CloseHandle(hMapping); CloseHandle(hMapping);
if (addr == NULL) { if (addr == NULL) {
throw format("MapViewOfFile failed: %s", llama_format_win_err(error).c_str()); throw std::runtime_error(format("MapViewOfFile failed: %s", llama_format_win_err(error).c_str()));
} }
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
@ -245,7 +246,7 @@ struct llama_mmap {
llama_mmap(struct llama_file *, bool prefetch = true) { llama_mmap(struct llama_file *, bool prefetch = true) {
(void)prefetch; (void)prefetch;
throw std::string("mmap not supported"); throw std::runtime_error(std::string("mmap not supported"));
} }
#endif #endif
}; };