summaryrefslogtreecommitdiff
path: root/chromium/components/cryptauth/cryptauth_api_call_flow.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/components/cryptauth/cryptauth_api_call_flow.cc')
-rw-r--r--chromium/components/cryptauth/cryptauth_api_call_flow.cc30
1 files changed, 21 insertions, 9 deletions
diff --git a/chromium/components/cryptauth/cryptauth_api_call_flow.cc b/chromium/components/cryptauth/cryptauth_api_call_flow.cc
index 40023c46251..4f6f17cb0f8 100644
--- a/chromium/components/cryptauth/cryptauth_api_call_flow.cc
+++ b/chromium/components/cryptauth/cryptauth_api_call_flow.cc
@@ -4,6 +4,7 @@
#include "components/cryptauth/cryptauth_api_call_flow.h"
+#include "base/optional.h"
#include "base/strings/string_number_conversions.h"
#include "chromeos/components/proximity_auth/logging/logging.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
@@ -13,9 +14,21 @@ namespace cryptauth {
namespace {
-const char kResponseBodyError[] = "Failed to get response body";
-const char kRequestFailedError[] = "Request failed";
-const char kHttpStatusErrorPrefix[] = "HTTP status: ";
+NetworkRequestError GetErrorForHttpResponseCode(int response_code) {
+ if (response_code == 400)
+ return NetworkRequestError::kBadRequest;
+
+ if (response_code == 403)
+ return NetworkRequestError::kAuthenticationError;
+
+ if (response_code == 404)
+ return NetworkRequestError::kEndpointNotFound;
+
+ if (response_code >= 500 && response_code < 600)
+ return NetworkRequestError::kInternalServerError;
+
+ return NetworkRequestError::kUnknown;
+}
} // namespace
@@ -59,7 +72,7 @@ void CryptAuthApiCallFlow::ProcessApiCallSuccess(
const net::URLFetcher* source) {
std::string serialized_response;
if (!source->GetResponseAsString(&serialized_response)) {
- error_callback_.Run(kResponseBodyError);
+ error_callback_.Run(NetworkRequestError::kResponseMalformed);
return;
}
result_callback_.Run(serialized_response);
@@ -67,18 +80,17 @@ void CryptAuthApiCallFlow::ProcessApiCallSuccess(
void CryptAuthApiCallFlow::ProcessApiCallFailure(
const net::URLFetcher* source) {
- std::string error_message;
+ base::Optional<NetworkRequestError> error;
if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS) {
- error_message =
- kHttpStatusErrorPrefix + base::IntToString(source->GetResponseCode());
+ error = GetErrorForHttpResponseCode(source->GetResponseCode());
} else {
- error_message = kRequestFailedError;
+ error = NetworkRequestError::kOffline;
}
std::string response;
source->GetResponseAsString(&response);
PA_LOG(INFO) << "API call failed:\n" << response;
- error_callback_.Run(error_message);
+ error_callback_.Run(*error);
}
net::PartialNetworkTrafficAnnotationTag