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,
MaintenanceRebootTriggered,
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 "MqttSettings.h"
#include "WebApi.h"
#include "WebApi_errors.h"
#include "helper.h"
#include <AsyncJson.h>
@ -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<bool>()) {
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("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<String>().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<String>().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<String>().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<String>().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<String>().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<uint>() == 0 || root[F("mqtt_port")].as<uint>() > 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<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("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<String>().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<String>().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<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("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<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("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<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("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<bool>()) {
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("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<String>().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);