From f9f155bfbbc5439f7e247dbf3fc1d997bfaf2ec7 Mon Sep 17 00:00:00 2001
From: Piotr Kosko
Date: Tue, 3 Oct 2017 08:29:50 +0200
Subject: [PATCH] [Download] Added information about HTTP status in error
message
[Feature] Error message of onfailed callback of start method, return only
Unknown error. Now information about HTTP status was added into message.
[Verification] Checked in chrome console.
TCT passrate - 100%.
Change-Id: I157053d10a1e770c070b55465f1a7351c0c80cce
Signed-off-by: Piotr Kosko
---
src/download/download_instance.cc | 109 +++++++++++++-----------------
src/download/download_instance.h | 2 +-
2 files changed, 49 insertions(+), 62 deletions(-)
diff --git a/src/download/download_instance.cc b/src/download/download_instance.cc
index 3ea612b5..208caae3 100644
--- a/src/download/download_instance.cc
+++ b/src/download/download_instance.cc
@@ -23,9 +23,9 @@
#include "common/filesystem/filesystem_provider.h"
#include "common/logger.h"
#include "common/picojson.h"
+#include "common/scope_exit.h"
#include "common/tools.h"
#include "common/typeutil.h"
-#include "common/scope_exit.h"
namespace extension {
namespace download {
@@ -109,90 +109,69 @@ bool DownloadInstance::CheckInstance(DownloadInstance* instance) {
return false;
}
-common::PlatformResult DownloadInstance::convertError(int err) {
- char* error = get_error_message(err);
+common::PlatformResult DownloadInstance::convertError(int err, const std::string& message) {
+ char* error = nullptr;
+ if (message.empty()) {
+ error = get_error_message(err);
+ } else {
+ error = (char*)message.c_str();
+ }
+
switch (err) {
case DOWNLOAD_ERROR_INVALID_PARAMETER:
- return LogAndCreateResult(common::ErrorCode::INVALID_VALUES_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::INVALID_VALUES_ERR, error);
case DOWNLOAD_ERROR_OUT_OF_MEMORY:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_NETWORK_UNREACHABLE:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_CONNECTION_TIMED_OUT:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_NO_SPACE:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_PERMISSION_DENIED:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_NOT_SUPPORTED:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_INVALID_STATE:
- return LogAndCreateResult(common::ErrorCode::INVALID_VALUES_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::INVALID_VALUES_ERR, error);
case DOWNLOAD_ERROR_CONNECTION_FAILED:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_INVALID_URL:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_INVALID_DESTINATION:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_QUEUE_FULL:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_ALREADY_COMPLETED:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_FILE_ALREADY_EXISTS:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_CANNOT_RESUME:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_FIELD_NOT_FOUND:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_TOO_MANY_REDIRECTS:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_UNHANDLED_HTTP_CODE:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_REQUEST_TIMEOUT:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_RESPONSE_TIMEOUT:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_SYSTEM_DOWN:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_ID_NOT_FOUND:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_INVALID_NETWORK_TYPE:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_NO_DATA:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
case DOWNLOAD_ERROR_IO_ERROR:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, error,
- ("Error %d (%s)", err, error));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, error);
default:
- return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, "Unknown error.",
- ("Unknown error: %d", err));
+ return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, "Unknown error.");
}
}
@@ -432,9 +411,17 @@ gboolean DownloadInstance::OnFailed(void* user_data) {
LoggerD("OnFailed for callbackID %d called", callback_id);
download_get_error(downCbPtr->downloadId, &error);
-
if (DOWNLOAD_ERROR_NONE != error) {
- LogAndReportError(convertError(error), &out,
+ int http_status = 0;
+ int ret = download_get_http_status(downCbPtr->downloadId, &http_status);
+ std::string error_message;
+ if (DOWNLOAD_ERROR_NONE != ret) {
+ LoggerE("Gathering HTTP status failed, default error message will be used");
+ } else {
+ LoggerD("HTTP status is: %d", http_status);
+ error_message = "Error with HTTP status: " + std::to_string(http_status);
+ }
+ LogAndReportError(convertError(error, error_message), &out,
("download_get_error error: %d (%s)", error, get_error_message(error)));
}
@@ -530,8 +517,8 @@ void DownloadInstance::DownloadManagerStart(const picojson::value& args, picojso
if (CONNECTION_TYPE_DISCONNECTED == connection_type) {
LogAndReportError(
- common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, "Connection problem occured"),
- &out, ("Connection type is disconnected"));
+ common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, "Connection problem occured"), &out,
+ ("Connection type is disconnected"));
return;
}
diff --git a/src/download/download_instance.h b/src/download/download_instance.h
index a951c9e4..f628736e 100644
--- a/src/download/download_instance.h
+++ b/src/download/download_instance.h
@@ -56,7 +56,7 @@ class DownloadInstance : public common::ParsedInstance {
bool GetDownloadID(const int callback_id, int& download_id);
- static common::PlatformResult convertError(int err);
+ static common::PlatformResult convertError(int err, const std::string& message = "");
static void OnStateChanged(int download_id, download_state_e state, void* user_data);
static void progress_changed_cb(int download_id, long long unsigned received, void* user_data);
static void OnStart(int download_id, void* user_data);
--
2.34.1