RequestFailedException: decouple Message and what(). (#3201)

Co-authored-by: Anton Kolesnyk <antkmsft@users.noreply.github.com>
This commit is contained in:
Anton Kolesnyk 2021-12-18 09:00:04 -08:00 committed by GitHub
parent 33aca0caaf
commit b4fe751310
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 28 additions and 39 deletions

View File

@ -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) {}
};
/**

View File

@ -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.
@ -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

View File

@ -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.
@ -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.

View File

@ -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)
{
}
};

View File

@ -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();
@ -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)

View File

@ -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"));
}

View File

@ -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.