diff --git a/hueplusplus/BaseHttpHandler.cpp b/hueplusplus/BaseHttpHandler.cpp new file mode 100644 index 0000000..ad4f9c3 --- /dev/null +++ b/hueplusplus/BaseHttpHandler.cpp @@ -0,0 +1,114 @@ +/** + \file BaseHttpHandler.cpp + Copyright Notice\n + Copyright (C) 2020 Jan Rogall - developer\n + Copyright (C) 2020 Moritz Wirger - developer\n + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +**/ + +#include "BaseHttpHandler.h" + +#include "include/HueExceptionMacro.h" + +std::string BaseHttpHandler::sendGetHTTPBody(const std::string& msg, const std::string& adr, int port) const +{ + std::string response = send(msg, adr, port); + size_t start = response.find("\r\n\r\n"); + if (start == std::string::npos) + { + std::cerr << "BaseHttpHandler: Failed to find body in response\n"; + std::cerr << "Request:\n"; + std::cerr << "\"" << msg << "\"\n"; + std::cerr << "Response:\n"; + std::cerr << "\"" << response << "\"\n"; + throw HueException(CURRENT_FILE_INFO, "Failed to find body in response"); + } + response.erase(0, start + 4); + return response; +} + +std::string BaseHttpHandler::sendHTTPRequest(const std::string& method, const std::string& uri, + const std::string& contentType, const std::string& body, const std::string& adr, int port) const +{ + std::string request; + // Protocol reference: + // https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html Request-Line + request.append(method); // Method + request.append(" "); // Separation + request.append(uri); // Request-URI + request.append(" "); // Separation + request.append("HTTP/1.0"); // HTTP-Version + request.append("\r\n"); // Ending + // Entities + request.append("Content-Type:"); // entity-header + request.append(" "); // Separation + request.append(contentType); // media-type + request.append("\r\n"); // Entity ending + request.append("Content-Length:"); // entity-header + request.append(" "); // Separation + request.append(std::to_string(body.size())); // length + request.append("\r\n\r\n"); // Entity ending & Request-Line ending + request.append(body); // message-body + request.append("\r\n\r\n"); // Ending + + return sendGetHTTPBody(request.c_str(), adr, port); +} + +std::string BaseHttpHandler::GETString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port) const +{ + return sendHTTPRequest("GET", uri, contentType, body, adr, port); +} + +std::string BaseHttpHandler::POSTString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port) const +{ + return sendHTTPRequest("POST", uri, contentType, body, adr, port); +} + +std::string BaseHttpHandler::PUTString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port) const +{ + return sendHTTPRequest("PUT", uri, contentType, body, adr, port); +} + +std::string BaseHttpHandler::DELETEString(const std::string& uri, const std::string& contentType, + const std::string& body, const std::string& adr, int port) const +{ + return sendHTTPRequest("DELETE", uri, contentType, body, adr, port); +} + +nlohmann::json BaseHttpHandler::GETJson( + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const +{ + return nlohmann::json::parse(GETString(uri, "application/json", body.dump(), adr, port)); +} + +nlohmann::json BaseHttpHandler::POSTJson( + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const +{ + return nlohmann::json::parse(POSTString(uri, "application/json", body.dump(), adr, port)); +} + +nlohmann::json BaseHttpHandler::PUTJson( + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const +{ + return nlohmann::json::parse(PUTString(uri, "application/json", body.dump(), adr, port)); +} + +nlohmann::json BaseHttpHandler::DELETEJson( + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const +{ + return nlohmann::json::parse(DELETEString(uri, "application/json", body.dump(), adr, port)); +} diff --git a/hueplusplus/CMakeLists.txt b/hueplusplus/CMakeLists.txt index 0e5ee6e..10d6829 100755 --- a/hueplusplus/CMakeLists.txt +++ b/hueplusplus/CMakeLists.txt @@ -1,6 +1,7 @@ file(GLOB hueplusplus_HEADERS include/*.h include/*.hpp) set(hueplusplus_SOURCES - ${CMAKE_CURRENT_SOURCE_DIR}/ExtendedColorHueStrategy.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/BaseHttpHandler.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/ExtendedColorHueStrategy.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ExtendedColorTemperatureStrategy.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Hue.cpp ${CMAKE_CURRENT_SOURCE_DIR}/HueCommandAPI.cpp diff --git a/hueplusplus/HueException.cpp b/hueplusplus/HueException.cpp index be27ab7..6678fd2 100644 --- a/hueplusplus/HueException.cpp +++ b/hueplusplus/HueException.cpp @@ -19,7 +19,9 @@ #include "HueException.h" -HueException::HueException(FileInfo fileInfo) : HueException("Hue exception", std::move(fileInfo)) {} +HueException::HueException(FileInfo fileInfo, const std::string& message) + : HueException("Hue exception", std::move(fileInfo), message) +{} const char* HueException::what() const { @@ -31,27 +33,23 @@ const FileInfo& HueException::GetFile() const noexcept return fileInfo; } -HueException::HueException(const char* exceptionName, FileInfo fileInfo) : fileInfo(std::move(fileInfo)) +HueException::HueException(const char* exceptionName, FileInfo fileInfo, const std::string& message) + : fileInfo(std::move(fileInfo)) { whatMessage = exceptionName; whatMessage.append(" from"); whatMessage.append(this->fileInfo.ToString()); + whatMessage.append(" "); + whatMessage.append(message); } HueAPIResponseException::HueAPIResponseException( FileInfo fileInfo, int error, std::string address, std::string description) - : HueException("Hue api response exception", std::move(fileInfo)), + : HueException("Hue api response exception", std::move(fileInfo), GetMessage(error, address, description)), error(error), address(std::move(address)), description(std::move(description)) -{ - whatMessage.append(" "); - whatMessage.append(std::to_string(error)); - whatMessage.append(" "); - whatMessage.append(this->address); - whatMessage.append(" "); - whatMessage.append(this->description); -} +{} int HueAPIResponseException::GetErrorNumber() const noexcept { @@ -68,6 +66,16 @@ const std::string& HueAPIResponseException::GetDescription() const noexcept return description; } +std::string HueAPIResponseException::GetMessage(int error, const std::string& addr, const std::string& description) +{ + std::string result = std::to_string(error); + result.append(" "); + result.append(addr); + result.append(" "); + result.append(description); + return result; +} + std::string FileInfo::ToString() const { if (filename.empty() || line < 0) diff --git a/hueplusplus/include/BaseHttpHandler.h b/hueplusplus/include/BaseHttpHandler.h old mode 100755 new mode 100644 index 09f8230..3f1419a --- a/hueplusplus/include/BaseHttpHandler.h +++ b/hueplusplus/include/BaseHttpHandler.h @@ -1,8 +1,8 @@ /** \file BaseHttpHandler.h Copyright Notice\n - Copyright (C) 2017 Jan Rogall - developer\n - Copyright (C) 2017 Moritz Wirger - developer\n + Copyright (C) 2020 Jan Rogall - developer\n + Copyright (C) 2020 Moritz Wirger - developer\n This file is part of hueplusplus. @@ -40,233 +40,162 @@ public: //! \brief Virtual dtor virtual ~BaseHttpHandler() = default; - //! \brief Virtual function that should send a given message to a specified - //! host and return the response. + //! \brief Send a message to a specified host and return the response. //! - //! \param msg String that contains the message that should be sent to the - //! specified address \param adr String that contains an ip or hostname in - //! dotted decimal notation like "192.168.2.1" \param port Optional integer - //! that specifies the port to which the request should be sent to. Default is - //! 80 \return String containing the response of the host + //! \param msg The message that should be sent to the specified address + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return The response of the host as a string + //! \throws std::system_error when system or socket operations fail virtual std::string send(const std::string& msg, const std::string& adr, int port = 80) const = 0; - //! \brief Virtual function that should given message to a specified host and - //! return the body of the response. + //! \brief Send a message to a specified host and return the body of the response. //! - //! Note if no body is found a runtime error is thrown! - //! \param msg String that contains the message that should sent to the - //! specified address \param adr String that contains an ip or hostname in - //! dotted decimal notation like "192.168.2.1" \param port Optional integer - //! that specifies the port to which the request should be sent. Default is 80 - //! \return String containing the body of the response of the host - virtual std::string sendGetHTTPBody(const std::string& msg, const std::string& adr, int port = 80) const - { - std::string response = send(msg, adr, port); - size_t start = response.find("\r\n\r\n"); - if (start == std::string::npos) - { - std::cerr << "BaseHttpHandler: Failed to find body in response\n"; - std::cerr << "Request:\n"; - std::cerr << "\"" << msg << "\"\n"; - std::cerr << "Response:\n"; - std::cerr << "\"" << response << "\"\n"; - throw(std::runtime_error("BaseHttpHandler: Failed to find body in response")); - } - response.erase(0, start + 4); - return response; - }; - - //! \brief Virtual function that should send a multicast request with a - //! specified message. + //! \param msg The message that should sent to the specified address + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return The body of the response of the host as a string + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + virtual std::string sendGetHTTPBody(const std::string& msg, const std::string& adr, int port = 80) const; + + //! \brief Send a multicast request with a specified message. + //! + //! \param msg The message that should sent to the specified multicast address + //! \param adr Optional ip or hostname in dotted decimal notation, default is "239.255.255.250" + //! \param port Optional port the request is sent to, default is 1900 + //! \param timeout Optional time to wait for responses in seconds, default is 5 //! - //! \param msg String that contains the request that should be sent to the - //! specified address \param adr Optional String that contains an ip or - //! hostname in dotted decimal notation, default is "239.255.255.250" \param - //! port Optional integer that specifies the port to which the request should - //! be sent. Default is 1900 \param timeout Optional Integer that specifies - //! the timeout of the request in seconds. Default is 5 \return Vector - //! containing strings of each answer received + //! Blocks for the duration of the timeout. + //! + //! \return vector of strings containing each received answer + //! \throws std::system_error when system or socket operations fail virtual std::vector sendMulticast( const std::string& msg, const std::string& adr = "239.255.255.250", int port = 1900, int timeout = 5) const = 0; - //! \brief Virtual function that should send a HTTP request with the given - //! method to the specified host and return the body of the response. + //! \brief Send a HTTP request with the given method to the specified host and return the body of the response. //! - //! Note body can also be left empty! - //! \param method String that contains the HTTP method type e.g. GET, HEAD, - //! POST, PUT, DELETE, ... \param uri String that contains the uniform - //! resource identifier \param content_type String that contains the - //! type(MIME) of the body data e.g. "text/html", "application/json", ... - //! \param body String that contains the data of the request - //! \param adr String that contains an ip or hostname in dotted decimal - //! notation like "192.168.2.1" \param port Optional integer that specifies - //! the port to which the request is sent to. Default is 80 \return String - //! containing the body of the response of the host - virtual std::string sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, - const std::string& adr, int port = 80) const - { - std::string request; - // Protocol reference: - // https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html Request-Line - request.append(method); // Method - request.append(" "); // Separation - request.append(uri); // Request-URI - request.append(" "); // Separation - request.append("HTTP/1.0"); // HTTP-Version - request.append("\r\n"); // Ending - // Entities - request.append("Content-Type:"); // entity-header - request.append(" "); // Separation - request.append(content_type); // media-type - request.append("\r\n"); // Entity ending - request.append("Content-Length:"); // entity-header - request.append(" "); // Separation - request.append(std::to_string(body.size())); // length - request.append("\r\n\r\n"); // Entity ending & Request-Line ending - request.append(body); // message-body - request.append("\r\n\r\n"); // Ending - - return sendGetHTTPBody(request.c_str(), adr, port); - }; - - //! \brief Virtual function that should send a HTTP GET request to the - //! specified host and return the body of the response. + //! \param method HTTP method type e.g. GET, HEAD, POST, PUT, DELETE, ... + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + virtual std::string sendHTTPRequest(const std::string& method, const std::string& uri, + const std::string& contentType, const std::string& body, const std::string& adr, int port = 80) const; + + //! \brief Send a HTTP GET request to the specified host and return the body of the response. //! - //! Note body can also be left empty! - //! \param uri String that contains the uniform resource identifier - //! \param content_type String that contains the type(MIME) of the body data - //! e.g. "text/html", "application/json", ... \param body String that contains - //! the data of the request \param adr String that contains an ip or hostname - //! in dotted decimal notation like "192.168.2.1" \param port Optional integer + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 //! that specifies the port to which the request is sent to. Default is 80 - //! \return String containing the body of the response of the host - virtual std::string GETString( - std::string uri, std::string content_type, std::string body, const std::string& adr, int port = 80) const - { - return sendHTTPRequest("GET", uri, content_type, body, adr, port); - }; + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + virtual std::string GETString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port = 80) const; - //! \brief Virtual function that should send a HTTP POST request to the - //! specified host and returns the body of the response. + //! \brief Send a HTTP POST request to the specified host and return the body of the response. //! - //! Note body can also be left empty! - //! \param uri String that contains the uniform resource identifier - //! \param content_type String that contains the type(MIME) of the body data - //! e.g. "text/html", "application/json", ... \param body String that contains - //! the data of the request \param adr String that contains an ip or hostname - //! in dotted decimal notation like "192.168.2.1" \param port Optional integer + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 //! that specifies the port to which the request is sent to. Default is 80 - //! \return String containing the body of the response of the host - virtual std::string POSTString( - std::string uri, std::string content_type, std::string body, const std::string& adr, int port = 80) const - { - return sendHTTPRequest("POST", uri, content_type, body, adr, port); - }; + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + virtual std::string POSTString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port = 80) const; - //! \brief Virtual function that should send a HTTP PUT request to the - //! specified host and return the body of the response. + //! \brief Send a HTTP PUT request to the specified host and return the body of the response. //! - //! Note body can also be left empty! - //! \param uri String that contains the uniform resource identifier - //! \param content_type String that contains the type(MIME) of the body data - //! e.g. "text/html", "application/json", ... \param body String that contains - //! the data of the request \param adr String that contains an ip or hostname - //! in dotted decimal notation like "192.168.2.1" \param port Optional integer + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 //! that specifies the port to which the request is sent to. Default is 80 - //! \return String containing the body of the response of the host - virtual std::string PUTString( - std::string uri, std::string content_type, std::string body, const std::string& adr, int port = 80) const - { - return sendHTTPRequest("PUT", uri, content_type, body, adr, port); - }; + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + virtual std::string PUTString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port = 80) const; - //! \brief Virtual function that should send a HTTP DELETE request to the - //! specified host and return the body of the response. + //! \brief Send a HTTP DELETE request to the specified host and return the body of the response. //! - //! Note body can also be left empty! - //! \param uri String that contains the uniform resource identifier - //! \param content_type String that contains the type(MIME) of the body data - //! e.g. "text/html", "application/json", ... \param body String that contains - //! the data of the request \param adr String that contains an ip or hostname - //! in dotted decimal notation like "192.168.2.1" \param port Optional integer + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 //! that specifies the port to which the request is sent to. Default is 80 - //! \return String containing the body of the response of the host - virtual std::string DELETEString( - std::string uri, std::string content_type, std::string body, const std::string& adr, int port = 80) const - { - return sendHTTPRequest("DELETE", uri, content_type, body, adr, port); - }; + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + virtual std::string DELETEString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port = 80) const; - //! \brief Virtual function that should send a HTTP GET request to the - //! specified host and return the body of the response. + //! \brief Send a HTTP GET request to the specified host and return the body of the response parsed as JSON. //! - //! Note body can also be left empty! - //! \param uri String that contains the uniform resource identifier - //! \param body nlohmann::json that contains the data of the request - //! \param adr String that contains an ip or hostname in dotted decimal - //! notation like "192.168.2.1" \param port Optional integer that specifies - //! the port to which the request is sent to. Default is 80 \return - //! nlohmann::json containing the parsed body of the response of the host + //! \param uri Uniform Resource Identifier in the request + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Parsed body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws nlohmann::json::parse_error when the body could not be parsed virtual nlohmann::json GETJson( - std::string uri, const nlohmann::json& body, const std::string& adr, int port = 80) const - { - return strToJsonValue(GETString(uri, "application/json", body.dump(), adr, port)); - }; + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const; + ; - //! \brief Virtual function that should send a HTTP POST request to the - //! specified host and return the body of the response. + //! \brief Send a HTTP POST request to the specified host and return the body of the response parsed as JSON. //! - //! Note body can also be left empty! - //! \param uri String that contains the uniform resource identifier - //! \param body nlohmann::json that contains the data of the request - //! \param adr String that contains an ip or hostname in dotted decimal - //! notation like "192.168.2.1" \param port Optional integer that specifies - //! the port to which the request is sent to. Default is 80 \return - //! nlohmann::json containing the parsed body of the response of the host + //! \param uri Uniform Resource Identifier in the request + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Parsed body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws nlohmann::json::parse_error when the body could not be parsed virtual nlohmann::json POSTJson( - std::string uri, const nlohmann::json& body, const std::string& adr, int port = 80) const - { - return strToJsonValue(POSTString(uri, "application/json", body.dump(), adr, port)); - } + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const; - //! \brief Virtual function that should send a HTTP PUT request to the - //! specified host and return the body of the response. + //! \brief Send a HTTP PUT request to the specified host and return the body of the response parsed as JSON. //! - //! Note body can also be left empty! - //! \param uri String that contains the uniform resource identifier - //! \param body nlohmann::json that contains the data of the request - //! \param adr String that contains an ip or hostname in dotted decimal - //! notation like "192.168.2.1" \param port Optional integer that specifies - //! the port to which the request is sent to. Default is 80 \return - //! nlohmann::json containing the parsed body of the response of the host + //! \param uri Uniform Resource Identifier in the request + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Parsed body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws nlohmann::json::parse_error when the body could not be parsed virtual nlohmann::json PUTJson( - std::string uri, const nlohmann::json& body, const std::string& adr, int port = 80) const - { - return strToJsonValue(PUTString(uri, "application/json", body.dump(), adr, port)); - }; + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const; - //! \brief Virtual function that should send a HTTP DELETE request to the - //! specified host and return the body of the response. + //! \brief Send a HTTP DELETE request to the specified host and return the body of the response parsed as JSON. //! - //! Note body can also be left empty! - //! \param uri String that contains the uniform resource identifier - //! \param body nlohmann::json that contains the data of the request - //! \param adr String that contains an ip or hostname in dotted decimal - //! notation like "192.168.2.1" \param port Optional integer that specifies - //! the port to which the request is sent to. Default is 80 \return - //! nlohmann::json containing the parsed body of the response of the host + //! \param uri Uniform Resource Identifier in the request + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Parsed body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws nlohmann::json::parse_error when the body could not be parsed virtual nlohmann::json DELETEJson( - std::string uri, const nlohmann::json& body, const std::string& adr, int port = 80) const - { - return strToJsonValue(DELETEString(uri, "application/json", body.dump(), adr, port)); - }; - -private: - //! \brief Function that converts a given string to a nlohmann::json - //! - //! \param str String that gets converted - //! \return nlohmann::json containing parsed string - nlohmann::json strToJsonValue(std::string str) const { return nlohmann::json::parse(str); } + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const; + ; }; #endif diff --git a/hueplusplus/include/HueException.h b/hueplusplus/include/HueException.h index d058687..11eb232 100644 --- a/hueplusplus/include/HueException.h +++ b/hueplusplus/include/HueException.h @@ -35,7 +35,7 @@ class HueException : public std::exception { public: //! \brief Creates HueException with information about the thrown location - HueException(FileInfo fileInfo); + HueException(FileInfo fileInfo, const std::string& message); const char* what() const override; @@ -46,10 +46,10 @@ protected: //! \brief Creates HueException with child class name //! //! Should be used by subclasses which can append additional information to the end of whatMessage. - HueException(const char* exceptionName, FileInfo fileInfo); - std::string whatMessage; + HueException(const char* exceptionName, FileInfo fileInfo, const std::string& message); private: + std::string whatMessage; FileInfo fileInfo; }; @@ -63,6 +63,9 @@ public: const std::string& GetDescription() const noexcept; private: + static std::string GetMessage(int error, const std::string& addr, const std::string& description); + +private: int error; std::string address; std::string description; diff --git a/hueplusplus/include/HueLight.h b/hueplusplus/include/HueLight.h index dc278cb..b65079a 100644 --- a/hueplusplus/include/HueLight.h +++ b/hueplusplus/include/HueLight.h @@ -649,7 +649,7 @@ protected: //! \brief Utility function to send a put request to the light. //! - //! \throws std::runtime_error if the reply could not be parsed + //! \throws nlohmann::json::parse_error if the reply could not be parsed //! \param request A nlohmann::json aka the request to send //! \param subPath A path that is appended to the uri, note it should always //! start with a slash ("/") \return The parsed reply