Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. All Rights Reserved.
3 : * SPDX-License-Identifier: Apache-2.0
4 : */
5 :
6 : #include "grpc_response_handler.hpp"
7 : #include "logger/logger.hpp"
8 : #include "logger/logger_manager.hpp"
9 :
10 : using namespace grpc;
11 : namespace iroha_cli {
12 :
13 : GrpcResponseHandler::GrpcResponseHandler(
14 : logger::LoggerManagerTreePtr log_manager)
15 0 : : tx_handler_(log_manager->getChild("Transaction")->getLogger()),
16 0 : query_handler_(log_manager->getChild("Query")->getLogger()),
17 0 : log_(log_manager->getChild("Grpc")->getLogger()) {
18 0 : handler_map_[CANCELLED] = "Operation canceled";
19 0 : handler_map_[UNKNOWN] = "Unknown error";
20 0 : handler_map_[INVALID_ARGUMENT] = "INVALID_ARGUMENT";
21 0 : handler_map_[DEADLINE_EXCEEDED] = "DEADLINE_EXCEEDED";
22 0 : handler_map_[NOT_FOUND] = "NOT_FOUND";
23 0 : handler_map_[ALREADY_EXISTS] = "ALREADY_EXISTS";
24 0 : handler_map_[PERMISSION_DENIED] = "PERMISSION_DENIED";
25 0 : handler_map_[UNAUTHENTICATED] = "UNAUTHENTICATED";
26 0 : handler_map_[RESOURCE_EXHAUSTED] = "RESOURCE_EXHAUSTED";
27 0 : handler_map_[FAILED_PRECONDITION] = "FAILED_PRECONDITION";
28 0 : handler_map_[ABORTED] = "ABORTED";
29 0 : handler_map_[OUT_OF_RANGE] = "OUT_OF_RANGE";
30 0 : handler_map_[INTERNAL] = "INTERNAL";
31 0 : handler_map_[UNIMPLEMENTED] = "UNIMPLEMENTED";
32 0 : handler_map_[UNAVAILABLE] = "Server is unavailable";
33 0 : handler_map_[DATA_LOSS] = "DATA_LOSS";
34 0 : }
35 :
36 : void GrpcResponseHandler::handle(
37 : CliClient::Response<CliClient::TxStatus> response) {
38 0 : if (response.status.ok()) {
39 0 : tx_handler_.handle(response.answer);
40 0 : } else {
41 0 : handleGrpcErrors(response.status.error_code());
42 : }
43 0 : }
44 :
45 : void GrpcResponseHandler::handle(
46 : CliClient::Response<iroha::protocol::QueryResponse> response) {
47 0 : if (response.status.ok()) {
48 0 : query_handler_.handle(response.answer);
49 0 : } else {
50 0 : handleGrpcErrors(response.status.error_code());
51 : }
52 0 : }
53 :
54 : void GrpcResponseHandler::handleGrpcErrors(grpc::StatusCode code) {
55 0 : auto it = handler_map_.find(code);
56 0 : if (it != handler_map_.end()) {
57 0 : log_->error(it->second);
58 0 : } else {
59 0 : log_->error("Handler for grpc {} not implemented", code);
60 : }
61 0 : }
62 :
63 : } // namespace iroha_cli
|