Skip to content

Commit 3a1cf7c

Browse files
committed
Improved on_error testis, fixed a minor noexcept issue
1 parent beaac5b commit 3a1cf7c

File tree

3 files changed

+128
-3
lines changed

3 files changed

+128
-3
lines changed

include/boost/leaf/error.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,15 +611,14 @@ namespace detail
611611
return int(id);
612612
}
613613

614-
inline int start_new_error()
614+
inline int start_new_error() noexcept(!BOOST_LEAF_CFG_CAPTURE)
615615
{
616-
int id = new_id();
617616
#if BOOST_LEAF_CFG_CAPTURE
618617
if( dynamic_allocator * da = get_dynamic_allocator() )
619618
for( preloaded_base const * e = da->preloaded_list(); e; e = e->next_ )
620619
e->reserve(*da);
621620
#endif
622-
return id;
621+
return new_id();
623622
}
624623

625624
struct inject_loc

include/boost/leaf/on_error.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,19 @@ class error_monitor
4040
# else
4141
if( std::uncaught_exception() )
4242
# endif
43+
# if BOOST_LEAF_CFG_CAPTURE
44+
try
45+
{
46+
return detail::start_new_error();
47+
}
48+
catch(...)
49+
{
50+
BOOST_LEAF_ASSERT(detail::current_id() == err_id_);
51+
return detail::new_id();
52+
}
53+
# else
4354
return detail::start_new_error();
55+
# endif
4456
#endif
4557
return 0;
4658
}

test/on_error_alloc_fail_test.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# include <boost/leaf/handle_errors.hpp>
1111
# include <boost/leaf/result.hpp>
1212
# include <boost/leaf/diagnostics.hpp>
13+
# include <boost/leaf/exception.hpp>
1314
#endif
1415

1516
#if !BOOST_LEAF_CFG_CAPTURE || defined(BOOST_LEAF_NO_EXCEPTIONS)
@@ -22,6 +23,7 @@ int main()
2223
#else
2324

2425
#include <sstream>
26+
#include <stdexcept>
2527
#include <string>
2628

2729
namespace leaf = boost::leaf;
@@ -119,6 +121,118 @@ int main()
119121
}
120122
#endif
121123

124+
////////////////////////////////////////
125+
126+
{
127+
auto captured = leaf::try_capture_all(
128+
[]() -> leaf::result<void>
129+
{
130+
auto load = leaf::on_error( other_info<1>{1}, alloc_fail_info{42}, other_info<2>{2} );
131+
throw std::runtime_error("test");
132+
} );
133+
134+
int r = leaf::try_handle_all(
135+
[&]() -> leaf::result<int>
136+
{
137+
(void) captured.value();
138+
return 0;
139+
},
140+
[]( other_info<1> const & oi1, other_info<2> const * oi2, alloc_fail_info const * afi )
141+
{
142+
BOOST_TEST_EQ(oi1.value, 1);
143+
BOOST_TEST_EQ(oi2, nullptr);
144+
BOOST_TEST_EQ(afi, nullptr);
145+
return 1;
146+
},
147+
[]
148+
{
149+
return 2;
150+
} );
151+
BOOST_TEST_EQ(r, 1);
152+
}
153+
154+
#if BOOST_LEAF_CFG_DIAGNOSTICS && BOOST_LEAF_CFG_STD_STRING
155+
{
156+
int r = leaf::try_catch(
157+
[]() -> int
158+
{
159+
auto load = leaf::on_error( other_info<1>{1}, alloc_fail_info{42}, other_info<2>{2} );
160+
throw std::runtime_error("test");
161+
},
162+
[]( leaf::diagnostic_details const & dd )
163+
{
164+
std::ostringstream s;
165+
s << dd;
166+
std::string str = s.str();
167+
BOOST_TEST(str.find("other_info<1>") != std::string::npos);
168+
BOOST_TEST(str.find("other_info<2>") == std::string::npos);
169+
BOOST_TEST(str.find("alloc_fail_info") == std::string::npos);
170+
return 1;
171+
},
172+
[]
173+
{
174+
return 2;
175+
} );
176+
BOOST_TEST_EQ(r, 1);
177+
}
178+
#endif
179+
180+
////////////////////////////////////////
181+
182+
{
183+
auto captured = leaf::try_capture_all(
184+
[]() -> leaf::result<void>
185+
{
186+
auto load = leaf::on_error( other_info<1>{1}, alloc_fail_info{42}, other_info<2>{2} );
187+
leaf::throw_exception(std::runtime_error("test"));
188+
} );
189+
190+
int r = leaf::try_handle_all(
191+
[&]() -> leaf::result<int>
192+
{
193+
(void) captured.value();
194+
return 0;
195+
},
196+
[]( std::bad_alloc const &, other_info<1> const & oi1, other_info<2> const * oi2, alloc_fail_info const * afi )
197+
{
198+
BOOST_TEST_EQ(oi1.value, 1);
199+
BOOST_TEST_EQ(oi2, nullptr);
200+
BOOST_TEST_EQ(afi, nullptr);
201+
return 1;
202+
},
203+
[]
204+
{
205+
return 2;
206+
} );
207+
BOOST_TEST_EQ(r, 1);
208+
}
209+
210+
#if BOOST_LEAF_CFG_DIAGNOSTICS && BOOST_LEAF_CFG_STD_STRING
211+
{
212+
int r = leaf::try_catch(
213+
[]() -> int
214+
{
215+
auto load = leaf::on_error( other_info<1>{1}, alloc_fail_info{42}, other_info<2>{2} );
216+
leaf::throw_exception(std::runtime_error("test"));
217+
},
218+
[]( std::bad_alloc const &, leaf::diagnostic_details const & dd )
219+
{
220+
std::ostringstream s;
221+
s << dd;
222+
std::string str = s.str();
223+
BOOST_TEST(str.find("other_info<1>") != std::string::npos);
224+
BOOST_TEST(str.find("other_info<2>") == std::string::npos);
225+
BOOST_TEST(str.find("alloc_fail_info") == std::string::npos);
226+
return 1;
227+
},
228+
[]
229+
{
230+
return 2;
231+
} );
232+
BOOST_TEST_EQ(r, 1);
233+
}
234+
#endif
235+
122236
return boost::report_errors();
123237
}
124238

0 commit comments

Comments
 (0)