вывод нормальной ошибки от сервера + фикс установки режима работы передатчика

This commit is contained in:
Vladislav Ostapov 2025-01-16 13:50:01 +03:00
parent cbd2adc1c8
commit 790bfc06c2
7 changed files with 26 additions and 17 deletions

View File

@ -13,7 +13,7 @@
this.submitStatus.{{ g['group'] }} = true this.submitStatus.{{ g['group'] }} = true
fetch('/api/set/{{ g["group"] }}', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) 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}`) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) })
.finally(() => { this.submitStatus.{{ g['group'] }} = false }) .finally(() => { this.submitStatus.{{ g['group'] }} = false })
}, },

View File

@ -63,6 +63,7 @@
body: JSON.stringify(query) body: JSON.stringify(query)
}).then(async (resp) => { }).then(async (resp) => {
this.submitStatusQos = false this.submitStatusQos = false
if (resp['error']) { throw new Error(resp['error']) }
this.updateQosSettings(await resp.json()) this.updateQosSettings(await resp.json())
}).catch((reason) => { }).catch((reason) => {
this.submitStatusQos = false this.submitStatusQos = false

View File

@ -281,7 +281,7 @@ public:
rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size());
} catch (std::exception& e) { } catch (std::exception& e) {
BOOST_LOG_TRIVIAL(error) << "WebHandle(/api/set/qos): Can't set QoS settings: " << e.what(); 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()); 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()); rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size());
} catch (std::exception& e) { } catch (std::exception& e) {
BOOST_LOG_TRIVIAL(error) << "WebHandle(/api/set/buclnb): Can't set BUC LNB settings: " << e.what(); 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()); 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()); rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size());
} catch (std::exception& e) { } catch (std::exception& e) {
BOOST_LOG_TRIVIAL(error) << "WebHandle(/api/set/cinc): Can't set CinC settings: " << e.what(); 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()); 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()); rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size());
} catch (std::exception& e) { } catch (std::exception& e) {
BOOST_LOG_TRIVIAL(error) << "WebHandle(/api/set/rxtx): Can't set RX/TX settings: " << e.what(); 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()); 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()); rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size());
} catch (std::exception& e) { } catch (std::exception& e) {
BOOST_LOG_TRIVIAL(error) << "WebHandle(/api/set/network): Can't set network settings: " << e.what(); 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()); rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size());
} }
})); }));

View File

@ -651,7 +651,7 @@ static const char* boolAsStr(bool value) {
return value ? "true" : "false"; 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); std::string str(source);
size_t start_pos = 0; size_t start_pos = 0;
while((start_pos = str.find('\"', start_pos)) != std::string::npos) { while((start_pos = str.find('\"', start_pos)) != std::string::npos) {
@ -661,7 +661,7 @@ static std::string buildEscapedString(const std::string& source) {
return "\"" + str + "\""; 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)) { if (std::isnan(value) || std::isinf(value)) {
out << "\"nan\""; out << "\"nan\"";
} else { } else {
@ -873,7 +873,7 @@ std::string api_driver::ApiDriver::loadSettings() const {
std::stringstream result; std::stringstream result;
result << "{\n\"txAutoStart\":" << boolAsStr(modSettings.is_save_current_state); result << "{\n\"txAutoStart\":" << boolAsStr(modSettings.is_save_current_state);
#ifdef MODEM_IS_SCPC #ifdef MODEM_IS_SCPC
result << ",\"txEn\":" << boolAsStr(!modSettings.tx_is_on); result << ",\"txEn\":" << boolAsStr(modSettings.tx_is_on);
result << ",\"txModulatorIsTest\":" << boolAsStr(!modSettings.is_carrier); result << ",\"txModulatorIsTest\":" << boolAsStr(!modSettings.is_carrier);
#endif #endif
result << ",\"txIsTestInput\":" << boolAsStr(modSettings.is_test_data); result << ",\"txIsTestInput\":" << boolAsStr(modSettings.is_test_data);

View File

@ -75,6 +75,13 @@ namespace api_driver {
private: private:
std::unique_ptr<TerminalApiDaemon> daemon; std::unique_ptr<TerminalApiDaemon> daemon;
}; };
/**
* Функция для создания экранированной строки (для json)
* @param source исходная строка (например, {123"})
* @return {"123\""}
*/
std::string buildEscapedString(const std::string& source);
} }
#endif //TERMINAL_API_DRIVER_H #endif //TERMINAL_API_DRIVER_H

View File

@ -774,7 +774,7 @@
this.submitStatus.rxtx = true this.submitStatus.rxtx = true
fetch('/api/set/rxtx', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) 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}`) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) })
.finally(() => { this.submitStatus.rxtx = false }) .finally(() => { this.submitStatus.rxtx = false })
}, },
@ -793,7 +793,7 @@
this.submitStatus.cinc = true this.submitStatus.cinc = true
fetch('/api/set/cinc', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) 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}`) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) })
.finally(() => { this.submitStatus.cinc = false }) .finally(() => { this.submitStatus.cinc = false })
}, },
@ -812,7 +812,7 @@
this.submitStatus.buclnb = true this.submitStatus.buclnb = true
fetch('/api/set/buclnb', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) 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}`) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) })
.finally(() => { this.submitStatus.buclnb = false }) .finally(() => { this.submitStatus.buclnb = false })
}, },
@ -826,7 +826,7 @@
this.submitStatus.tcpaccel = true this.submitStatus.tcpaccel = true
fetch('/api/set/tcpaccel', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) 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}`) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) })
.finally(() => { this.submitStatus.tcpaccel = false }) .finally(() => { this.submitStatus.tcpaccel = false })
}, },
@ -843,7 +843,7 @@
this.submitStatus.network = true this.submitStatus.network = true
fetch('/api/set/network', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) 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}`) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) })
.finally(() => { this.submitStatus.network = false }) .finally(() => { this.submitStatus.network = false })
}, },
@ -1077,6 +1077,7 @@
body: JSON.stringify(query) body: JSON.stringify(query)
}).then(async (resp) => { }).then(async (resp) => {
this.submitStatusQos = false this.submitStatusQos = false
if (resp['error']) { throw new Error(resp['error']) }
this.updateQosSettings(await resp.json()) this.updateQosSettings(await resp.json())
}).catch((reason) => { }).catch((reason) => {
this.submitStatusQos = false this.submitStatusQos = false

View File

@ -436,7 +436,7 @@
this.submitStatus.rxtx = true this.submitStatus.rxtx = true
fetch('/api/set/rxtx', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) 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}`) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) })
.finally(() => { this.submitStatus.rxtx = false }) .finally(() => { this.submitStatus.rxtx = false })
}, },
@ -455,7 +455,7 @@
this.submitStatus.buclnb = true this.submitStatus.buclnb = true
fetch('/api/set/buclnb', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) 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}`) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) })
.finally(() => { this.submitStatus.buclnb = false }) .finally(() => { this.submitStatus.buclnb = false })
}, },
@ -472,7 +472,7 @@
this.submitStatus.network = true this.submitStatus.network = true
fetch('/api/set/network', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(query) }) 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}`) }) .catch((reason) => { alert(`Ошибка при применении настроек: ${reason}`) })
.finally(() => { this.submitStatus.network = false }) .finally(() => { this.submitStatus.network = false })
}, },