I have written a driver in C++ for a peripheral device and in short, there is a number of errors that can occur during interaction. I have defined error codes for all the possible errors. I want to write a method that enables the user to query the meaning of error code.
Bear in mind that I have a total of 17 possible errors. The corresponding messages are of varying lengths.
I have solved it with a function that accepts the error code and returns a string with the error message. The function uses the error code to loop through a switch case routine each case returning a different error message as shown below.
std::string getErrorMessage(int errorCode)
{
std::string errorMessage = "no error" ;
switch(errorCode) {
case 0:
errorMessage = "no error" ;
break ;
case 10:
errorMessage = "there is a network error" ;
break ;
case 40:
errorMessage = "there is a network or protocol error" ;
break ;
default:
errorMessage = "unknown error" ;
}
return errorMessage ;
}
This function works, but it's not a "pretty" solution. Does anyone have any ideas or suggestions for a better solution?
strerror(errCode)?