Introduce numeric error codes in mqtt webapi

This commit is contained in:
Thomas Basler 2022-12-23 19:46:59 +01:00
parent 7a5d8dd251
commit 8e9680f394
2 changed files with 49 additions and 0 deletions

View File

@ -37,4 +37,21 @@ enum WebApiError {
MaintenanceBase = 6000, MaintenanceBase = 6000,
MaintenanceRebootTriggered, MaintenanceRebootTriggered,
MaintenanceRebootCancled, MaintenanceRebootCancled,
MqttBase = 7000,
MqttHostnameLength,
MqttUsernameLength,
MqttPasswordLength,
MqttTopicLength,
MqttTopicCharacter,
MqttTopicTrailingSlash,
MqttPort,
MqttCertificateLength,
MqttLwtTopicLength,
MqttLwtTopicCharacter,
MqttLwtOnlineLength,
MqttLwtOfflineLength,
MqttPublishInterval,
MqttHassTopicLength,
MqttHassTopicCharacter,
}; };

View File

@ -7,6 +7,7 @@
#include "MqttHandleHass.h" #include "MqttHandleHass.h"
#include "MqttSettings.h" #include "MqttSettings.h"
#include "WebApi.h" #include "WebApi.h"
#include "WebApi_errors.h"
#include "helper.h" #include "helper.h"
#include <AsyncJson.h> #include <AsyncJson.h>
@ -101,6 +102,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (!request->hasParam("data", true)) { if (!request->hasParam("data", true)) {
retMsg[F("message")] = F("No values found!"); retMsg[F("message")] = F("No values found!");
retMsg[F("code")] = WebApiError::GenericNoValueFound;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -110,6 +112,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (json.length() > MQTT_JSON_DOC_SIZE) { if (json.length() > MQTT_JSON_DOC_SIZE) {
retMsg[F("message")] = F("Data too large!"); retMsg[F("message")] = F("Data too large!");
retMsg[F("code")] = WebApiError::GenericDataTooLarge;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -120,6 +123,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (error) { if (error) {
retMsg[F("message")] = F("Failed to parse data!"); retMsg[F("message")] = F("Failed to parse data!");
retMsg[F("code")] = WebApiError::GenericParseError;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -143,6 +147,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
&& root.containsKey("mqtt_hass_topic") && root.containsKey("mqtt_hass_topic")
&& root.containsKey("mqtt_hass_individualpanels"))) { && root.containsKey("mqtt_hass_individualpanels"))) {
retMsg[F("message")] = F("Values are missing!"); retMsg[F("message")] = F("Values are missing!");
retMsg[F("code")] = WebApiError::GenericValueMissing;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -151,6 +156,8 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (root[F("mqtt_enabled")].as<bool>()) { if (root[F("mqtt_enabled")].as<bool>()) {
if (root[F("mqtt_hostname")].as<String>().length() == 0 || root[F("mqtt_hostname")].as<String>().length() > MQTT_MAX_HOSTNAME_STRLEN) { if (root[F("mqtt_hostname")].as<String>().length() == 0 || root[F("mqtt_hostname")].as<String>().length() > MQTT_MAX_HOSTNAME_STRLEN) {
retMsg[F("message")] = F("MqTT Server must between 1 and " STR(MQTT_MAX_HOSTNAME_STRLEN) " characters long!"); retMsg[F("message")] = F("MqTT Server must between 1 and " STR(MQTT_MAX_HOSTNAME_STRLEN) " characters long!");
retMsg[F("code")] = WebApiError::MqttHostnameLength;
retMsg[F("param")][F("max")] = MQTT_MAX_HOSTNAME_STRLEN;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -158,18 +165,24 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (root[F("mqtt_username")].as<String>().length() > MQTT_MAX_USERNAME_STRLEN) { if (root[F("mqtt_username")].as<String>().length() > MQTT_MAX_USERNAME_STRLEN) {
retMsg[F("message")] = F("Username must not longer then " STR(MQTT_MAX_USERNAME_STRLEN) " characters!"); retMsg[F("message")] = F("Username must not longer then " STR(MQTT_MAX_USERNAME_STRLEN) " characters!");
retMsg[F("code")] = WebApiError::MqttUsernameLength;
retMsg[F("param")][F("max")] = MQTT_MAX_USERNAME_STRLEN;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
} }
if (root[F("mqtt_password")].as<String>().length() > MQTT_MAX_PASSWORD_STRLEN) { if (root[F("mqtt_password")].as<String>().length() > MQTT_MAX_PASSWORD_STRLEN) {
retMsg[F("message")] = F("Password must not longer then " STR(MQTT_MAX_PASSWORD_STRLEN) " characters!"); retMsg[F("message")] = F("Password must not longer then " STR(MQTT_MAX_PASSWORD_STRLEN) " characters!");
retMsg[F("code")] = WebApiError::MqttPasswordLength;
retMsg[F("param")][F("max")] = MQTT_MAX_PASSWORD_STRLEN;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
} }
if (root[F("mqtt_topic")].as<String>().length() > MQTT_MAX_TOPIC_STRLEN) { if (root[F("mqtt_topic")].as<String>().length() > MQTT_MAX_TOPIC_STRLEN) {
retMsg[F("message")] = F("Topic must not longer then " STR(MQTT_MAX_TOPIC_STRLEN) " characters!"); retMsg[F("message")] = F("Topic must not longer then " STR(MQTT_MAX_TOPIC_STRLEN) " characters!");
retMsg[F("code")] = WebApiError::MqttTopicLength;
retMsg[F("param")][F("max")] = MQTT_MAX_TOPIC_STRLEN;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -177,6 +190,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (root[F("mqtt_topic")].as<String>().indexOf(' ') != -1) { if (root[F("mqtt_topic")].as<String>().indexOf(' ') != -1) {
retMsg[F("message")] = F("Topic must not contain space characters!"); retMsg[F("message")] = F("Topic must not contain space characters!");
retMsg[F("code")] = WebApiError::MqttTopicCharacter;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -184,6 +198,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (!root[F("mqtt_topic")].as<String>().endsWith("/")) { if (!root[F("mqtt_topic")].as<String>().endsWith("/")) {
retMsg[F("message")] = F("Topic must end with slash (/)!"); retMsg[F("message")] = F("Topic must end with slash (/)!");
retMsg[F("code")] = WebApiError::MqttTopicTrailingSlash;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -191,6 +206,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (root[F("mqtt_port")].as<uint>() == 0 || root[F("mqtt_port")].as<uint>() > 65535) { if (root[F("mqtt_port")].as<uint>() == 0 || root[F("mqtt_port")].as<uint>() > 65535) {
retMsg[F("message")] = F("Port must be a number between 1 and 65535!"); retMsg[F("message")] = F("Port must be a number between 1 and 65535!");
retMsg[F("code")] = WebApiError::MqttPort;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -198,6 +214,8 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (root[F("mqtt_root_ca_cert")].as<String>().length() > MQTT_MAX_ROOT_CA_CERT_STRLEN) { if (root[F("mqtt_root_ca_cert")].as<String>().length() > MQTT_MAX_ROOT_CA_CERT_STRLEN) {
retMsg[F("message")] = F("Certificate must not longer then " STR(MQTT_MAX_ROOT_CA_CERT_STRLEN) " characters!"); retMsg[F("message")] = F("Certificate must not longer then " STR(MQTT_MAX_ROOT_CA_CERT_STRLEN) " characters!");
retMsg[F("code")] = WebApiError::MqttCertificateLength;
retMsg[F("param")][F("max")] = MQTT_MAX_ROOT_CA_CERT_STRLEN;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -205,6 +223,8 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (root[F("mqtt_lwt_topic")].as<String>().length() > MQTT_MAX_TOPIC_STRLEN) { if (root[F("mqtt_lwt_topic")].as<String>().length() > MQTT_MAX_TOPIC_STRLEN) {
retMsg[F("message")] = F("LWT topic must not longer then " STR(MQTT_MAX_TOPIC_STRLEN) " characters!"); retMsg[F("message")] = F("LWT topic must not longer then " STR(MQTT_MAX_TOPIC_STRLEN) " characters!");
retMsg[F("code")] = WebApiError::MqttLwtTopicLength;
retMsg[F("param")][F("max")] = MQTT_MAX_TOPIC_STRLEN;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -212,6 +232,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (root[F("mqtt_lwt_topic")].as<String>().indexOf(' ') != -1) { if (root[F("mqtt_lwt_topic")].as<String>().indexOf(' ') != -1) {
retMsg[F("message")] = F("LWT topic must not contain space characters!"); retMsg[F("message")] = F("LWT topic must not contain space characters!");
retMsg[F("code")] = WebApiError::MqttLwtTopicCharacter;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -219,6 +240,8 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (root[F("mqtt_lwt_online")].as<String>().length() > MQTT_MAX_LWTVALUE_STRLEN) { if (root[F("mqtt_lwt_online")].as<String>().length() > MQTT_MAX_LWTVALUE_STRLEN) {
retMsg[F("message")] = F("LWT online value must not longer then " STR(MQTT_MAX_LWTVALUE_STRLEN) " characters!"); retMsg[F("message")] = F("LWT online value must not longer then " STR(MQTT_MAX_LWTVALUE_STRLEN) " characters!");
retMsg[F("code")] = WebApiError::MqttLwtOnlineLength;
retMsg[F("param")][F("max")] = MQTT_MAX_LWTVALUE_STRLEN;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -226,6 +249,8 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (root[F("mqtt_lwt_offline")].as<String>().length() > MQTT_MAX_LWTVALUE_STRLEN) { if (root[F("mqtt_lwt_offline")].as<String>().length() > MQTT_MAX_LWTVALUE_STRLEN) {
retMsg[F("message")] = F("LWT offline value must not longer then " STR(MQTT_MAX_LWTVALUE_STRLEN) " characters!"); retMsg[F("message")] = F("LWT offline value must not longer then " STR(MQTT_MAX_LWTVALUE_STRLEN) " characters!");
retMsg[F("code")] = WebApiError::MqttLwtOfflineLength;
retMsg[F("param")][F("max")] = MQTT_MAX_LWTVALUE_STRLEN;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -233,6 +258,9 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (root[F("mqtt_publish_interval")].as<uint32_t>() < 5 || root[F("mqtt_publish_interval")].as<uint32_t>() > 65535) { if (root[F("mqtt_publish_interval")].as<uint32_t>() < 5 || root[F("mqtt_publish_interval")].as<uint32_t>() > 65535) {
retMsg[F("message")] = F("Publish interval must be a number between 5 and 65535!"); retMsg[F("message")] = F("Publish interval must be a number between 5 and 65535!");
retMsg[F("code")] = WebApiError::MqttPublishInterval;
retMsg[F("param")][F("min")] = 5;
retMsg[F("param")][F("max")] = 65535;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -241,6 +269,8 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (root[F("mqtt_hass_enabled")].as<bool>()) { if (root[F("mqtt_hass_enabled")].as<bool>()) {
if (root[F("mqtt_hass_topic")].as<String>().length() > MQTT_MAX_TOPIC_STRLEN) { if (root[F("mqtt_hass_topic")].as<String>().length() > MQTT_MAX_TOPIC_STRLEN) {
retMsg[F("message")] = F("Hass topic must not longer then " STR(MQTT_MAX_TOPIC_STRLEN) " characters!"); retMsg[F("message")] = F("Hass topic must not longer then " STR(MQTT_MAX_TOPIC_STRLEN) " characters!");
retMsg[F("code")] = WebApiError::MqttHassTopicLength;
retMsg[F("param")][F("max")] = MQTT_MAX_TOPIC_STRLEN;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -248,6 +278,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
if (root[F("mqtt_hass_topic")].as<String>().indexOf(' ') != -1) { if (root[F("mqtt_hass_topic")].as<String>().indexOf(' ') != -1) {
retMsg[F("message")] = F("Hass topic must not contain space characters!"); retMsg[F("message")] = F("Hass topic must not contain space characters!");
retMsg[F("code")] = WebApiError::MqttHassTopicCharacter;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -278,6 +309,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
retMsg[F("type")] = F("success"); retMsg[F("type")] = F("success");
retMsg[F("message")] = F("Settings saved!"); retMsg[F("message")] = F("Settings saved!");
retMsg[F("code")] = WebApiError::GenericSuccess;
response->setLength(); response->setLength();
request->send(response); request->send(response);