mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-10 01:39:54 +03:00
error handling improvements - Refactor MQTT subscriptions #173
This commit is contained in:
@@ -80,16 +80,16 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject & input) {
|
||||
JsonObject output = response->getRoot();
|
||||
|
||||
// call command
|
||||
uint8_t command_ret = Command::process(request->url().c_str(), authenticated, input, output);
|
||||
uint8_t return_code = Command::process(request->url().c_str(), authenticated, input, output);
|
||||
|
||||
// handle response codes
|
||||
// the output will be populated with a message key if an error occurred
|
||||
int ret_code;
|
||||
if (command_ret == CommandRet::NOT_ALLOWED) {
|
||||
if (return_code == CommandRet::NOT_ALLOWED) {
|
||||
ret_code = 401; // Unauthorized
|
||||
} else if (command_ret == CommandRet::NOT_FOUND) {
|
||||
} else if (return_code == CommandRet::NOT_FOUND) {
|
||||
ret_code = 400; // Bad request
|
||||
} else if (command_ret == CommandRet::OK) {
|
||||
} else if (return_code == CommandRet::OK) {
|
||||
ret_code = 200; // OK
|
||||
// if there was not json output from the call, default to the message 'OK'.
|
||||
// this will be used for example when calling endpoints e.g. /api/thermostat/temp
|
||||
@@ -106,9 +106,20 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject & input) {
|
||||
response->setContentType("application/json");
|
||||
request->send(response); // send json response
|
||||
|
||||
// do a log entry if there is an error
|
||||
if (return_code != CommandRet::OK) {
|
||||
char error[100];
|
||||
if (output.size()) {
|
||||
snprintf(error, sizeof(error), "Call failed with error: %s (%s)", (const char *)output["message"], Command::return_code_string(return_code).c_str());
|
||||
} else {
|
||||
snprintf(error, sizeof(error), "Call failed with error code (%s)", Command::return_code_string(return_code).c_str());
|
||||
}
|
||||
emsesp::EMSESP::logger().err(error);
|
||||
}
|
||||
|
||||
#if defined(EMSESP_STANDALONE)
|
||||
Serial.print(COLOR_YELLOW);
|
||||
Serial.print("return code: ");
|
||||
Serial.print("web response code: ");
|
||||
Serial.println(ret_code);
|
||||
if (output.size()) {
|
||||
serializeJsonPretty(output, Serial);
|
||||
|
||||
@@ -146,28 +146,28 @@ void WebDataService::write_value(AsyncWebServerRequest * request, JsonVariant &
|
||||
// the data could be in any format, but we need string
|
||||
// authenticated is always true
|
||||
JsonVariant data = dv["v"]; // the value in any format
|
||||
uint8_t command_ret = CommandRet::OK;
|
||||
uint8_t return_code = CommandRet::OK;
|
||||
uint8_t device_type = emsdevice->device_type();
|
||||
if (data.is<const char *>()) {
|
||||
command_ret = Command::call(device_type, cmd, data.as<const char *>(), true, id, output);
|
||||
return_code = Command::call(device_type, cmd, data.as<const char *>(), true, id, output);
|
||||
} else if (data.is<int>()) {
|
||||
char s[10];
|
||||
command_ret = Command::call(device_type, cmd, Helpers::render_value(s, data.as<int16_t>(), 0), true, id, output);
|
||||
return_code = Command::call(device_type, cmd, Helpers::render_value(s, data.as<int16_t>(), 0), true, id, output);
|
||||
} else if (data.is<float>()) {
|
||||
char s[10];
|
||||
command_ret = Command::call(device_type, cmd, Helpers::render_value(s, (float)data.as<float>(), 1), true, id, output);
|
||||
return_code = Command::call(device_type, cmd, Helpers::render_value(s, (float)data.as<float>(), 1), true, id, output);
|
||||
} else if (data.is<bool>()) {
|
||||
command_ret = Command::call(device_type, cmd, data.as<bool>() ? "true" : "false", true, id, output);
|
||||
return_code = Command::call(device_type, cmd, data.as<bool>() ? "true" : "false", true, id, output);
|
||||
}
|
||||
|
||||
// write debug
|
||||
if (command_ret != CommandRet::OK) {
|
||||
EMSESP::logger().err(F("Write command failed %s (%d)"), (const char *)output["message"], command_ret);
|
||||
if (return_code != CommandRet::OK) {
|
||||
EMSESP::logger().err(F("Write command failed %s (%s)"), (const char *)output["message"], Command::return_code_string(return_code).c_str());
|
||||
} else {
|
||||
EMSESP::logger().debug(F("Write command successful"));
|
||||
}
|
||||
|
||||
response->setCode((command_ret == CommandRet::OK) ? 200 : 204);
|
||||
response->setCode((return_code == CommandRet::OK) ? 200 : 204);
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user