diff --git a/front-generator/template/common/all-params-methods.js.j2 b/front-generator/template/common/all-params-methods.js.j2 index 82d61cd..5d373f5 100644 --- a/front-generator/template/common/all-params-methods.js.j2 +++ b/front-generator/template/common/all-params-methods.js.j2 @@ -13,7 +13,7 @@ this.submitStatus.{{ g['group'] }} = true fetch('/api/set/{{ g["group"] }}', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) - .then(async (resp) => { this.update{{ g['group'] | title }}Settings(await resp.json()) }) + .then(async (resp) => { let vals = await resp.json(); if (vals['status'] !== 'ok') { throw new Error(vals['error'] ? vals['error'] : "Server returns undefined error") } this.update{{ g['group'] | title }}Settings(vals) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) }) .finally(() => { this.submitStatus.{{ g['group'] }} = false }) }, diff --git a/front-generator/template/common/qos-methods.js.j2 b/front-generator/template/common/qos-methods.js.j2 index c234bf9..9083e7c 100644 --- a/front-generator/template/common/qos-methods.js.j2 +++ b/front-generator/template/common/qos-methods.js.j2 @@ -63,6 +63,7 @@ body: JSON.stringify(query) }).then(async (resp) => { this.submitStatusQos = false + if (resp['error']) { throw new Error(resp['error']) } this.updateQosSettings(await resp.json()) }).catch((reason) => { this.submitStatusQos = false diff --git a/src/main.cpp b/src/main.cpp index 5cbd7f7..346b306 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -281,7 +281,7 @@ public: rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "WebHandle(/api/set/qos): Can't set QoS settings: " << e.what(); - const std::string result = R"({"status":"error"})"; + const std::string result = R"({"status": "error", "error": )" + api_driver::buildEscapedString(e.what()) + "}"; rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); } })); @@ -309,7 +309,7 @@ public: rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "WebHandle(/api/set/buclnb): Can't set BUC LNB settings: " << e.what(); - const std::string result = R"({"status":"error"})"; + const std::string result = R"({"status": "error", "error": )" + api_driver::buildEscapedString(e.what()) + "}"; rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); } })); @@ -337,7 +337,7 @@ public: rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "WebHandle(/api/set/cinc): Can't set CinC settings: " << e.what(); - const std::string result = R"({"status":"error"})"; + const std::string result = R"({"status": "error", "error": )" + api_driver::buildEscapedString(e.what()) + "}"; rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); } })); @@ -365,7 +365,7 @@ public: rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "WebHandle(/api/set/rxtx): Can't set RX/TX settings: " << e.what(); - const std::string result = R"({"status":"error"})"; + const std::string result = R"({"status": "error", "error": )" + api_driver::buildEscapedString(e.what()) + "}"; rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); } })); @@ -393,7 +393,7 @@ public: rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << "WebHandle(/api/set/network): Can't set network settings: " << e.what(); - const std::string result = R"({"status":"error"})"; + const std::string result = R"({"status": "error", "error": )" + api_driver::buildEscapedString(e.what()) + "}"; rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); } })); diff --git a/src/terminal_api_driver.cpp b/src/terminal_api_driver.cpp index 05dafd1..ed657af 100644 --- a/src/terminal_api_driver.cpp +++ b/src/terminal_api_driver.cpp @@ -651,7 +651,7 @@ static const char* boolAsStr(bool value) { return value ? "true" : "false"; } -static std::string buildEscapedString(const std::string& source) { +std::string api_driver::buildEscapedString(const std::string& source) { std::string str(source); size_t start_pos = 0; while((start_pos = str.find('\"', start_pos)) != std::string::npos) { @@ -661,7 +661,7 @@ static std::string buildEscapedString(const std::string& source) { return "\"" + str + "\""; } -void writeDouble(std::ostream& out, double value, int prec = 2) { +static void writeDouble(std::ostream& out, double value, int prec = 2) { if (std::isnan(value) || std::isinf(value)) { out << "\"nan\""; } else { @@ -873,7 +873,7 @@ std::string api_driver::ApiDriver::loadSettings() const { std::stringstream result; result << "{\n\"txAutoStart\":" << boolAsStr(modSettings.is_save_current_state); #ifdef MODEM_IS_SCPC - result << ",\"txEn\":" << boolAsStr(!modSettings.tx_is_on); + result << ",\"txEn\":" << boolAsStr(modSettings.tx_is_on); result << ",\"txModulatorIsTest\":" << boolAsStr(!modSettings.is_carrier); #endif result << ",\"txIsTestInput\":" << boolAsStr(modSettings.is_test_data); diff --git a/src/terminal_api_driver.h b/src/terminal_api_driver.h index 4685532..d4418c1 100644 --- a/src/terminal_api_driver.h +++ b/src/terminal_api_driver.h @@ -75,6 +75,13 @@ namespace api_driver { private: std::unique_ptr daemon; }; + + /** + * Функция для создания экранированной строки (для json) + * @param source исходная строка (например, {123"}) + * @return {"123\""} + */ + std::string buildEscapedString(const std::string& source); } #endif //TERMINAL_API_DRIVER_H diff --git a/static/main-scpc.html b/static/main-scpc.html index 36c13d0..d802238 100644 --- a/static/main-scpc.html +++ b/static/main-scpc.html @@ -774,7 +774,7 @@ this.submitStatus.rxtx = true fetch('/api/set/rxtx', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) - .then(async (resp) => { this.updateRxtxSettings(await resp.json()) }) + .then(async (resp) => { let vals = await resp.json(); if (vals['status'] !== 'ok') { throw new Error(vals['error'] ? vals['error'] : "Server returns undefined error") } this.updateRxtxSettings(vals) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) }) .finally(() => { this.submitStatus.rxtx = false }) }, @@ -793,7 +793,7 @@ this.submitStatus.cinc = true fetch('/api/set/cinc', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) - .then(async (resp) => { this.updateCincSettings(await resp.json()) }) + .then(async (resp) => { let vals = await resp.json(); if (vals['status'] !== 'ok') { throw new Error(vals['error'] ? vals['error'] : "Server returns undefined error") } this.updateCincSettings(vals) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) }) .finally(() => { this.submitStatus.cinc = false }) }, @@ -812,7 +812,7 @@ this.submitStatus.buclnb = true fetch('/api/set/buclnb', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) - .then(async (resp) => { this.updateBuclnbSettings(await resp.json()) }) + .then(async (resp) => { let vals = await resp.json(); if (vals['status'] !== 'ok') { throw new Error(vals['error'] ? vals['error'] : "Server returns undefined error") } this.updateBuclnbSettings(vals) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) }) .finally(() => { this.submitStatus.buclnb = false }) }, @@ -826,7 +826,7 @@ this.submitStatus.tcpaccel = true fetch('/api/set/tcpaccel', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) - .then(async (resp) => { this.updateTcpaccelSettings(await resp.json()) }) + .then(async (resp) => { let vals = await resp.json(); if (vals['status'] !== 'ok') { throw new Error(vals['error'] ? vals['error'] : "Server returns undefined error") } this.updateTcpaccelSettings(vals) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) }) .finally(() => { this.submitStatus.tcpaccel = false }) }, @@ -843,7 +843,7 @@ this.submitStatus.network = true fetch('/api/set/network', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) - .then(async (resp) => { this.updateNetworkSettings(await resp.json()) }) + .then(async (resp) => { let vals = await resp.json(); if (vals['status'] !== 'ok') { throw new Error(vals['error'] ? vals['error'] : "Server returns undefined error") } this.updateNetworkSettings(vals) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) }) .finally(() => { this.submitStatus.network = false }) }, @@ -1077,6 +1077,7 @@ body: JSON.stringify(query) }).then(async (resp) => { this.submitStatusQos = false + if (resp['error']) { throw new Error(resp['error']) } this.updateQosSettings(await resp.json()) }).catch((reason) => { this.submitStatusQos = false diff --git a/static/main-tdma.html b/static/main-tdma.html index 470bc45..00472be 100644 --- a/static/main-tdma.html +++ b/static/main-tdma.html @@ -436,7 +436,7 @@ this.submitStatus.rxtx = true fetch('/api/set/rxtx', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) - .then(async (resp) => { this.updateRxtxSettings(await resp.json()) }) + .then(async (resp) => { let vals = await resp.json(); if (vals['status'] !== 'ok') { throw new Error(vals['error'] ? vals['error'] : "Server returns undefined error") } this.updateRxtxSettings(vals) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) }) .finally(() => { this.submitStatus.rxtx = false }) }, @@ -455,7 +455,7 @@ this.submitStatus.buclnb = true fetch('/api/set/buclnb', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) - .then(async (resp) => { this.updateBuclnbSettings(await resp.json()) }) + .then(async (resp) => { let vals = await resp.json(); if (vals['status'] !== 'ok') { throw new Error(vals['error'] ? vals['error'] : "Server returns undefined error") } this.updateBuclnbSettings(vals) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) }) .finally(() => { this.submitStatus.buclnb = false }) }, @@ -472,7 +472,7 @@ this.submitStatus.network = true fetch('/api/set/network', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) - .then(async (resp) => { this.updateNetworkSettings(await resp.json()) }) + .then(async (resp) => { let vals = await resp.json(); if (vals['status'] !== 'ok') { throw new Error(vals['error'] ? vals['error'] : "Server returns undefined error") } this.updateNetworkSettings(vals) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) }) .finally(() => { this.submitStatus.network = false }) },