Introduce numeric error codes in power webapi

This commit is contained in:
Thomas Basler 2022-12-23 20:41:54 +01:00
parent 534f200eb9
commit 5bc264f4d2
2 changed files with 12 additions and 0 deletions

View File

@ -77,4 +77,8 @@ enum WebApiError {
SecurityBase = 10000,
SecurityPasswordLength,
SecurityAuthSuccess,
PowerBase = 11000,
PowerSerialZero,
PowerInvalidInverter,
};

View File

@ -4,6 +4,7 @@
*/
#include "WebApi_power.h"
#include "WebApi.h"
#include "WebApi_errors.h"
#include <AsyncJson.h>
#include <Hoymiles.h>
@ -61,6 +62,7 @@ void WebApiPowerClass::onPowerPost(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;
@ -70,6 +72,7 @@ void WebApiPowerClass::onPowerPost(AsyncWebServerRequest* request)
if (json.length() > 1024) {
retMsg[F("message")] = F("Data too large!");
retMsg[F("code")] = WebApiError::GenericDataTooLarge;
response->setLength();
request->send(response);
return;
@ -80,6 +83,7 @@ void WebApiPowerClass::onPowerPost(AsyncWebServerRequest* request)
if (error) {
retMsg[F("message")] = F("Failed to parse data!");
retMsg[F("code")] = WebApiError::GenericParseError;
response->setLength();
request->send(response);
return;
@ -88,6 +92,7 @@ void WebApiPowerClass::onPowerPost(AsyncWebServerRequest* request)
if (!(root.containsKey("serial")
&& (root.containsKey("power") || root.containsKey("restart")))) {
retMsg[F("message")] = F("Values are missing!");
retMsg[F("code")] = WebApiError::GenericValueMissing;
response->setLength();
request->send(response);
return;
@ -95,6 +100,7 @@ void WebApiPowerClass::onPowerPost(AsyncWebServerRequest* request)
if (root[F("serial")].as<uint64_t>() == 0) {
retMsg[F("message")] = F("Serial must be a number > 0!");
retMsg[F("code")] = WebApiError::PowerSerialZero;
response->setLength();
request->send(response);
return;
@ -104,6 +110,7 @@ void WebApiPowerClass::onPowerPost(AsyncWebServerRequest* request)
auto inv = Hoymiles.getInverterBySerial(serial);
if (inv == nullptr) {
retMsg[F("message")] = F("Invalid inverter specified!");
retMsg[F("code")] = WebApiError::PowerInvalidInverter;
response->setLength();
request->send(response);
return;
@ -120,6 +127,7 @@ void WebApiPowerClass::onPowerPost(AsyncWebServerRequest* request)
retMsg[F("type")] = F("success");
retMsg[F("message")] = F("Settings saved!");
retMsg[F("code")] = WebApiError::GenericSuccess;
response->setLength();
request->send(response);