input/tidal: parse subStatus in error responses

This commit is contained in:
Max Kellermann 2018-01-24 12:26:12 +01:00
parent 2e1481f49d
commit 4398101706
3 changed files with 37 additions and 4 deletions

View File

@ -33,15 +33,25 @@ class TidalError : public std::runtime_error {
*/
unsigned status;
/**
* The Tidal-specific "subStatus". 0 if none was found in the
* JSON response.
*/
unsigned sub_status;
public:
template<typename W>
TidalError(unsigned _status, W &&_what) noexcept
TidalError(unsigned _status, unsigned _sub_status, W &&_what) noexcept
:std::runtime_error(std::forward<W>(_what)),
status(_status) {}
status(_status), sub_status(_sub_status) {}
unsigned GetStatus() const noexcept {
return status;
}
unsigned GetSubStatus() const noexcept {
return sub_status;
}
};
#endif

View File

@ -28,7 +28,7 @@ using Wrapper = Yajl::CallbacksWrapper<TidalErrorParser>;
static constexpr yajl_callbacks tidal_error_parser_callbacks = {
nullptr,
nullptr,
nullptr,
Wrapper::Integer,
nullptr,
nullptr,
Wrapper::String,
@ -62,7 +62,23 @@ TidalErrorParser::OnEnd()
else
snprintf(what, sizeof(what), "Status %u from Tidal", status);
throw TidalError(status, what);
throw TidalError(status, sub_status, what);
}
inline bool
TidalErrorParser::Integer(long long value) noexcept
{
switch (state) {
case State::NONE:
case State::USER_MESSAGE:
break;
case State::SUB_STATUS:
sub_status = value;
break;
}
return true;
}
inline bool
@ -70,6 +86,7 @@ TidalErrorParser::String(StringView value) noexcept
{
switch (state) {
case State::NONE:
case State::SUB_STATUS:
break;
case State::USER_MESSAGE:
@ -85,6 +102,8 @@ TidalErrorParser::MapKey(StringView value) noexcept
{
if (value.Equals("userMessage"))
state = State::USER_MESSAGE;
else if (value.Equals("subStatus"))
state = State::SUB_STATUS;
else
state = State::NONE;

View File

@ -39,8 +39,11 @@ class TidalErrorParser final : public YajlResponseParser {
enum class State {
NONE,
USER_MESSAGE,
SUB_STATUS,
} state = State::NONE;
unsigned sub_status = 0;
std::string message;
public:
@ -57,6 +60,7 @@ protected:
public:
/* yajl callbacks */
bool Integer(long long value) noexcept;
bool String(StringView value) noexcept;
bool MapKey(StringView value) noexcept;
bool EndMap() noexcept;