Introduce numeric error codes in dtu webapi

This commit is contained in:
Thomas Basler 2022-12-23 17:46:26 +01:00
parent ab25914c48
commit 3b5ff1691e
2 changed files with 25 additions and 0 deletions

16
include/WebApi_errors.h Normal file
View File

@ -0,0 +1,16 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
enum WebApiError {
GenericBase = 1000,
GenericSuccess,
GenericNoValueFound,
GenericDataTooLarge,
GenericParseError,
GenericValueMissing,
DtuBase = 2000,
DtuSerialZero,
DtuPollZero,
DtuInvalidPowerLevel,
};

View File

@ -5,6 +5,7 @@
#include "WebApi_dtu.h"
#include "Configuration.h"
#include "WebApi.h"
#include "WebApi_errors.h"
#include <AsyncJson.h>
#include <Hoymiles.h>
@ -57,6 +58,7 @@ void WebApiDtuClass::onDtuAdminPost(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;
@ -66,6 +68,7 @@ void WebApiDtuClass::onDtuAdminPost(AsyncWebServerRequest* request)
if (json.length() > 1024) {
retMsg[F("message")] = F("Data too large!");
retMsg[F("code")] = WebApiError::GenericDataTooLarge;
response->setLength();
request->send(response);
return;
@ -76,6 +79,7 @@ void WebApiDtuClass::onDtuAdminPost(AsyncWebServerRequest* request)
if (error) {
retMsg[F("message")] = F("Failed to parse data!");
retMsg[F("code")] = WebApiError::GenericParseError;
response->setLength();
request->send(response);
return;
@ -83,6 +87,7 @@ void WebApiDtuClass::onDtuAdminPost(AsyncWebServerRequest* request)
if (!(root.containsKey("dtu_serial") && root.containsKey("dtu_pollinterval") && root.containsKey("dtu_palevel"))) {
retMsg[F("message")] = F("Values are missing!");
retMsg[F("code")] = WebApiError::GenericValueMissing;
response->setLength();
request->send(response);
return;
@ -90,6 +95,7 @@ void WebApiDtuClass::onDtuAdminPost(AsyncWebServerRequest* request)
if (root[F("dtu_serial")].as<uint64_t>() == 0) {
retMsg[F("message")] = F("Serial cannot be zero!");
retMsg[F("code")] = WebApiError::DtuSerialZero;
response->setLength();
request->send(response);
return;
@ -97,6 +103,7 @@ void WebApiDtuClass::onDtuAdminPost(AsyncWebServerRequest* request)
if (root[F("dtu_pollinterval")].as<uint32_t>() == 0) {
retMsg[F("message")] = F("Poll interval must be greater zero!");
retMsg[F("code")] = WebApiError::DtuPollZero;
response->setLength();
request->send(response);
return;
@ -104,6 +111,7 @@ void WebApiDtuClass::onDtuAdminPost(AsyncWebServerRequest* request)
if (root[F("dtu_palevel")].as<uint8_t>() > 3) {
retMsg[F("message")] = F("Invalid power level setting!");
retMsg[F("code")] = WebApiError::DtuInvalidPowerLevel;
response->setLength();
request->send(response);
return;
@ -119,6 +127,7 @@ void WebApiDtuClass::onDtuAdminPost(AsyncWebServerRequest* request)
retMsg[F("type")] = F("success");
retMsg[F("message")] = F("Settings saved!");
retMsg[F("code")] = WebApiError::GenericSuccess;
response->setLength();
request->send(response);