Add error message

This commit is contained in:
badaix 2025-02-07 14:09:53 +01:00
parent 528e96919e
commit 94edf2df7c
5 changed files with 92 additions and 1 deletions

View file

@ -59,6 +59,7 @@
#include "browseZeroConf/browse_mdns.hpp"
#include "common/aixlog.hpp"
#include "common/message/client_info.hpp"
#include "common/message/error.hpp"
#include "common/message/hello.hpp"
#include "common/message/time.hpp"
#include "common/snap_exception.hpp"
@ -299,6 +300,11 @@ void Controller::getNextMessage()
player_->setVolume({serverSettings_->getVolume() / 100., serverSettings_->isMuted()});
// }
}
else if (response->type == message_type::kError)
{
auto error = msg::message_cast<msg::Error>(std::move(response));
LOG(ERROR, LOG_TAG) << "Received error: " << error->message << ", code: " << error->code << "\n";
}
else
{
LOG(WARNING, LOG_TAG) << "Unexpected message received, type: " << response->type << "\n";

68
common/message/error.hpp Normal file
View file

@ -0,0 +1,68 @@
/***
This file is part of snapcast
Copyright (C) 2014-2025 Johannes Pohl
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#pragma once
// local headers
#include "message.hpp"
namespace msg
{
/**
* Generic error message
*/
class Error : public BaseMessage
{
public:
/// c'tor taking the @p code and @p message of error
explicit Error(uint32_t code, std::string message) : BaseMessage(message_type::kError), code(code), message(std::move(message))
{
}
Error() : Error(0, "")
{
}
void read(std::istream& stream) override
{
readVal(stream, code);
readVal(stream, message);
}
uint32_t getSize() const override{
return static_cast<uint32_t>(sizeof(uint32_t) // code
+ sizeof(uint32_t) // message len
+ message.size()); // message;
}
/// error code
uint32_t code;
/// error message
std::string message;
protected:
void doserialize(std::ostream& stream) const override
{
writeVal(stream, code);
writeVal(stream, message);
}
};
} // namespace msg

View file

@ -21,6 +21,8 @@
// local headers
#include "client_info.hpp"
#include "codec_header.hpp"
#include "common/message/error.hpp"
#include "error.hpp"
#include "hello.hpp"
#include "pcm_chunk.hpp"
#include "server_settings.hpp"
@ -79,6 +81,8 @@ static std::unique_ptr<BaseMessage> createMessage(const BaseMessage& base_messag
return createMessage<PcmChunk>(base_message, buffer);
case message_type::kClientInfo:
return createMessage<ClientInfo>(base_message, buffer);
case message_type::kError:
return createMessage<msg::Error>(base_message, buffer);
default:
return nullptr;
}

View file

@ -66,9 +66,10 @@ enum class message_type : uint16_t
kHello = 5,
// kStreamTags = 6,
kClientInfo = 7,
kError = 8,
kFirst = kBase,
kLast = kClientInfo
kLast = kError
};
/// Message type to string
@ -97,6 +98,9 @@ static std::ostream& operator<<(std::ostream& os, const message_type& msg_type)
case message_type::kClientInfo:
os << "ClientInfo";
break;
case message_type::kError:
os << "Error";
break;
default:
os << "Unknown";
}

View file

@ -34,6 +34,7 @@ When a client joins a server, the following exchanges happen
| 4 | [Time](#time) | C->S<br>S->C | Used for synchronizing time with the server |
| 5 | [Hello](#hello) | C->S | Sent by the client when connecting with the server |
| 7 | [Client Info](#client-info) | C->S | Update the server when relevant information changes (e.g. client volume) |
| 8 | [Error](#error) | S->C | Error response, used e.g. for missing authentication |
### Base
@ -144,3 +145,11 @@ Sample JSON payload (whitespace added for readability):
```
- `volume` can have a value between 0-100 inclusive
### Error
| Field | Type | Description |
|---------|--------|----------------------------------------------------------|
| code | uint32 | Error code |
| size | uint32 | Size of the following error message |
| error | char[] | string containing the error (not null terminated) |