2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <gtest/gtest.h>
19 #include <folly/ExceptionWrapper.h>
20 #include <folly/Conv.h>
21 #include <folly/Portability.h>
23 using namespace folly;
25 // Tests that when we call throwException, the proper type is thrown (derived)
26 TEST(ExceptionWrapper, throw_test) {
27 std::runtime_error e("payload");
28 auto ew = make_exception_wrapper<std::runtime_error>(e);
30 std::vector<exception_wrapper> container;
31 container.push_back(ew);
34 container[0].throwException();
35 } catch (std::runtime_error& e) {
36 std::string expected = "payload";
37 std::string actual = e.what();
38 EXPECT_EQ(expected, actual);
42 TEST(ExceptionWrapper, members) {
43 auto ew = exception_wrapper();
44 EXPECT_FALSE(bool(ew));
45 EXPECT_EQ(ew.what(), "");
46 EXPECT_EQ(ew.class_name(), "");
47 ew = make_exception_wrapper<std::runtime_error>("payload");
48 EXPECT_TRUE(bool(ew));
49 EXPECT_EQ(ew.what(), "std::runtime_error: payload");
50 EXPECT_EQ(ew.class_name(), "std::runtime_error");
53 TEST(ExceptionWrapper, equals) {
54 std::runtime_error e("payload");
55 auto ew1 = make_exception_wrapper<std::runtime_error>(e);
59 auto ew3 = try_and_catch<std::exception>([&]() {
60 throw std::runtime_error("payload");
62 auto ew4 = try_and_catch<std::exception>([&]() {
68 TEST(ExceptionWrapper, not_equals) {
69 std::runtime_error e1("payload");
70 std::runtime_error e2("payload");
71 auto ew1 = make_exception_wrapper<std::runtime_error>(e1);
72 auto ew2 = make_exception_wrapper<std::runtime_error>(e2);
75 auto ew3 = make_exception_wrapper<std::runtime_error>(e1);
76 auto ew4 = make_exception_wrapper<std::runtime_error>(e1);
79 auto ew5 = try_and_catch<std::exception>([&]() {
82 auto ew6 = try_and_catch<std::exception>([&]() {
88 TEST(ExceptionWrapper, try_and_catch_test) {
89 std::string expected = "payload";
91 // Catch rightmost matching exception type
92 exception_wrapper ew = try_and_catch<std::exception, std::runtime_error>(
94 throw std::runtime_error(expected);
96 EXPECT_TRUE(bool(ew));
97 EXPECT_TRUE(ew.getCopied());
98 EXPECT_EQ(ew.what(), "std::runtime_error: payload");
99 EXPECT_EQ(ew.class_name(), "std::runtime_error");
100 auto rep = ew.is_compatible_with<std::runtime_error>();
103 // Changing order is like catching in wrong order. Beware of this in your
105 auto ew2 = try_and_catch<std::runtime_error, std::exception>([=]() {
106 throw std::runtime_error(expected);
108 EXPECT_TRUE(bool(ew2));
109 // We are catching a std::exception, not std::runtime_error.
110 EXPECT_FALSE(ew2.getCopied());
111 // But, we can still get the actual type if we want it.
112 rep = ew2.is_compatible_with<std::runtime_error>();
115 // Catches even if not rightmost.
116 auto ew3 = try_and_catch<std::exception, std::runtime_error>([]() {
117 throw std::exception();
119 EXPECT_TRUE(bool(ew3));
120 EXPECT_EQ(ew3.what(), "std::exception: std::exception");
121 EXPECT_EQ(ew3.class_name(), "std::exception");
122 rep = ew3.is_compatible_with<std::runtime_error>();
125 // If does not catch, throws.
127 try_and_catch<std::runtime_error>([]() {
128 throw std::exception();
133 class AbstractIntException : public std::exception {
135 virtual int getInt() const = 0;
138 class IntException : public AbstractIntException {
140 explicit IntException(int i)
143 int getInt() const override { return i_; }
144 const char* what() const noexcept override {
145 what_ = folly::to<std::string>("int == ", i_);
146 return what_.c_str();
151 mutable std::string what_;
154 TEST(ExceptionWrapper, with_exception_test) {
157 // This works, and doesn't slice.
158 exception_wrapper ew = try_and_catch<std::exception, std::runtime_error>(
160 throw IntException(expected);
162 EXPECT_TRUE(bool(ew));
163 EXPECT_EQ(ew.what(), "IntException: int == 23");
164 EXPECT_EQ(ew.class_name(), "IntException");
165 ew.with_exception([&](const IntException& ie) {
166 EXPECT_EQ(ie.getInt(), expected);
169 // I can try_and_catch a non-copyable base class. This will use
170 // std::exception_ptr internally.
171 exception_wrapper ew2 = try_and_catch<AbstractIntException>(
173 throw IntException(expected);
175 EXPECT_TRUE(bool(ew2));
176 EXPECT_EQ(ew2.what(), "IntException: int == 23");
177 EXPECT_EQ(ew2.class_name(), "IntException");
178 ew2.with_exception([&](AbstractIntException& ie) {
179 EXPECT_EQ(ie.getInt(), expected);
180 EXPECT_TRUE(dynamic_cast<IntException*>(&ie));
183 // Test with const this. If this compiles and does not crash due to
184 // infinite loop when it runs, it succeeds.
185 const exception_wrapper& cew = ew;
186 cew.with_exception([&](const IntException& /* ie */) { SUCCEED(); });
188 // This won't even compile. You can't use a function which takes a
189 // non-const reference with a const exception_wrapper.
191 cew.with_exception([&](IntException& ie) {
197 TEST(ExceptionWrapper, with_exception_deduction) {
198 auto ew = make_exception_wrapper<std::runtime_error>("hi");
199 EXPECT_TRUE(ew.with_exception([](std::runtime_error&) {}));
200 EXPECT_TRUE(ew.with_exception([](std::exception&) {}));
201 EXPECT_FALSE(ew.with_exception([](std::logic_error&) {}));
204 TEST(ExceptionWrapper, with_exception_deduction_exn_const) {
205 auto ew = make_exception_wrapper<std::runtime_error>("hi");
206 EXPECT_TRUE(ew.with_exception([](const std::runtime_error&) {}));
207 EXPECT_TRUE(ew.with_exception([](const std::exception&) {}));
208 EXPECT_FALSE(ew.with_exception([](const std::logic_error&) {}));
211 TEST(ExceptionWrapper, with_exception_deduction_wrap_const_exn_const) {
212 const auto cew = make_exception_wrapper<std::runtime_error>("hi");
213 EXPECT_TRUE(cew.with_exception([](const std::runtime_error&) {}));
214 EXPECT_TRUE(cew.with_exception([](const std::exception&) {}));
215 EXPECT_FALSE(cew.with_exception([](const std::logic_error&) {}));
218 TEST(ExceptionWrapper, with_exception_deduction_returning) {
219 auto ew = make_exception_wrapper<std::runtime_error>("hi");
220 EXPECT_TRUE(ew.with_exception([](std::runtime_error&) { return 3; }));
221 EXPECT_TRUE(ew.with_exception([](std::exception&) { return "hello"; }));
222 EXPECT_FALSE(ew.with_exception([](std::logic_error&) { return nullptr; }));
226 template <typename T>
227 T& r_to_l(T v) { return std::ref(v); }
230 TEST(ExceptionWrapper, with_exception_deduction_functor_lvalue) {
231 auto ew = make_exception_wrapper<std::runtime_error>("hi");
232 EXPECT_TRUE(ew.with_exception(r_to_l([](std::runtime_error&) {})));
233 EXPECT_TRUE(ew.with_exception(r_to_l([](std::exception&) {})));
234 EXPECT_FALSE(ew.with_exception(r_to_l([](std::logic_error&) {})));
237 TEST(ExceptionWrapper, non_std_exception_test) {
240 exception_wrapper ew = try_and_catch<std::exception, int>(
244 EXPECT_TRUE(bool(ew));
245 EXPECT_FALSE(ew.is_compatible_with<std::exception>());
246 EXPECT_EQ(ew.what(), "int");
247 EXPECT_EQ(ew.class_name(), "int");
248 // non-std::exception types are supported, but the only way to
249 // access their value is to explicity rethrow and catch it.
252 } catch /* nolint */ (int& i) {
253 EXPECT_EQ(i, expected);
258 TEST(ExceptionWrapper, exceptionStr) {
259 auto ew = make_exception_wrapper<std::runtime_error>("argh");
260 EXPECT_EQ("std::runtime_error: argh", exceptionStr(ew));
264 class TestException : public std::exception { };
265 void testEW(const exception_wrapper& ew) {
266 EXPECT_THROW(ew.throwException(), TestException);
270 TEST(ExceptionWrapper, implicitConstruction) {
271 // Try with both lvalue and rvalue references
274 testEW(TestException());