Skip to content

Commit

Permalink
Better error notification
Browse files Browse the repository at this point in the history
  • Loading branch information
xfangfang committed Nov 15, 2023
1 parent d53373c commit 960c043
Show file tree
Hide file tree
Showing 24 changed files with 106 additions and 94 deletions.
4 changes: 2 additions & 2 deletions wiliwili/include/api/bilibili.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ class SearchSuggestList;
class WatchLaterListWrapper;

using Cookies = std::map<std::string, std::string>;
using ErrorCallback = std::function<void(const std::string&)>;
using ErrorCallback = std::function<void(const std::string&, int code)>;

#define BILI bilibili::BilibiliClient
#define BILI_ERR const std::string& error
#define BILI_ERR const std::string& error, int code

class BilibiliClient {
inline static std::function<void(Cookies, std::string)>
Expand Down
5 changes: 4 additions & 1 deletion wiliwili/include/api/bilibili/result/video_detail_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ inline void from_json(const nlohmann::json& nlohmann_json_j,
} else {
nlohmann_json_t.upper = 0;
}
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, cursor, root));
if (nlohmann_json_j.contains("root") && !nlohmann_json_j.at("root").is_null()) {
nlohmann_json_j.at("root").get_to(nlohmann_json_t.root);
}
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, cursor));
}

