esp_exception.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #ifdef __cpp_exceptions
  16. #include "esp_err.h"
  17. #include <exception>
  18. namespace idf {
  19. /**
  20. * General exception class for exceptions on the ESP chips.
  21. * All throwing code in IDF should use either this exception directly or a sub-classes.
  22. */
  23. struct ESPException : public std::exception {
  24. ESPException(esp_err_t error);
  25. esp_err_t error;
  26. };
  27. /**
  28. * Convenience macro to help converting IDF error codes into ESPException.
  29. */
  30. #define CHECK_THROW(error_) \
  31. do { \
  32. esp_err_t result = error_; \
  33. if (result != ESP_OK) throw idf::ESPException(result); \
  34. } while (0)
  35. /**
  36. * Convenience macro to help converting IDF error codes into a child of ESPException.
  37. */
  38. #define CHECK_THROW_SPECIFIC(error_, exception_type_) \
  39. do { \
  40. esp_err_t result = error_; \
  41. if (result != ESP_OK) throw idf::exception_type_(result); \
  42. } while (0)
  43. } // namespace idf
  44. #endif // __cpp_exceptions