HueException.h
4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
\file HueException.h
Copyright Notice\n
Copyright (C) 2020 Jan Rogall - developer\n
Copyright (C) 2020 Moritz Wirger - developer\n
This file is part of hueplusplus.
hueplusplus is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
hueplusplus 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
**/
#ifndef INCLUDE_HUEPLUSPLUS_HUE_EXCEPTION_H
#define INCLUDE_HUEPLUSPLUS_HUE_EXCEPTION_H
#include <exception>
#include <string>
#include "json/json.hpp"
namespace hueplusplus
{
//! \brief Contains information about error location, use \ref CURRENT_FILE_INFO to create
struct FileInfo
{
//! \brief Current file name from __FILE__. Empty if unknown
std::string filename;
//! \brief Current line number from __LINE__. -1 if unknown
int line = -1;
//! \brief Current function from __func__. Empty if unknown
std::string func;
//! \brief String representation of func, file and line.
//! \returns "<func> in <filename>:<line>" or "Unknown file" if unknown.
std::string ToString() const;
};
//! \brief Exception class with file information. Base class of all custom exception classes
class HueException : public std::exception
{
public:
//! \brief Creates HueException with information about the error and source
//! \param fileInfo Source of the error. Must not always be the throw location,
//! can also be a calling function which matches the cause better.
//! \param message Human readable error message.
HueException(FileInfo fileInfo, const std::string& message);
//! \brief What message of the exception
//! \returns exception name, file info and constructor message as char* into member string
const char* what() const noexcept override;
//! \brief Filename and line where the exception was thrown or caused by
const FileInfo& GetFile() const noexcept;
protected:
//! \brief Creates HueException with child class name
//!
//! Should be used by subclasses which can append additional information to the end of whatMessage.
//! \param exceptionName class name of the subclass
//! \param fileInfo Source of the error. Must not always be the throw location,
//! can also be a calling function which matches the cause better.
//! \param message Human readable error message
HueException(const char* exceptionName, FileInfo fileInfo, const std::string& message);
private:
std::string whatMessage;
FileInfo fileInfo;
};
//! \brief Exception caused by a Hue API "error" response with additional information
//!
//! Refer to Hue developer documentation for more detail on specific error codes.
class HueAPIResponseException : public HueException
{
public:
//! \brief Create exception with info from Hue API error
//! \param fileInfo Source of the error. Must not always be the throw location,
//! can also be a calling function which matches the cause better.
//! \param error Hue API error code from error response.
//! \param address URI the API call referred to from error response.
//! \param description Error description from response.
HueAPIResponseException(FileInfo fileInfo, int error, std::string address, std::string description);
//! \brief Error number from Hue API error response.
//!
//! Refer to Hue developer documentation for meaning of error codes.
int GetErrorNumber() const noexcept;
//! \brief Address the API call tried to access.
const std::string& GetAddress() const noexcept;
//! \brief Error description
const std::string& GetDescription() const noexcept;
//! \brief Creates exception from API response.
//! \param fileInfo Location of the cause
//! \param response Hue API response. Must contain a member "error" with "type", "address" and "description".
//! \returns HueAPIResponseException with info from the response.
//! If response does not contain the required members, they are defaulted to -1 or "".
static HueAPIResponseException Create(FileInfo fileInfo, const nlohmann::json& response);
private:
//! \brief Creates exception message containing the given information
static std::string GetMessage(int error, const std::string& addr, const std::string& description);
private:
int error;
std::string address;
std::string description;
};
} // namespace hueplusplus
#endif