class VideoCommentAddResult {
Expand Down
55 changes: 28 additions & 27 deletions wiliwili/include/api/bilibili/util/http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const std::string BILIBILI_APP_KEY = "aa1e74ee4874176e";
const std::string BILIBILI_APP_SECRET = "54e6a9a31b911cd5fc0daa66ebf94bc4";
const std::string BILIBILI_BUILD = "1001011000";

using ErrorCallback = std::function<void(const std::string&)>;
using ErrorCallback = std::function<void(const std::string&, int code)>;
#define ERROR_MSG(msg, ...) \
if (error) error(msg)
if (error) error(msg, __VA_ARGS__)
#ifdef CALLBACK
#undef CALLBACK
#endif
Expand Down Expand Up @@ -50,10 +50,12 @@ class HTTP {
const ErrorCallback& error = nullptr) {
cpr::PostCallback(
[callback, error](const cpr::Response& r) {
if (r.status_code != 200) {
if (r.status_code == 0) {
ERROR_MSG("No network connection", -1);
} else if (r.status_code != 200) {
ERROR_MSG("Network error. [Status code: " +
std::to_string(r.status_code) + " ]",
-404);
r.status_code);
return;
}
callback(r);
Expand All @@ -73,10 +75,12 @@ class HTTP {
const ErrorCallback& error = nullptr) {
cpr::GetCallback(
[callback, error](const cpr::Response& r) {
if (r.status_code != 200) {
if (r.status_code == 0) {
ERROR_MSG("No network connection", -1);
} else if (r.status_code != 200) {
ERROR_MSG("Network error. [Status code: " +
std::to_string(r.status_code) + " ]",
-404);
r.status_code);
return;
}
callback(r);
Expand Down Expand Up @@ -114,31 +118,33 @@ class HTTP {
nlohmann::json res = nlohmann::json::parse(r.text);
int code = res.at("code").get<int>();
if (code == 0) {
if (res.contains("data")) {
if (res.contains("data") &&
res.at("data").is_object()) {
CALLBACK(res.at("data").get<ReturnType>());
} else if (res.contains("result")) {
} else if (res.contains("result") &&
res.at("result").is_object()) {
CALLBACK(res.at("result").get<ReturnType>());
} else {
printf("data: %s\n", r.text.c_str());
ERROR_MSG("Cannot find data");
ERROR_MSG("Cannot find data", -1);
}
return;
}

if (res.at("message").is_string()) {
ERROR_MSG("error msg: " +
res.at("message").get<std::string>() +
"; error code: " + std::to_string(code));
ERROR_MSG(res.at("message").get<std::string>(), code);
} else {
ERROR_MSG("Param error");
ERROR_MSG("Param error", -1);
}
} catch (const std::exception& e) {
if (r.status_code == 200) {
ERROR_MSG("Api error. \n" + std::string{e.what()}, 200);
} else if (r.status_code == 0) {
ERROR_MSG("No network connection", -1);
} else {
ERROR_MSG("Network error. \nStatus code: " +
std::to_string(r.status_code),
-404);
r.status_code);
}
printf("data: %s\n", r.text.c_str());
printf("ERROR: %s\n", e.what());
Expand Down Expand Up @@ -172,21 +178,19 @@ class HTTP {
nlohmann::json res = nlohmann::json::parse(r.text);
int code = res.at("code").get<int>();
if (code == 0) {
if (res.contains("data")) {
if (res.contains("data") && res.at("data").is_object()) {
CALLBACK(res.at("data").get<ReturnType>());
} else if (res.contains("result")) {
} else if (res.contains("result") && res.at("result").is_object()) {
CALLBACK(res.at("result").get<ReturnType>());
} else {
ERROR_MSG("");
printf("data: %s\n", r.text.c_str());
ERROR_MSG("Cannot find data", -1);
}
return;
}
ERROR_MSG("code: " + std::to_string(code) +
"; msg: " + res.at("message").get<std::string>());
ERROR_MSG(res.at("message").get<std::string>(), code);
} catch (const std::exception& e) {
ERROR_MSG(
"API error: code: " + std::to_string(r.status_code) +
"; msg: " + std::string(e.what()));
ERROR_MSG(std::string(e.what()), r.status_code);
printf("data: %s\n", r.text.c_str());
printf("ERROR: %s\n", e.what());
}
Expand All @@ -209,12 +213,9 @@ class HTTP {
if (callback) callback();
return;
}
ERROR_MSG("code: " + std::to_string(code) +
"; msg: " + res.at("message").get<std::string>());
ERROR_MSG(res.at("message").get<std::string>(), code);
} catch (const std::exception& e) {
ERROR_MSG(
"API error: code: " + std::to_string(r.status_code) +
"; msg: " + std::string(e.what()));
ERROR_MSG(std::string(e.what()), r.status_code);
printf("data: %s\n", r.text.c_str());
printf("ERROR: %s\n", e.what());
}
Expand Down
12 changes: 6 additions & 6 deletions wiliwili/include/presenter/user_home.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,35 @@ class UserHome {
}

void getUserInfo() {
bilibili::BilibiliClient::get_my_info(
BILI::get_my_info(
[this](const bilibili::UserResult& data) {
this->userInfo = data;
this->onUserInfo(this->userInfo);
},
[this](const std::string& error) {
[this](BILI_ERR) {
brls::Logger::error("getUserInfo: {}", error);
this->onUserNotLogin();
});
}

void getUserDynamicStat(const std::string& mid) {
bilibili::BilibiliClient::get_user_dynamic_count(
BILI::get_user_dynamic_count(
mid,
[this](const bilibili::UserDynamicCount& data) {
this->onUserDynamicStat(data);
},
[](const std::string& error) {
[](BILI_ERR) {
brls::Logger::error("getUserDynamicStat: {}", error);
});
}

void getUserRelationStat(const std::string& mid) {
bilibili::BilibiliClient::get_user_relation(
BILI::get_user_relation(
mid,
[this](const bilibili::UserRelationStat& data) {
this->onUserRelationStat(data);
},
[](const std::string& error) {
[](BILI_ERR) {
brls::Logger::error("getUserRelationStat: {}", error);
});
}
Expand Down
4 changes: 2 additions & 2 deletions wiliwili/source/api/mine_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void BilibiliClient::get_login_info(
if (callback) callback(data.data);
return;
} catch (const std::exception& e) {
ERROR_MSG("API error");
ERROR_MSG("API error", -1);
printf("data: %s\n", r.text.c_str());
printf("ERROR: %s\n", e.what());
}
Expand Down Expand Up @@ -112,7 +112,7 @@ void BilibiliClient::get_login_info_v2(
if (callback) callback(data.data);
return;
} catch (const std::exception& e) {
ERROR_MSG("API error");
ERROR_MSG("API error", -1);
printf("data: %s\n", r.text.c_str());
printf("ERROR: %s\n", e.what());
}
Expand Down
8 changes: 4 additions & 4 deletions wiliwili/source/api/video_detail_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ void BilibiliClient::get_live_pay_info(
ret.message = res.at("message").get<std::string>();
CALLBACK(ret);
} catch (const std::exception& e) {
ERROR_MSG("cannot get live pay info");
ERROR_MSG("cannot get live pay info", -1);
}
}, error);
}
Expand Down Expand Up @@ -298,7 +298,7 @@ void BilibiliClient::get_danmaku(
} catch (const std::exception& e) {
ERROR_MSG("Network error. [Status code: " +
std::to_string(r.status_code) + " ]",
-404);
r.status_code);
printf("data: %s\n", r.text.c_str());
printf("ERROR: %s\n", e.what());
}
Expand Down Expand Up @@ -327,7 +327,7 @@ void BilibiliClient::get_subtitle(
} catch (const std::exception& e) {
ERROR_MSG("Network error. [Status code: " +
std::to_string(r.status_code) + " ]",
-404);
r.status_code);
printf("data: %s\n", r.text.c_str());
printf("ERROR: %s\n", e.what());
}
Expand Down Expand Up @@ -461,7 +461,7 @@ void BilibiliClient::add_comment(
Api::CommentAdd, {}, payload,
[callback, error](const VideoCommentAddResult& result) {
if (result.success_action != 0) {
ERROR_MSG("cannot add comment");
ERROR_MSG("cannot add comment", -1);
} else {
CALLBACK(result);
}
Expand Down
4 changes: 2 additions & 2 deletions wiliwili/source/fragment/mine_qr_login.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ void MineQrLogin::onLoginError() {

void MineQrLogin::getLoginUrl() {
ASYNC_RETAIN
bilibili::BilibiliClient::get_login_url_v2(
BILI::get_login_url_v2(
[ASYNC_TOKEN](const std::string& url, const std::string& key) {
ASYNC_RELEASE
this->oauthKey = key;
this->login_url = url;
this->onLoginUrlChange(url);
this->checkLogin();
},
[this](const std::string& error) { this->onError(); });
[this](BILI_ERR) { this->onError(); });
}

void MineQrLogin::checkLogin() {
Expand Down
2 changes: 1 addition & 1 deletion wiliwili/source/fragment/player_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void PlayerCollection::getCollectionList(int rid, int type) {
this->onCollectionList(result);
});
},
[ASYNC_TOKEN](const std::string& error) {
[ASYNC_TOKEN](BILI_ERR) {
brls::Logger::error("{}", error);
brls::sync([ASYNC_TOKEN, error]() {
ASYNC_RELEASE
Expand Down
4 changes: 2 additions & 2 deletions wiliwili/source/fragment/search_cinema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void SearchCinema::requestSearch(const std::string& key) {

void SearchCinema::_requestSearch(const std::string& key) {
ASYNC_RETAIN
bilibili::BilibiliClient::search_video(
BILI::search_video(
key, "media_ft", requestIndex, "",
[ASYNC_TOKEN](const bilibili::SearchResult& result) {
for (auto i : result.result) {
Expand Down Expand Up @@ -65,7 +65,7 @@ void SearchCinema::_requestSearch(const std::string& key) {
this->requestIndex = result.page + 1;
});
},
[ASYNC_TOKEN](const std::string& error) {
[ASYNC_TOKEN](BILI_ERR) {
brls::Logger::error("SearchCinema: {}", error);
brls::sync([ASYNC_TOKEN, error]() {
ASYNC_RELEASE
Expand Down
4 changes: 2 additions & 2 deletions wiliwili/source/fragment/search_video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void SearchVideo::requestSearch(const std::string& key) {

void SearchVideo::_requestSearch(const std::string& key) {
ASYNC_RETAIN
bilibili::BilibiliClient::search_video(
BILI::search_video(
key, "video", requestIndex, "",
[ASYNC_TOKEN](const bilibili::SearchResult& result) {
for (auto i : result.result) {
Expand Down Expand Up @@ -66,7 +66,7 @@ void SearchVideo::_requestSearch(const std::string& key) {
this->requestIndex = result.page + 1;
});
},
[ASYNC_TOKEN](const std::string error) {
[ASYNC_TOKEN](BILI_ERR) {
brls::Logger::error("SearchVideo: {}", error);
brls::sync([ASYNC_TOKEN, error]() {
ASYNC_RELEASE
Expand Down
8 changes: 4 additions & 4 deletions wiliwili/source/fragment/setting_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ SettingNetwork::SettingNetwork() {

void SettingNetwork::networkTest() {
ASYNC_RETAIN
bilibili::BilibiliClient::get_recommend(
BILI::get_recommend(
1, 1, 0, "V1", 3, 4,
[ASYNC_TOKEN](const auto& result) {
brls::sync([ASYNC_TOKEN]() {
Expand All @@ -47,7 +47,7 @@ void SettingNetwork::networkTest() {
this->labelTest1->setText("hints/success"_i18n);
});
},
[ASYNC_TOKEN](const std::string& error) {
[ASYNC_TOKEN](BILI_ERR) {
brls::sync([ASYNC_TOKEN]() {
ASYNC_RELEASE
this->labelTest1->setTextColor(nvgRGB(199, 84, 80));
Expand All @@ -63,14 +63,14 @@ void SettingNetwork::getUnixTime() {

// 获取网络时间
ASYNC_RETAIN
bilibili::BilibiliClient::get_unix_time(
BILI::get_unix_time(
[ASYNC_TOKEN](const bilibili::UnixTimeResult& result) {
brls::sync([ASYNC_TOKEN, result]() {
ASYNC_RELEASE
this->labelNetTime->setText(wiliwili::sec2FullDate(result.now));
});
},
[ASYNC_TOKEN](const std::string& error) {
[ASYNC_TOKEN](BILI_ERR) {
brls::sync([ASYNC_TOKEN]() {
ASYNC_RELEASE
this->labelNetTime->setTextColor(nvgRGB(199, 84, 80));
Expand Down
10 changes: 7 additions & 3 deletions wiliwili/source/presenter/dynamic_tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ void DynamicTabRequest::requestUpList() {
return;
}
CHECK_AND_SET_REQUEST
bilibili::BilibiliClient::dynamic_up_list(
BILI::dynamic_up_list(
[this](const bilibili::DynamicUpListResultWrapper &result) {
this->onUpList(result);
UNSET_REQUEST
},
[this](const std::string &error) {
this->onError(error);
[this](BILI_ERR) {
if (code == -6) {
this->onError("账号未登录");
} else {
this->onError(error);
}
UNSET_REQUEST
});
}
Loading

0 comments on commit 960c043

Please sign in to comment.