Quellcode durchsuchen

KBF-31 make TaskList error handling more strict

Bence Balint vor 3 Jahren
Ursprung
Commit
559b157be4
3 geänderte Dateien mit 22 neuen und 8 gelöschten Zeilen
  1. 18 4
      include/kbf/exception.h
  2. 1 1
      include/kbf/task.h
  3. 3 3
      src/task.cpp

+ 18 - 4
include/kbf/exception.h

@@ -3,16 +3,29 @@
 
 #include <exception>
 #include <string>
+#include <utility>
 
 #include <esp_err.h>
 
+class KBFError : public std::exception {
+public:
+    explicit KBFError(const std::string &message) : message("KBF error: " + message) {}
+
+    const std::string message;
+
+    [[nodiscard]] const char *what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT override {
+        return message.c_str();
+    }
+};
+
 class ESPError : public std::exception {
     const esp_err_t err;
 public:
     explicit ESPError(const esp_err_t err) : err(err) {};
 
-    [[nodiscard]] const char *
-    what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT override { return esp_err_to_name(err); }
+    [[nodiscard]] const char *what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT override {
+        return esp_err_to_name(err);
+    }
 };
 
 class RTOSError : public std::exception {
@@ -20,8 +33,9 @@ class RTOSError : public std::exception {
 public:
     explicit RTOSError(const uint32_t err) : message("RTOS error: " + std::to_string(err)) {};
 
-    [[nodiscard]] const char *
-    what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT override { return message.c_str(); }
+    [[nodiscard]] const char *what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT override {
+        return message.c_str();
+    }
 };
 
 class OutOfMemoryError : public std::exception {

+ 1 - 1
include/kbf/task.h

@@ -105,7 +105,7 @@ namespace kbf::task {
 
             static void add(const std::shared_ptr<Task> &task);
 
-            static bool remove(uint32_t id);
+            static void remove(uint32_t id);
         };
     };
 

+ 3 - 3
src/task.cpp

@@ -17,16 +17,16 @@ void Task::TaskList::add(const shared_ptr<Task>& task) {
     tasks.push_back(task);
 };
 
-bool Task::TaskList::remove(uint32_t id) {
+void Task::TaskList::remove(uint32_t id) {
     ESP_LOGD(TAG, "%s(%d)", __func__, id);
     for (auto it = tasks.begin(); it != tasks.end(); it++) {
         if ((*it)->id == id) {
             tasks.erase(it);
-            return true;
+            return;
         }
     }
     ESP_LOGE(TAG, "%s(%d): not found", __func__, id);
-    return false;
+    throw KBFError("TaskList::remove(" + std::to_string(id) + ") failed");
 }
 
 Task::Task(string name, void *arg, const uint32_t stackSize, const uint32_t priority) :