Skip to content

Commit 801e5e8

Browse files
committed
Fix for Issue 53
1 parent e02fc6b commit 801e5e8

File tree

5 files changed

+176
-2
lines changed

5 files changed

+176
-2
lines changed

include/boost/leaf/handle_errors.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ try_handle_some( TryBlock && try_block, H && ... h ) noexcept
733733
using R = typename std::decay<decltype(std::declval<TryBlock>()())>::type;
734734
auto rr = ctx.template handle_error<R>(id, std::forward<H>(h)..., [&r]()->R { return std::move(r); });
735735
if( !rr )
736-
ctx.propagate(id);
736+
ctx.propagate(rr.error());
737737
return rr;
738738
}
739739
}
@@ -869,7 +869,7 @@ try_handle_some( TryBlock && try_block, H && ... h )
869869
using R = typename std::decay<decltype(std::declval<TryBlock>()())>::type;
870870
auto rr = ctx.template handle_error<R>(id, std::forward<H>(h)..., [&r]()->R { return std::move(r); });
871871
if( !rr )
872-
ctx.propagate(id);
872+
ctx.propagate(rr.error());
873873
return rr;
874874
}
875875
}

meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ if option_enable_unit_tests
143143
'exception_test',
144144
'exception_to_result_test',
145145
'function_traits_test',
146+
'github_issue53_test',
147+
'github_issue53x_test',
146148
'handle_all_other_result_test',
147149
'handle_all_test',
148150
'handle_basic_test',

test/Jamfile.v2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ run error_id_test.cpp ;
8282
run exception_test.cpp ;
8383
run exception_to_result_test.cpp ;
8484
run function_traits_test.cpp ;
85+
run github_issue53_test.cpp ;
86+
run github_issue53x_test.cpp ;
8587
run handle_all_other_result_test.cpp ;
8688
run handle_all_test.cpp ;
8789
run handle_basic_test.cpp ;

test/github_issue53_test.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2018-2022 Emil Dotchevski and Reverge Studios, Inc.
2+
3+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifdef BOOST_LEAF_TEST_SINGLE_HEADER
7+
# include "leaf.hpp"
8+
#else
9+
# include <boost/leaf/handle_errors.hpp>
10+
# include <boost/leaf/result.hpp>
11+
# include <boost/leaf/pred.hpp>
12+
#endif
13+
14+
#include "lightweight_test.hpp"
15+
16+
namespace leaf = boost::leaf;
17+
18+
enum class ErrorCode
19+
{
20+
E_GENERIC_UNEXPECTED,
21+
E_GENERIC_PARSE
22+
};
23+
24+
leaf::result<int> test1( int a )
25+
{
26+
if( a == 1 )
27+
return leaf::new_error(ErrorCode::E_GENERIC_UNEXPECTED);
28+
return 32;
29+
}
30+
31+
leaf::result<float> test2(int a)
32+
{
33+
return leaf::try_handle_some(
34+
[&]() -> leaf::result<float>
35+
{
36+
BOOST_LEAF_AUTO(val, test1(a));
37+
(void) val;
38+
return 4.5;
39+
},
40+
[](leaf::match<ErrorCode,ErrorCode::E_GENERIC_UNEXPECTED>) -> leaf::result<float>
41+
{
42+
return leaf::new_error(ErrorCode::E_GENERIC_PARSE);
43+
}
44+
);
45+
}
46+
47+
void test3(int a)
48+
{
49+
int x = 0;
50+
leaf::try_handle_all(
51+
[&]() -> leaf::result<void>
52+
{
53+
BOOST_LEAF_AUTO(val, test2(a));
54+
(void) val;
55+
return {};
56+
},
57+
[&](leaf::match<ErrorCode, ErrorCode::E_GENERIC_PARSE>)
58+
{
59+
x = 1;
60+
},
61+
[&](ErrorCode e)
62+
{
63+
x = 2;
64+
},
65+
[&]()
66+
{
67+
x = 3;
68+
}
69+
);
70+
BOOST_TEST_EQ(x, 1);
71+
}
72+
73+
int main()
74+
{
75+
test3(1);
76+
return boost::report_errors();
77+
}

test/github_issue53x_test.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright 2018-2022 Emil Dotchevski and Reverge Studios, Inc.
2+
3+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5+
6+
7+
#include <boost/leaf/config.hpp>
8+
9+
#ifdef BOOST_LEAF_NO_EXCEPTIONS
10+
11+
#include <iostream>
12+
13+
int main()
14+
{
15+
std::cout << "Unit test not applicable." << std::endl;
16+
return 0;
17+
}
18+
19+
#else
20+
21+
#ifdef BOOST_LEAF_TEST_SINGLE_HEADER
22+
# include "leaf.hpp"
23+
#else
24+
# include <boost/leaf/handle_errors.hpp>
25+
# include <boost/leaf/pred.hpp>
26+
#endif
27+
28+
#include "lightweight_test.hpp"
29+
30+
namespace leaf = boost::leaf;
31+
32+
enum class ErrorCode
33+
{
34+
E_GENERIC_UNEXPECTED,
35+
E_GENERIC_PARSE
36+
};
37+
38+
int test1( int a )
39+
{
40+
if( a == 1 )
41+
BOOST_LEAF_THROW_EXCEPTION(ErrorCode::E_GENERIC_UNEXPECTED);
42+
return 32;
43+
}
44+
45+
float test2(int a)
46+
{
47+
return leaf::try_catch(
48+
[&]
49+
{
50+
auto val = test1(a);
51+
(void) val;
52+
return 4.5;
53+
},
54+
[](leaf::match<ErrorCode,ErrorCode::E_GENERIC_UNEXPECTED>)
55+
{
56+
BOOST_LEAF_THROW_EXCEPTION(ErrorCode::E_GENERIC_PARSE);
57+
return 0;
58+
}
59+
);
60+
}
61+
62+
void test3(int a)
63+
{
64+
int x = 0;
65+
leaf::try_catch(
66+
[&]
67+
{
68+
auto val = test2(a);
69+
(void) val;
70+
},
71+
[&](leaf::match<ErrorCode, ErrorCode::E_GENERIC_PARSE>)
72+
{
73+
x = 1;
74+
},
75+
[&](ErrorCode e)
76+
{
77+
x = 2;
78+
},
79+
[&]()
80+
{
81+
x = 3;
82+
}
83+
);
84+
BOOST_TEST_EQ(x, 1);
85+
}
86+
87+
int main()
88+
{
89+
test3(1);
90+
return boost::report_errors();
91+
}
92+
93+
#endif

0 commit comments

Comments
 (0)