Introduce numeric error codes in dtu webapi
This commit is contained in:
parent
ab25914c48
commit
3b5ff1691e
16
include/WebApi_errors.h
Normal file
16
include/WebApi_errors.h
Normal 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,
|
||||||
|
};
|
||||||
@ -5,6 +5,7 @@
|
|||||||
#include "WebApi_dtu.h"
|
#include "WebApi_dtu.h"
|
||||||
#include "Configuration.h"
|
#include "Configuration.h"
|
||||||
#include "WebApi.h"
|
#include "WebApi.h"
|
||||||
|
#include "WebApi_errors.h"
|
||||||
#include <AsyncJson.h>
|
#include <AsyncJson.h>
|
||||||
#include <Hoymiles.h>
|
#include <Hoymiles.h>
|
||||||
|
|
||||||
@ -57,6 +58,7 @@ void WebApiDtuClass::onDtuAdminPost(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;
|
||||||
@ -66,6 +68,7 @@ void WebApiDtuClass::onDtuAdminPost(AsyncWebServerRequest* request)
|
|||||||
|
|
||||||
if (json.length() > 1024) {
|
if (json.length() > 1024) {
|
||||||
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;
|
||||||
@ -76,6 +79,7 @@ void WebApiDtuClass::onDtuAdminPost(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;
|
||||||
@ -83,6 +87,7 @@ void WebApiDtuClass::onDtuAdminPost(AsyncWebServerRequest* request)
|
|||||||
|
|
||||||
if (!(root.containsKey("dtu_serial") && root.containsKey("dtu_pollinterval") && root.containsKey("dtu_palevel"))) {
|
if (!(root.containsKey("dtu_serial") && root.containsKey("dtu_pollinterval") && root.containsKey("dtu_palevel"))) {
|
||||||
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;
|
||||||
@ -90,6 +95,7 @@ void WebApiDtuClass::onDtuAdminPost(AsyncWebServerRequest* request)
|
|||||||
|
|
||||||
if (root[F("dtu_serial")].as<uint64_t>() == 0) {
|
if (root[F("dtu_serial")].as<uint64_t>() == 0) {
|
||||||
retMsg[F("message")] = F("Serial cannot be zero!");
|
retMsg[F("message")] = F("Serial cannot be zero!");
|
||||||
|
retMsg[F("code")] = WebApiError::DtuSerialZero;
|
||||||
response->setLength();
|
response->setLength();
|
||||||
request->send(response);
|
request->send(response);
|
||||||
return;
|
return;
|
||||||
@ -97,6 +103,7 @@ void WebApiDtuClass::onDtuAdminPost(AsyncWebServerRequest* request)
|
|||||||
|
|
||||||
if (root[F("dtu_pollinterval")].as<uint32_t>() == 0) {
|
if (root[F("dtu_pollinterval")].as<uint32_t>() == 0) {
|
||||||
retMsg[F("message")] = F("Poll interval must be greater zero!");
|
retMsg[F("message")] = F("Poll interval must be greater zero!");
|
||||||
|
retMsg[F("code")] = WebApiError::DtuPollZero;
|
||||||
response->setLength();
|
response->setLength();
|
||||||
request->send(response);
|
request->send(response);
|
||||||
return;
|
return;
|
||||||
@ -104,6 +111,7 @@ void WebApiDtuClass::onDtuAdminPost(AsyncWebServerRequest* request)
|
|||||||
|
|
||||||
if (root[F("dtu_palevel")].as<uint8_t>() > 3) {
|
if (root[F("dtu_palevel")].as<uint8_t>() > 3) {
|
||||||
retMsg[F("message")] = F("Invalid power level setting!");
|
retMsg[F("message")] = F("Invalid power level setting!");
|
||||||
|
retMsg[F("code")] = WebApiError::DtuInvalidPowerLevel;
|
||||||
response->setLength();
|
response->setLength();
|
||||||
request->send(response);
|
request->send(response);
|
||||||
return;
|
return;
|
||||||
@ -119,6 +127,7 @@ void WebApiDtuClass::onDtuAdminPost(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);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user