improved error message

This commit is contained in:
faraphel 2024-10-22 12:10:45 +02:00
parent 33033b0c96
commit 21f14948b9

View file

@ -20,7 +20,7 @@ Client::Client(const int channels, const double rate) {
// TODO(Faraphel): make the sampleFormat an argument.
// open a PortAudio stream
if (Pa_OpenDefaultStream(
if (const PaError error = Pa_OpenDefaultStream(
&this->stream,
0,
channels,
@ -30,13 +30,15 @@ Client::Client(const int channels, const double rate) {
nullptr,
nullptr
) != paNoError)
throw std::runtime_error("[Client] Could not open PortAudio stream.");
throw std::runtime_error("[Client] Could not open the stream: " + std::string(Pa_GetErrorText(error)));
}
Client::~Client() {
// close the audio stream
// stop any currently playing audio
Pa_StopStream(this->stream);
Pa_CloseStream(this->stream);
// close the audio stream
if (const PaError error = Pa_CloseStream(this->stream))
std::cerr << "[Client] Could not close the stream: " << std::string(Pa_GetErrorText(error)) << std::endl;
}
@ -57,7 +59,7 @@ void Client::loopReceiver() {
0
);
if (clientSocket < 0)
throw std::runtime_error("[Client] Could not create the socket." + std::string(gai_strerror(errno)));
throw std::runtime_error("[Client] Could not create the socket: " + std::string(gai_strerror(errno)));
// get the broadcast address
addrinfo serverHints = {};
@ -81,7 +83,7 @@ void Client::loopReceiver() {
serverInfo->ai_addr,
serverInfo->ai_addrlen
) < 0)
throw std::runtime_error("[Client] Could not bind to the address." + std::string(gai_strerror(errno)));
throw std::runtime_error("[Client] Could not bind to the address: " + std::string(gai_strerror(errno)));
// free the server address
freeaddrinfo(serverInfo);
@ -157,7 +159,7 @@ void Client::loopPlayer() {
break;
default:
std::cerr << "[Client] Could not write to the PortAudio stream: " << Pa_GetErrorText(error) << std::endl;
std::cerr << "[Client] Could not write to the audio stream: " << Pa_GetErrorText(error) << std::endl;
}
// remove the audio chunk