Handle Google Drive get file errors.

When a call to translate a file ID to a URL fails, tell the user and
don't try to load the file.
This commit is contained in:
Jim Broadus 2020-02-14 23:20:41 -08:00 committed by John Maguire
parent 06ed4a8232
commit f81533bb58
4 changed files with 12 additions and 2 deletions

View File

@ -72,7 +72,7 @@ QStringList File::parent_ids() const {
ConnectResponse::ConnectResponse(QObject* parent) : QObject(parent) {}
GetFileResponse::GetFileResponse(const QString& file_id, QObject* parent)
: QObject(parent), file_id_(file_id) {}
: QObject(parent), file_id_(file_id), had_error_(false) {}
ListChangesResponse::ListChangesResponse(const QString& cursor, QObject* parent)
: QObject(parent), cursor_(cursor) {}
@ -165,6 +165,7 @@ void Client::GetFileFinished(GetFileResponse* response, QNetworkReply* reply) {
QJsonDocument document = QJsonDocument::fromJson(reply->readAll(), &error);
if (error.error != QJsonParseError::NoError) {
qLog(Error) << "Failed to fetch file with ID" << response->file_id_;
response->had_error_ = true;
emit response->Finished();
return;
}

View File

@ -103,6 +103,7 @@ class GetFileResponse : public QObject {
public:
const QString& file_id() const { return file_id_; }
const File& file() const { return file_; }
bool had_error() { return had_error_; }
signals:
void Finished();
@ -111,6 +112,7 @@ class GetFileResponse : public QObject {
GetFileResponse(const QString& file_id, QObject* parent);
QString file_id_;
File file_;
bool had_error_;
};
class ListChangesResponse : public QObject {

View File

@ -204,6 +204,11 @@ QUrl GoogleDriveService::GetStreamingUrlFromSongId(const QString& id) {
connect(response.data(), SIGNAL(Finished()), &loop, SLOT(quit()));
loop.exec();
if (response->had_error()) {
app_->AddError(tr("Could not find Google Drive file."));
return QUrl();
}
QUrl url(response->file().download_url());
QUrlQuery url_query(url);
url_query.addQueryItem("access_token", client_->access_token());

View File

@ -27,5 +27,7 @@ GoogleDriveUrlHandler::GoogleDriveUrlHandler(GoogleDriveService* service,
UrlHandler::LoadResult GoogleDriveUrlHandler::StartLoading(const QUrl& url) {
QString file_id = url.path();
QUrl real_url = service_->GetStreamingUrlFromSongId(file_id);
return LoadResult(url, LoadResult::TrackAvailable, real_url);
LoadResult::Type type = real_url.isValid() ? LoadResult::TrackAvailable
: LoadResult::NoMoreTracks;
return LoadResult(url, type, real_url);
}