Skip to content

Commit

Permalink
More detailed error message for SystemError exceptions (#6756)
Browse files Browse the repository at this point in the history
* a more detailed error message for SystemError exceptions

* update error message

* fix windows message
  • Loading branch information
ironage authored and nicola-cab committed Jul 12, 2023
1 parent ee448fb commit f31cd9b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

### Internals
* Prebuilt binaries for non-Apple platforms are no longer published as nothing was using them ([PR #6746](https://github.com/realm/realm-core/pull/6746)).
* SystemError exceptions now have a more detailed error message. ([#6739](https://github.com/realm/realm-core/issues/6739))

----------------------------------------------

Expand Down
5 changes: 3 additions & 2 deletions src/realm/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,14 @@ class FileAccessError : public RuntimeError {

struct SystemError : RuntimeError {
SystemError(std::error_code err, std::string_view msg)
: RuntimeError(ErrorCodes::SystemError, msg)
: RuntimeError(ErrorCodes::SystemError,
util::format("%1 (SystemError %2: %3)", msg, err.value(), err.message()))
{
const_cast<Status&>(to_status()).set_std_error_code(err);
}

SystemError(int err_no, std::string_view msg)
: SystemError(std::error_code(err_no, std::system_category()), msg)
: SystemError(std::error_code(err_no, std::generic_category()), msg)
{
}

Expand Down
11 changes: 7 additions & 4 deletions test/object-store/sync/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2771,7 +2771,8 @@ TEST_CASE("app: sync integration", "[sync][app][baas]") {
sync_error_handler_called.store(true);
REQUIRE(error.get_system_error() ==
sync::make_error_code(realm::sync::ProtocolError::bad_authentication));
REQUIRE(error.reason() == "Unable to refresh the user access token.");
REQUIRE_THAT(std::string(error.reason()),
Catch::Matchers::ContainsSubstring("Unable to refresh the user access token."));
};
auto r = Realm::get_shared_realm(config);
timed_wait_for([&] {
Expand Down Expand Up @@ -2871,7 +2872,8 @@ TEST_CASE("app: sync integration", "[sync][app][baas]") {
sync_error_handler_called.store(true);
REQUIRE(error.get_system_error() ==
sync::make_error_code(realm::sync::ProtocolError::bad_authentication));
REQUIRE(error.reason() == "Unable to refresh the user access token.");
REQUIRE_THAT(std::string(error.reason()),
Catch::Matchers::ContainsSubstring("Unable to refresh the user access token."));
};

auto transport = static_cast<SynchronousTestTransport*>(session.transport());
Expand Down Expand Up @@ -3101,8 +3103,9 @@ TEST_CASE("app: sync integration", "[sync][app][baas]") {

auto error = wait_for_future(std::move(pf.future), std::chrono::minutes(5)).get();
REQUIRE(error.get_system_error() == make_error_code(sync::ProtocolError::limits_exceeded));
REQUIRE(error.reason() == "Sync websocket closed because the server received a message that was too large: "
"read limited at 16777217 bytes");
REQUIRE_THAT(std::string(error.reason()),
Catch::Matchers::ContainsSubstring("Sync websocket closed because the server received a message "
"that was too large: read limited at 16777217 bytes"));
REQUIRE(error.is_client_reset_requested());
REQUIRE(error.server_requests_action == sync::ProtocolErrorInfo::Action::ClientReset);
}
Expand Down
13 changes: 13 additions & 0 deletions test/test_util_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,17 @@ TEST(Utils_File_Lock)
CHECK_NOT(f2.try_rw_lock_exclusive());
}

TEST(Utils_File_SystemErrorMessage)
{
std::error_code err = std::make_error_code(std::errc::too_many_files_open);
std::string_view message = "my message";
#ifdef _WIN32
std::string expected = util::format("%1 (SystemError %2: %3)", message, err.value(), "too many files open");
#else
std::string expected = util::format("%1 (SystemError %2: %3)", message, err.value(), "Too many open files");
#endif
CHECK_THROW_CONTAINING_MESSAGE(throw SystemError(err, message), expected);
CHECK_THROW_CONTAINING_MESSAGE(throw SystemError(err.value(), message), expected);
}

#endif

0 comments on commit f31cd9b

Please sign in to comment.