Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maintained RpcReqError with headers in kphp #926

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 15 additions & 26 deletions runtime/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "runtime/resumable.h"
#include "runtime/string_functions.h"
#include "runtime/tl/rpc_function.h"
#include "runtime/tl/rpc_req_error.h"
#include "runtime/tl/rpc_request.h"
#include "runtime/tl/rpc_server.h"
#include "runtime/tl/rpc_tl_query.h"
Expand Down Expand Up @@ -1016,10 +1017,12 @@ bool f$store_long(int64_t v) {


int32_t tl_parse_int() {
return TRY_CALL(int32_t, int32_t, (rpc_fetch_int()));
CHECK_EXCEPTION(return 0);
return rpc_fetch_int();
}

long long tl_parse_long() {
CHECK_EXCEPTION(return 0);
TRY_CALL_VOID(int, check_rpc_data_len(2));
long long result = *reinterpret_cast<const long long *>(rpc_data);
rpc_data += 2;
Expand All @@ -1028,15 +1031,18 @@ long long tl_parse_long() {
}

double tl_parse_double() {
return TRY_CALL(double, double, (f$fetch_double()));
CHECK_EXCEPTION(return 0.0);
return f$fetch_double();
}

double tl_parse_float() {
return TRY_CALL(double, double, (f$fetch_float()));
CHECK_EXCEPTION(return 0.0);
return f$fetch_float();
}

string tl_parse_string() {
return TRY_CALL(string, string, (f$fetch_string()));
CHECK_EXCEPTION(return {});
return f$fetch_string();
}

void tl_parse_end() {
Expand Down Expand Up @@ -1065,29 +1071,12 @@ array<mixed> tl_fetch_error(const char *error, int error_code) {
long long rpc_tl_results_last_query_num = -1;

bool try_fetch_rpc_error(array<mixed> &out_if_error) {
int x = rpc_lookup_int();
if (x == TL_RPC_REQ_ERROR && CurException.is_null()) {
php_assert (tl_parse_int() == TL_RPC_REQ_ERROR);
if (CurException.is_null()) {
tl_parse_long();
if (CurException.is_null()) {
int error_code = tl_parse_int();
if (CurException.is_null()) {
string error = tl_parse_string();
if (CurException.is_null()) {
out_if_error = tl_fetch_error(error, error_code);
return true;
}
}
}
}
}
if (!CurException.is_null()) {
out_if_error = tl_fetch_error(CurException->$message, TL_ERROR_SYNTAX);
CurException = Optional<bool>{};
return true;
RpcError rpc_error;
if (!rpc_error.try_fetch()) {
return false;
}
return false;
out_if_error = tl_fetch_error(rpc_error.error_msg, rpc_error.error_code);
return true;
}

class_instance<RpcTlQuery> store_function(const mixed &tl_object) {
Expand Down
1 change: 1 addition & 0 deletions runtime/runtime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ prepend(KPHP_RUNTIME_SOURCES ${BASE_DIR}/runtime/
string_buffer.cpp
string_cache.cpp
string_functions.cpp
tl/rpc_req_error.cpp
tl/rpc_tl_query.cpp
tl/rpc_response.cpp
tl/rpc_server.cpp
Expand Down
73 changes: 73 additions & 0 deletions runtime/tl/rpc_req_error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2023 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include "runtime/tl/rpc_req_error.h"

#include "common/rpc-error-codes.h"
#include "common/tl/constants/common.h"
#include "runtime/rpc.h"

bool RpcError::try_fetch() noexcept {
int pos_backup = rpc_get_pos();
int op = tl_parse_int();
if (op == TL_REQ_RESULT_HEADER) {
int flags = tl_parse_int();
fetch_and_skip_header(flags);
op = tl_parse_int();
}
if (op != TL_RPC_REQ_ERROR) {
rpc_set_pos(pos_backup);
return false;
}

tl_parse_long();
error_code = tl_parse_int();
error_msg = tl_parse_string();

if (!CurException.is_null()) {
error_code = TL_ERROR_SYNTAX;
error_msg = std::move(CurException->$message);
CurException = Optional<bool>{};
}
return true;
}

void RpcError::fetch_and_skip_header(int flags) const noexcept {
if (flags & vk::tl::common::rpc_req_result_extra_flags::binlog_pos) {
tl_parse_long();
}
if (flags & vk::tl::common::rpc_req_result_extra_flags::binlog_time) {
tl_parse_long();
}
if (flags & vk::tl::common::rpc_req_result_extra_flags::engine_pid) {
tl_parse_int();
tl_parse_int();
tl_parse_int();
}
if (flags & vk::tl::common::rpc_req_result_extra_flags::request_size) {
tl_parse_int();
}
if (flags & vk::tl::common::rpc_req_result_extra_flags::response_size) {
tl_parse_int();
}
if (flags & vk::tl::common::rpc_req_result_extra_flags::failed_subqueries) {
tl_parse_int();
}
if (flags & vk::tl::common::rpc_req_result_extra_flags::compression_version) {
tl_parse_int();
}
if (flags & vk::tl::common::rpc_req_result_extra_flags::stats) {
size_t n = tl_parse_int();
for (int i = 0; i < n; ++i) {
tl_parse_string();
tl_parse_string();
}
}
if (flags & vk::tl::common::rpc_req_result_extra_flags::epoch_number) {
tl_parse_long();
}
if (flags & vk::tl::common::rpc_req_result_extra_flags::view_number) {
tl_parse_long();
}
}
17 changes: 17 additions & 0 deletions runtime/tl/rpc_req_error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2023 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#pragma once

#include "runtime/kphp_core.h"

struct RpcError {
int error_code = 0;
string error_msg = {};

bool try_fetch() noexcept;

private:
void fetch_and_skip_header(int flags) const noexcept;
};
21 changes: 5 additions & 16 deletions runtime/tl/rpc_response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "runtime/exception.h"
#include "runtime/rpc.h"
#include "runtime/tl/rpc_req_error.h"

class_instance<C$VK$TL$RpcResponse> RpcErrorFactory::make_error(const char *error, int error_code) const {
return make_error(string{error}, error_code);
Expand All @@ -24,21 +25,9 @@ class_instance<C$VK$TL$RpcResponse> RpcErrorFactory::make_error_from_exception_i
}

class_instance<C$VK$TL$RpcResponse> RpcErrorFactory::fetch_error_if_possible() const {
int x = rpc_lookup_int();
if (x == TL_RPC_REQ_ERROR && CurException.is_null()) {
php_assert (tl_parse_int() == TL_RPC_REQ_ERROR);
if (CurException.is_null()) {
tl_parse_long();
if (CurException.is_null()) {
int error_code = tl_parse_int();
if (CurException.is_null()) {
string error = tl_parse_string();
if (CurException.is_null()) {
return make_error(error, error_code);
}
}
}
}
RpcError rpc_error;
if (!rpc_error.try_fetch()) {
return {};
}
return make_error_from_exception_if_possible();
return make_error(rpc_error.error_msg, rpc_error.error_code);
}