From 8e9680f3948a4001bcdc406bf79a662b115c9ea7 Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Fri, 23 Dec 2022 19:46:59 +0100 Subject: [PATCH] Introduce numeric error codes in mqtt webapi --- include/WebApi_errors.h | 17 +++++++++++++++++ src/WebApi_mqtt.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/WebApi_errors.h b/include/WebApi_errors.h index 8640d9ad..0dd5fa3f 100644 --- a/include/WebApi_errors.h +++ b/include/WebApi_errors.h @@ -37,4 +37,21 @@ enum WebApiError { MaintenanceBase = 6000, MaintenanceRebootTriggered, MaintenanceRebootCancled, + + MqttBase = 7000, + MqttHostnameLength, + MqttUsernameLength, + MqttPasswordLength, + MqttTopicLength, + MqttTopicCharacter, + MqttTopicTrailingSlash, + MqttPort, + MqttCertificateLength, + MqttLwtTopicLength, + MqttLwtTopicCharacter, + MqttLwtOnlineLength, + MqttLwtOfflineLength, + MqttPublishInterval, + MqttHassTopicLength, + MqttHassTopicCharacter, }; \ No newline at end of file diff --git a/src/WebApi_mqtt.cpp b/src/WebApi_mqtt.cpp index 71ab6774..2bcf79c1 100644 --- a/src/WebApi_mqtt.cpp +++ b/src/WebApi_mqtt.cpp @@ -7,6 +7,7 @@ #include "MqttHandleHass.h" #include "MqttSettings.h" #include "WebApi.h" +#include "WebApi_errors.h" #include "helper.h" #include @@ -101,6 +102,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (!request->hasParam("data", true)) { retMsg[F("message")] = F("No values found!"); + retMsg[F("code")] = WebApiError::GenericNoValueFound; response->setLength(); request->send(response); return; @@ -110,6 +112,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (json.length() > MQTT_JSON_DOC_SIZE) { retMsg[F("message")] = F("Data too large!"); + retMsg[F("code")] = WebApiError::GenericDataTooLarge; response->setLength(); request->send(response); return; @@ -120,6 +123,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (error) { retMsg[F("message")] = F("Failed to parse data!"); + retMsg[F("code")] = WebApiError::GenericParseError; response->setLength(); request->send(response); return; @@ -143,6 +147,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) && root.containsKey("mqtt_hass_topic") && root.containsKey("mqtt_hass_individualpanels"))) { retMsg[F("message")] = F("Values are missing!"); + retMsg[F("code")] = WebApiError::GenericValueMissing; response->setLength(); request->send(response); return; @@ -151,6 +156,8 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (root[F("mqtt_enabled")].as()) { if (root[F("mqtt_hostname")].as().length() == 0 || root[F("mqtt_hostname")].as().length() > MQTT_MAX_HOSTNAME_STRLEN) { 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(); request->send(response); return; @@ -158,18 +165,24 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (root[F("mqtt_username")].as().length() > MQTT_MAX_USERNAME_STRLEN) { 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(); request->send(response); return; } if (root[F("mqtt_password")].as().length() > MQTT_MAX_PASSWORD_STRLEN) { 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(); request->send(response); return; } if (root[F("mqtt_topic")].as().length() > MQTT_MAX_TOPIC_STRLEN) { 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(); request->send(response); return; @@ -177,6 +190,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (root[F("mqtt_topic")].as().indexOf(' ') != -1) { retMsg[F("message")] = F("Topic must not contain space characters!"); + retMsg[F("code")] = WebApiError::MqttTopicCharacter; response->setLength(); request->send(response); return; @@ -184,6 +198,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (!root[F("mqtt_topic")].as().endsWith("/")) { retMsg[F("message")] = F("Topic must end with slash (/)!"); + retMsg[F("code")] = WebApiError::MqttTopicTrailingSlash; response->setLength(); request->send(response); return; @@ -191,6 +206,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (root[F("mqtt_port")].as() == 0 || root[F("mqtt_port")].as() > 65535) { retMsg[F("message")] = F("Port must be a number between 1 and 65535!"); + retMsg[F("code")] = WebApiError::MqttPort; response->setLength(); request->send(response); return; @@ -198,6 +214,8 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (root[F("mqtt_root_ca_cert")].as().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("code")] = WebApiError::MqttCertificateLength; + retMsg[F("param")][F("max")] = MQTT_MAX_ROOT_CA_CERT_STRLEN; response->setLength(); request->send(response); return; @@ -205,6 +223,8 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (root[F("mqtt_lwt_topic")].as().length() > MQTT_MAX_TOPIC_STRLEN) { 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(); request->send(response); return; @@ -212,6 +232,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (root[F("mqtt_lwt_topic")].as().indexOf(' ') != -1) { retMsg[F("message")] = F("LWT topic must not contain space characters!"); + retMsg[F("code")] = WebApiError::MqttLwtTopicCharacter; response->setLength(); request->send(response); return; @@ -219,6 +240,8 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (root[F("mqtt_lwt_online")].as().length() > MQTT_MAX_LWTVALUE_STRLEN) { 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(); request->send(response); return; @@ -226,6 +249,8 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (root[F("mqtt_lwt_offline")].as().length() > MQTT_MAX_LWTVALUE_STRLEN) { 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(); request->send(response); return; @@ -233,6 +258,9 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (root[F("mqtt_publish_interval")].as() < 5 || root[F("mqtt_publish_interval")].as() > 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(); request->send(response); return; @@ -241,6 +269,8 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (root[F("mqtt_hass_enabled")].as()) { if (root[F("mqtt_hass_topic")].as().length() > MQTT_MAX_TOPIC_STRLEN) { 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(); request->send(response); return; @@ -248,6 +278,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) if (root[F("mqtt_hass_topic")].as().indexOf(' ') != -1) { retMsg[F("message")] = F("Hass topic must not contain space characters!"); + retMsg[F("code")] = WebApiError::MqttHassTopicCharacter; response->setLength(); request->send(response); return; @@ -278,6 +309,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) retMsg[F("type")] = F("success"); retMsg[F("message")] = F("Settings saved!"); + retMsg[F("code")] = WebApiError::GenericSuccess; response->setLength(); request->send(response);