-
Notifications
You must be signed in to change notification settings - Fork 725
Top level catch all #1430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Top level catch all #1430
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b67a3e3
[catch-all] Complete top-level catch-all in daemon
ricab 06c8cbd
[catch-all] Add general catch-all, use in daemon
ricab 20e7741
[catch-all] Add top-level catch-all to CLI client
ricab e80fb16
[catch-all] Add top-level catch-all to GUI client
ricab b260c5e
[test] Check top catcher calls function
ricab fec32c2
[catch-all] Update category type
ricab 83453e9
[logging] Make CString accessor const
ricab 0a0ebad
[test] Add mock logger
ricab 1ec1443
[test] Check top catcher handles unknown errors
ricab 339b2e2
[test] Use mock logger in all top catcher tests
ricab 48be3a5
[test] Move catcher-test elements to fixture
ricab b4a4d7a
[test] Check top catcher handles normal exceptions
ricab 517546e
[doc] Document top_catch_all
ricab abd64e8
[test] Check catch-all handles custom exceptions
ricab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ class CString | |
{ | ||
} | ||
|
||
const char* c_str() | ||
const char* c_str() const | ||
{ | ||
return data; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (C) 2020 Canonical, Ltd. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#ifndef MULTIPASS_TOP_CATCH_ALL_H | ||
#define MULTIPASS_TOP_CATCH_ALL_H | ||
|
||
#include <multipass/format.h> | ||
#include <multipass/logging/log.h> | ||
|
||
namespace multipass | ||
{ | ||
/** | ||
* Call f within a try-catch, catching and logging anything that it throws. | ||
* | ||
* @tparam F The type of the callable f. It must be callable and return int. | ||
* @tparam Args The types of f's arguments | ||
* @param log_category The category to use when logging exceptions | ||
* @param f The int-returning function to protect with a catch-all | ||
* @param args The arguments to pass to the function f | ||
* @return The result of f when no exception is thrown, EXIT_FAILURE (from cstdlib) otherwise | ||
*/ | ||
template <typename F, typename... Args> // F needs to return int | ||
int top_catch_all(const logging::CString log_category, F f, Args&&... args); // not noexcept because logging isn't | ||
} | ||
|
||
template <typename F, typename... Args> | ||
inline int multipass::top_catch_all(const logging::CString log_category, F f, Args&&... args) | ||
{ | ||
namespace mpl = multipass::logging; | ||
try | ||
{ | ||
return f(std::forward<Args>(args)...); | ||
} | ||
catch (const std::exception& e) | ||
{ | ||
mpl::log(mpl::Level::error, log_category, fmt::format("Caught an unhandled exception: {}", e.what())); | ||
return EXIT_FAILURE; | ||
} | ||
catch (...) | ||
{ | ||
mpl::log(mpl::Level::error, log_category, "Caught an unknown exception"); | ||
return EXIT_FAILURE; | ||
} | ||
} | ||
|
||
#endif // MULTIPASS_TOP_CATCH_ALL_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2020 Canonical, Ltd. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#ifndef MULTIPASS_MOCK_LOGGER_H | ||
#define MULTIPASS_MOCK_LOGGER_H | ||
|
||
#include <multipass/logging/logger.h> | ||
|
||
#include <gmock/gmock.h> | ||
|
||
namespace multipass | ||
{ | ||
namespace test | ||
{ | ||
class MockLogger : public multipass::logging::Logger | ||
{ | ||
public: | ||
MockLogger() = default; | ||
MOCK_CONST_METHOD3(log, void(multipass::logging::Level level, multipass::logging::CString category, | ||
multipass::logging::CString message)); | ||
}; | ||
} // namespace test | ||
} // namespace multipass | ||
|
||
#endif // MULTIPASS_MOCK_LOGGER_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright (C) 2020 Canonical, Ltd. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#include "mock_logger.h" | ||
|
||
#include <multipass/logging/log.h> | ||
#include <multipass/top_catch_all.h> | ||
|
||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <stdexcept> | ||
|
||
namespace mp = multipass; | ||
namespace mpl = multipass::logging; | ||
namespace mpt = multipass::test; | ||
using namespace testing; | ||
|
||
namespace | ||
{ | ||
struct TopCatchAll : public Test | ||
{ | ||
void SetUp() override | ||
{ | ||
mock_logger = std::make_shared<StrictMock<mpt::MockLogger>>(); | ||
mpl::set_logger(mock_logger); | ||
} | ||
|
||
void TearDown() override | ||
{ | ||
mock_logger.reset(); | ||
mpl::set_logger(mock_logger); | ||
} | ||
|
||
template <typename Matcher> | ||
auto make_cstring_matcher(const Matcher& matcher) | ||
{ | ||
return Property(&mpl::CString::c_str, matcher); | ||
} | ||
|
||
auto make_category_matcher() | ||
{ | ||
return make_cstring_matcher(StrEq(category.c_str())); | ||
} | ||
|
||
const std::string category = "testing"; | ||
std::shared_ptr<StrictMock<mpt::MockLogger>> mock_logger = nullptr; | ||
}; | ||
|
||
struct CustomExceptionForTesting : public std::exception | ||
{ | ||
public: | ||
CustomExceptionForTesting() = default; | ||
const char* what() const noexcept override | ||
{ | ||
return msg; | ||
} | ||
|
||
inline static constexpr const auto msg = "custom"; | ||
}; | ||
|
||
} // namespace | ||
|
||
TEST_F(TopCatchAll, calls_function_with_no_args) | ||
{ | ||
int ret = 123, got = 0; | ||
EXPECT_NO_THROW(got = mp::top_catch_all("", [ret] { return ret; });); | ||
EXPECT_EQ(got, ret); | ||
} | ||
|
||
TEST_F(TopCatchAll, calls_function_with_args) | ||
{ | ||
int a = 5, b = 7, got = 0; | ||
EXPECT_NO_THROW(got = mp::top_catch_all("", std::plus<int>{}, a, b);); | ||
EXPECT_EQ(got, a + b); | ||
} | ||
|
||
TEST_F(TopCatchAll, handles_unknown_error) | ||
luis4a0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
int got = 0; | ||
|
||
EXPECT_CALL(*mock_logger, | ||
log(Eq(mpl::Level::error), make_category_matcher(), make_cstring_matcher(HasSubstr("unknown")))); | ||
EXPECT_NO_THROW(got = mp::top_catch_all(category, [] { | ||
throw 123; | ||
return 0; | ||
});); | ||
EXPECT_EQ(got, EXIT_FAILURE); | ||
} | ||
|
||
TEST_F(TopCatchAll, handles_standard_exception) | ||
{ | ||
int got = 0; | ||
const std::string emsg = "some error"; | ||
const auto msg_matcher = AllOf(HasSubstr("exception"), HasSubstr(emsg.c_str())); | ||
|
||
EXPECT_CALL(*mock_logger, log(Eq(mpl::Level::error), make_category_matcher(), make_cstring_matcher(msg_matcher))); | ||
EXPECT_NO_THROW(got = mp::top_catch_all(category, [&emsg] { | ||
throw std::runtime_error{emsg}; | ||
return 0; | ||
});); | ||
EXPECT_EQ(got, EXIT_FAILURE); | ||
} | ||
|
||
TEST_F(TopCatchAll, handles_custom_exception) | ||
{ | ||
int got = 0; | ||
const auto msg_matcher = AllOf(HasSubstr("exception"), HasSubstr(CustomExceptionForTesting::msg)); | ||
|
||
EXPECT_CALL(*mock_logger, log(Eq(mpl::Level::error), make_category_matcher(), make_cstring_matcher(msg_matcher))); | ||
EXPECT_NO_THROW(got = mp::top_catch_all(category, [] { | ||
throw CustomExceptionForTesting{}; | ||
return 42; | ||
})); | ||
EXPECT_EQ(got, EXIT_FAILURE); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.