As mentioned in the comments, you should know the error codes that can be received from the remote server.
std::stringthat you get from a remote server contains 2 parts, as you said,
The problem is that they are identified by lines, for example, "0A01", and also contain a message, and the error code requires an integer value.
Since you did not share the format of the error message, I am not adding code to grind it, divide the line into 2 parts,
Now you can convert the type of error code std::stringin the intusing std::stoi(error_code), so let's say
int error_code_int = std::stoi(string_to_hexadecimal(error_code));
And for std::error_category, which serves as the base class for our custom error messages, do this,
std::string message_received = "This is the message which received from remote server.";
struct OurCustomErrCategory : std::error_category
{
const char* name() const noexcept override;
std::string message(int ev) const override;
};
const char* OurCustomErrCategory::name() const noexcept
{
return "Error Category Name";
}
std::string OurCustomErrCategory::message(int error_code_int) const
{
switch (error_code_int)
{
case 1:
return message_received;
default:
return "(unrecognized error)";
}
}
const OurCustomErrCategory ourCustomErrCategoryObject;
std::error_code make_error_code(int e)
{
return {e, ourCustomErrCategoryObject};
}
int main()
{
int error_code_int = std::stoi(string_to_hexadecimal(error_code));
ourCustomErrCategoryObject.message(error_code_int);
std::error_code ec(error_code_int , ourCustomErrCategoryObject);
assert(ec);
std::cout << ec << std::endl;
std::cout << ec.message() << std::endl;
}
The output for the above working example
Error Category Name : 0A01
This is the message which received from remote server.
string_to_hexadecimal() .
, .
1:
,
, . std::error_category?
, std::error_code::assign std::error_code::error_code int error_category. , , std::error_code .
, , std::error_code error_category , , ?
std::error_category , :
std::error_category .
, , struct std::error_category
struct OurCustomErrCategory : std::error_category
, -, struct ,
struct OurCustomErrCategory : std::error_category
{
std::string message_received;
OurCustomErrCategory(std::string m) : message_received(m) {}
const char* name() const noexcept override;
std::string message(int ev) const override;
};
, ,
const OurCustomErrCategory ourCustomErrCategoryObject("This is the message which received from remote server.");