From 6c2c804effd5e37108b7f4720a8e755786681d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hr=C3=A1zk=C3=BD?= Date: Thu, 8 Sep 2022 16:58:56 +0200 Subject: [PATCH] fix: Improve error handling of ifstream a bit Set the exceptions mask so that exceptions are thrown when an I/O error occurs. Also throw the same exception type when the opening fails. --- tests/test_parse_file.cpp | 2 +- toml/parser.hpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_parse_file.cpp b/tests/test_parse_file.cpp index 80d26e5..b525f98 100644 --- a/tests/test_parse_file.cpp +++ b/tests/test_parse_file.cpp @@ -1031,5 +1031,5 @@ BOOST_AUTO_TEST_CASE(test_parse_function_compiles) BOOST_AUTO_TEST_CASE(test_parse_nonexistent_file) { - BOOST_CHECK_THROW(toml::parse("nonexistent.toml"), std::runtime_error); + BOOST_CHECK_THROW(toml::parse("nonexistent.toml"), std::ios_base::failure); } diff --git a/toml/parser.hpp b/toml/parser.hpp index 9b70feb..ce93554 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -2488,8 +2488,9 @@ basic_value parse(std::string fname) std::ifstream ifs(fname, std::ios_base::binary); if(!ifs.good()) { - throw std::runtime_error("toml::parse: file open error -> " + fname); + throw std::ios_base::failure("toml::parse: Error opening file \"" + fname + "\""); } + ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit); return parse(ifs, std::move(fname)); }