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 15 #pragma once 16 17 #ifdef __cpp_exceptions 18 19 #include "esp_err.h" 20 #include <exception> 21 22 namespace idf { 23 24 /** 25 * @brief 26 * General exception class for all C++ exceptions in IDF. 27 * 28 * All throwing code in IDF should use either this exception directly or a sub-classes. 29 * An error from the underlying IDF function is mandatory. The idea is to wrap the orignal IDF error code to keep 30 * the error scheme partially compatible. If an exception occurs in a higher level C++ code not directly wrapping 31 * IDF functions, an appropriate error code reflecting the cause must be chosen or newly created. 32 */ 33 class ESPException : public std::exception { 34 public: 35 /** 36 * @param error Error from underlying IDF functions. 37 */ 38 ESPException(esp_err_t error); 39 ~ESPException()40 virtual ~ESPException() { } 41 42 /** 43 * @return A textual representation of the contained error. This method only wraps \c esp_err_to_name. 44 */ 45 virtual const char *what() const noexcept; 46 47 /** 48 * Error from underlying IDF functions. If an exception occurs in a higher level C++ code not directly wrapping 49 * IDF functions, an appropriate error code reflecting the cause must be chosen or newly created. 50 */ 51 const esp_err_t error; 52 }; 53 54 /** 55 * Convenience macro to help converting IDF error codes into ESPException. 56 */ 57 #define CHECK_THROW(error_) \ 58 do { \ 59 esp_err_t result = error_; \ 60 if (result != ESP_OK) throw idf::ESPException(result); \ 61 } while (0) 62 63 /** 64 * Convenience macro to help converting IDF error codes into a child of ESPException. 65 */ 66 #define CHECK_THROW_SPECIFIC(error_, exception_type_) \ 67 do { \ 68 esp_err_t result = error_; \ 69 if (result != ESP_OK) throw idf::exception_type_(result); \ 70 } while (0) 71 72 } // namespace idf 73 74 #endif // __cpp_exceptions 75