From 109381f8a37bda7947f6dc461e949e2fd6a063dd Mon Sep 17 00:00:00 2001 From: Moritz Sallermann Date: Thu, 14 Mar 2024 17:03:10 +0000 Subject: [PATCH] Better error message in get_file_contents, if the file does not exist --- include/util/misc.hpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/include/util/misc.hpp b/include/util/misc.hpp index 1f3f8bd..0baa0da 100644 --- a/include/util/misc.hpp +++ b/include/util/misc.hpp @@ -1,5 +1,9 @@ #pragma once +#include "fmt/format.h" +#include +#include #include +#include #include namespace Seldon @@ -7,6 +11,12 @@ namespace Seldon inline std::string get_file_contents( const std::string & filename ) { + auto path = std::filesystem::path( filename ); + if( !std::filesystem::exists( path ) ) + { + throw std::runtime_error( fmt::format( "Canot read from {}. File does not exist!", fmt::streamed( path ) ) ); + } + std::ifstream in( filename, std::ios::in | std::ios::binary ); if( in ) { @@ -18,7 +28,7 @@ inline std::string get_file_contents( const std::string & filename ) in.close(); return ( contents ); } - throw( std::runtime_error( "Cannot read in file." ) ); + throw( std::runtime_error( "Cannot read file." ) ); } } // namespace Seldon \ No newline at end of file