Skip to content

Commit

Permalink
RequestFailedException: decouple Message and what(). (#3201)
Browse files Browse the repository at this point in the history
Co-authored-by: Anton Kolesnyk <[email protected]>
  • Loading branch information
antkmsft and antkmsft authored Dec 18, 2021
1 parent 33aca0c commit b4fe751
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 39 deletions.
6 changes: 2 additions & 4 deletions sdk/core/azure-core/inc/azure/core/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ namespace Azure { namespace Core {
/**
* @brief Constructs an `OperationCancelledException` with message string as the description.
*
* @param whatArg The explanatory string.
* @param what The explanatory string.
*/
explicit OperationCancelledException(std::string const& whatArg) : std::runtime_error(whatArg)
{
}
explicit OperationCancelledException(std::string const& what) : std::runtime_error(what) {}
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ namespace Azure { namespace Core { namespace Credentials {
* @brief An exception that gets thrown when an authentication error occurs.
*/
class AuthenticationException final : public std::exception {
std::string m_whatArg;
std::string m_what;

public:
/**
* @brief Constructs `%AuthenticationException` with a message string.
*
* @param whatArg The explanatory string.
* @param what The explanatory string.
*/
explicit AuthenticationException(std::string whatArg) : m_whatArg(std::move(whatArg)) {}
explicit AuthenticationException(std::string what) : m_what(std::move(what)) {}

/**
* Gets the explanatory string.
Expand All @@ -115,6 +115,6 @@ namespace Azure { namespace Core { namespace Credentials {
*
* @return C string with explanatory information.
*/
char const* what() const noexcept override { return m_whatArg.c_str(); }
char const* what() const noexcept override { return m_what.c_str(); }
};
}}} // namespace Azure::Core::Credentials
10 changes: 5 additions & 5 deletions sdk/core/azure-core/inc/azure/core/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ namespace Azure { namespace Core {
* @note An Exception without an HTTP raw response represents an exception that happened
* before sending the request to the server.
*
* @param whatArg The explanatory string.
* @param what The explanatory string.
*/
explicit RequestFailedException(std::string const& whatArg) : std::runtime_error(whatArg) {}
explicit RequestFailedException(std::string const& what) : std::runtime_error(what) {}

/**
* @brief Constructs a new `%RequestFailedException` object with an HTTP raw response.
Expand All @@ -85,12 +85,12 @@ namespace Azure { namespace Core {
* Service exception which derives from this exception uses its constructor to parse the HTTP
* raw response adding the service specific values to the exception.
*
* @param message The error description.
* @param what The explanatory string.
* @param rawResponse The HTTP raw response from the service.
*/
explicit RequestFailedException(
const std::string& message,
std::unique_ptr<Azure::Core::Http::RawResponse> rawResponse);
const std::string& what,
std::unique_ptr<Azure::Core::Http::RawResponse>& rawResponse);

/**
* @brief Constructs a new `%RequestFailedException` object with an HTTP raw response.
Expand Down
5 changes: 2 additions & 3 deletions sdk/core/azure-core/inc/azure/core/http/http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ namespace Azure { namespace Core { namespace Http {
* @remark The transport policy will throw this error whenever the transport adapter fail to
* perform a request.
*
* @param whatArg The explanatory string.
* @param what The explanatory string.
*/
explicit TransportException(std::string const& whatArg)
: Azure::Core::RequestFailedException(whatArg)
explicit TransportException(std::string const& what) : Azure::Core::RequestFailedException(what)
{
}
};
Expand Down
26 changes: 9 additions & 17 deletions sdk/core/azure-core/src/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,11 @@ using namespace Azure::Core::Http::_internal;
namespace Azure { namespace Core {

RequestFailedException::RequestFailedException(
const std::string& message,
std::unique_ptr<Azure::Core::Http::RawResponse> rawResponse)
: std::runtime_error(message), Message(message)
{
auto const& headers = rawResponse->GetHeaders();

StatusCode = rawResponse->GetStatusCode();
ReasonPhrase = rawResponse->GetReasonPhrase();
RequestId = HttpShared::GetHeaderOrEmptyString(headers, HttpShared::MsRequestId);
ClientRequestId = HttpShared::GetHeaderOrEmptyString(headers, HttpShared::MsClientRequestId);
Message = message;
RawResponse = std::move(rawResponse);
}

RequestFailedException::RequestFailedException(
const std::string& what,
std::unique_ptr<Azure::Core::Http::RawResponse>& rawResponse)
: std::runtime_error("Received an HTTP unsuccessful status code.")
: std::runtime_error(what)
{
auto& headers = rawResponse->GetHeaders();
const auto& headers = rawResponse->GetHeaders();

// These are guaranteed to always be present in the rawResponse.
StatusCode = rawResponse->GetStatusCode();
Expand All @@ -49,6 +35,12 @@ namespace Azure { namespace Core {
RequestId = HttpShared::GetHeaderOrEmptyString(headers, HttpShared::MsRequestId);
}

RequestFailedException::RequestFailedException(
std::unique_ptr<Azure::Core::Http::RawResponse>& rawResponse)
: RequestFailedException("Received an HTTP unsuccessful status code.", rawResponse)
{
}

std::string RequestFailedException::GetRawResponseField(
std::unique_ptr<Azure::Core::Http::RawResponse>& rawResponse,
std::string fieldName)
Expand Down
8 changes: 4 additions & 4 deletions sdk/core/azure-core/test/ut/exception_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ TEST(RequestFailedException, Message)
response->SetBodyStream(std::make_unique<Azure::Core::IO::MemoryBodyStream>(
responseBodyStream, sizeof(responseBodyStream) - 1));

auto exception = Azure::Core::RequestFailedException("Msg", std::move(response));
auto exception = Azure::Core::RequestFailedException("what", response);

EXPECT_EQ(exception.StatusCode, Azure::Core::Http::HttpStatusCode::ServiceUnavailable);
EXPECT_EQ(exception.Message, "Msg");
EXPECT_EQ(exception.ErrorCode, "");
EXPECT_EQ(exception.Message, "JT");
EXPECT_EQ(exception.ErrorCode, "503");
EXPECT_EQ(exception.RequestId, "1");
EXPECT_EQ(exception.ClientRequestId, "2");
EXPECT_EQ(exception.ReasonPhrase, "retry please :");
EXPECT_EQ(exception.what(), std::string("Msg"));
EXPECT_EQ(exception.what(), std::string("what"));
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ namespace Azure { namespace Storage {
/**
* @brief Constructs a #StorageException with a message.
*
* @param whatArg The explanatory string.
* @param what The explanatory string.
*/
explicit StorageException(const std::string& whatArg) : RequestFailedException(whatArg) {}
explicit StorageException(const std::string& what) : RequestFailedException(what) {}

/**
* Some storage-specific information in response body.
Expand Down

0 comments on commit b4fe751

Please sign in to comment.