Commit 28632273 authored by estade's avatar estade Committed by Commit bot

handle more http error codes in wallet response

BUG=409867

Review URL: https://codereview.chromium.org/522023003

Cr-Commit-Position: refs/heads/master@{#293370}
parent 733d7aba
......@@ -578,9 +578,20 @@ void WalletClient::OnURLFetchComplete(
HandleWalletError(BAD_REQUEST);
return;
}
// HTTP_OK holds a valid response and HTTP_INTERNAL_SERVER_ERROR holds an
// error code and message for the user.
case net::HTTP_OK:
// Valid response.
case net::HTTP_OK: {
scoped_ptr<base::Value> message_value(base::JSONReader::Read(data));
if (message_value.get() &&
message_value->IsType(base::Value::TYPE_DICTIONARY)) {
response_dict.reset(
static_cast<base::DictionaryValue*>(message_value.release()));
}
break;
}
// Response contains an error to show the user.
case net::HTTP_FORBIDDEN:
case net::HTTP_INTERNAL_SERVER_ERROR: {
scoped_ptr<base::Value> message_value(base::JSONReader::Read(data));
if (message_value.get() &&
......@@ -588,7 +599,7 @@ void WalletClient::OnURLFetchComplete(
response_dict.reset(
static_cast<base::DictionaryValue*>(message_value.release()));
}
if (response_code == net::HTTP_INTERNAL_SERVER_ERROR) {
request_type_ = NO_REQUEST;
std::string error_type_string;
......@@ -596,8 +607,7 @@ void WalletClient::OnURLFetchComplete(
HandleWalletError(UNKNOWN_ERROR);
return;
}
WalletClient::ErrorType error_type =
StringToErrorType(error_type_string);
WalletClient::ErrorType error_type = StringToErrorType(error_type_string);
if (error_type == BUYER_ACCOUNT_ERROR) {
// If the error_type is |BUYER_ACCOUNT_ERROR|, then
// message_type_for_buyer field contains more specific information
......@@ -605,18 +615,16 @@ void WalletClient::OnURLFetchComplete(
std::string message_type_for_buyer_string;
if (response_dict->GetString(kMessageTypeForBuyerKey,
&message_type_for_buyer_string)) {
error_type = BuyerErrorStringToErrorType(
message_type_for_buyer_string);
error_type =
BuyerErrorStringToErrorType(message_type_for_buyer_string);
}
}
HandleWalletError(error_type);
return;
}
break;
}
// Anything else is an error.
// Handle anything else as a generic error.
default:
request_type_ = NO_REQUEST;
HandleWalletError(NETWORK_ERROR);
......
......@@ -281,6 +281,30 @@ const char kErrorResponse[] =
" }"
"}";
const char kErrorResponseSpendingLimitExceeded[] =
"{"
" \"error_type\":\"APPLICATION_ERROR\","
" \"error_detail\":\"error_detail\","
" \"application_error\":\"application_error\","
" \"debug_data\":"
" {"
" \"debug_message\":\"debug_message\","
" \"stack_trace\":\"stack_trace\""
" },"
" \"application_error_data\":\"application_error_data\","
" \"wallet_error\":"
" {"
" \"error_type\":\"SPENDING_LIMIT_EXCEEDED\","
" \"error_detail\":\"error_detail\","
" \"message_for_user\":"
" {"
" \"text\":\"text\","
" \"subtext\":\"subtext\","
" \"details\":\"details\""
" }"
" }"
"}";
const char kErrorTypeMissingInResponse[] =
"{"
" \"error_type\":\"Not APPLICATION_ERROR\","
......@@ -1755,7 +1779,8 @@ TEST_F(WalletClientTest, HasRequestInProgress) {
EXPECT_FALSE(wallet_client_->HasRequestInProgress());
}
TEST_F(WalletClientTest, ErrorResponse) {
// 500 (INTERNAL_SERVER_ERROR) - response json is parsed.
TEST_F(WalletClientTest, ErrorResponse500) {
EXPECT_FALSE(wallet_client_->HasRequestInProgress());
delegate_.ExpectBaselineMetrics();
wallet_client_->GetWalletItems(base::string16(), base::string16());
......@@ -1774,6 +1799,60 @@ TEST_F(WalletClientTest, ErrorResponse) {
kErrorResponse);
}
// 403 (FORBIDDEN) - response json is parsed.
TEST_F(WalletClientTest, ErrorResponse403) {
EXPECT_FALSE(wallet_client_->HasRequestInProgress());
delegate_.ExpectBaselineMetrics();
wallet_client_->GetWalletItems(base::string16(), base::string16());
EXPECT_TRUE(wallet_client_->HasRequestInProgress());
testing::Mock::VerifyAndClear(delegate_.metric_logger());
EXPECT_CALL(delegate_, OnWalletError(WalletClient::SPENDING_LIMIT_EXCEEDED))
.Times(1);
delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
1);
delegate_.ExpectWalletErrorMetric(
AutofillMetrics::WALLET_SPENDING_LIMIT_EXCEEDED);
VerifyAndFinishRequest(net::HTTP_FORBIDDEN,
kGetWalletItemsValidRequest,
kErrorResponseSpendingLimitExceeded);
}
// 400 (BAD_REQUEST) - response json is ignored.
TEST_F(WalletClientTest, ErrorResponse400) {
EXPECT_FALSE(wallet_client_->HasRequestInProgress());
delegate_.ExpectBaselineMetrics();
wallet_client_->GetWalletItems(base::string16(), base::string16());
EXPECT_TRUE(wallet_client_->HasRequestInProgress());
testing::Mock::VerifyAndClear(delegate_.metric_logger());
EXPECT_CALL(delegate_, OnWalletError(WalletClient::BAD_REQUEST)).Times(1);
delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
1);
delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_BAD_REQUEST);
VerifyAndFinishRequest(
net::HTTP_BAD_REQUEST, kGetWalletItemsValidRequest, kErrorResponse);
}
// Anything else - response json is ignored.
TEST_F(WalletClientTest, ErrorResponseOther) {
EXPECT_FALSE(wallet_client_->HasRequestInProgress());
delegate_.ExpectBaselineMetrics();
wallet_client_->GetWalletItems(base::string16(), base::string16());
EXPECT_TRUE(wallet_client_->HasRequestInProgress());
testing::Mock::VerifyAndClear(delegate_.metric_logger());
EXPECT_CALL(delegate_, OnWalletError(WalletClient::NETWORK_ERROR)).Times(1);
delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
1);
delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_NETWORK_ERROR);
VerifyAndFinishRequest(
net::HTTP_NOT_FOUND, kGetWalletItemsValidRequest, kErrorResponse);
}
TEST_F(WalletClientTest, CancelRequest) {
EXPECT_FALSE(wallet_client_->HasRequestInProgress());
delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment