feat: error callback in network.get, .get_binary, .post

This commit is contained in:
MihailRis
2025-04-30 19:15:39 +03:00
parent 76fb2e42d3
commit 556a561f1c
3 changed files with 39 additions and 8 deletions
+31 -3
View File
@@ -12,11 +12,20 @@ static int l_get(lua::State* L, network::Network& network) {
lua::pushvalue(L, 2);
auto onResponse = lua::create_lambda_nothrow(L);
network::OnReject onReject = nullptr;
if (lua::gettop(L) >= 3) {
lua::pushvalue(L, 3);
auto callback = lua::create_lambda_nothrow(L);
onReject = [callback](int code) {
callback({code});
};
}
network.get(url, [onResponse](std::vector<char> bytes) {
engine->postRunnable([=]() {
onResponse({std::string(bytes.data(), bytes.size())});
});
});
}, std::move(onReject));
return 0;
}
@@ -26,6 +35,15 @@ static int l_get_binary(lua::State* L, network::Network& network) {
lua::pushvalue(L, 2);
auto onResponse = lua::create_lambda_nothrow(L);
network::OnReject onReject = nullptr;
if (lua::gettop(L) >= 3) {
lua::pushvalue(L, 3);
auto callback = lua::create_lambda_nothrow(L);
onReject = [callback](int code) {
callback({code});
};
}
network.get(url, [onResponse](std::vector<char> bytes) {
auto buffer = std::make_shared<util::Buffer<ubyte>>(
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
@@ -33,7 +51,7 @@ static int l_get_binary(lua::State* L, network::Network& network) {
engine->postRunnable([=]() {
onResponse({buffer});
});
});
}, std::move(onReject));
return 0;
}
@@ -44,12 +62,22 @@ static int l_post(lua::State* L, network::Network& network) {
lua::pushvalue(L, 3);
auto onResponse = lua::create_lambda_nothrow(L);
network::OnReject onReject = nullptr;
if (lua::gettop(L) >= 4) {
lua::pushvalue(L, 4);
auto callback = lua::create_lambda_nothrow(L);
onReject = [callback](int code) {
callback({code});
};
}
std::string string;
if (data.isString()) {
string = data.asString();
} else {
string = json::stringify(data, false);
}
engine->getNetwork().post(url, string, [onResponse](std::vector<char> bytes) {
auto buffer = std::make_shared<util::Buffer<ubyte>>(
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
@@ -57,7 +85,7 @@ static int l_post(lua::State* L, network::Network& network) {
engine->postRunnable([=]() {
onResponse({std::string(bytes.data(), bytes.size())});
});
});
}, std::move(onReject));
return 0;
}