Skip to content

Commit

Permalink
Better error message in get_file_contents, if the file does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
MSallermann committed Mar 14, 2024
1 parent d52db23 commit 109381f
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion include/util/misc.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#pragma once
#include "fmt/format.h"
#include <fmt/ostream.h>
#include <filesystem>
#include <fstream>
#include <stdexcept>
#include <string>

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 )
{
Expand All @@ -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

0 comments on commit 109381f

Please sign in to comment.