HueException.cpp
3.17 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
/**
\file HueException.cpp
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/>.
**/
#include "hueplusplus/HueException.h"
namespace hueplusplus
{
HueException::HueException(FileInfo fileInfo, const std::string& message)
: HueException("HueException", std::move(fileInfo), message)
{}
const char* HueException::what() const noexcept
{
return whatMessage.c_str();
}
const FileInfo& HueException::GetFile() const noexcept
{
return 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("HueApiResponseException", std::move(fileInfo), GetMessage(error, address, description)),
error(error),
address(std::move(address)),
description(std::move(description))
{}
int HueAPIResponseException::GetErrorNumber() const noexcept
{
return error;
}
const std::string& HueAPIResponseException::GetAddress() const noexcept
{
return address;
}
const std::string& HueAPIResponseException::GetDescription() const noexcept
{
return description;
}
HueAPIResponseException HueAPIResponseException::Create(FileInfo fileInfo, const nlohmann::json& response)
{
const nlohmann::json error = response.at("error");
int errorCode = error.value("type", -1);
std::string address = error.value("address", "");
std::string description = error.value("description", "");
return HueAPIResponseException(std::move(fileInfo), errorCode, std::move(address), std::move(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)
{
return "Unknown file";
}
std::string result = func;
result.append(" in ");
result.append(filename);
result.append(":");
result.append(std::to_string(line));
return result;
}
} // namespace hueplusplus