From 4398101706e856358cbb72b36fa70ad71b288966 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 24 Jan 2018 12:26:12 +0100 Subject: [PATCH] input/tidal: parse subStatus in error responses --- src/input/plugins/TidalError.hxx | 14 ++++++++++++-- src/input/plugins/TidalErrorParser.cxx | 23 +++++++++++++++++++++-- src/input/plugins/TidalErrorParser.hxx | 4 ++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/input/plugins/TidalError.hxx b/src/input/plugins/TidalError.hxx index 108899fea..4ea1ff5ca 100644 --- a/src/input/plugins/TidalError.hxx +++ b/src/input/plugins/TidalError.hxx @@ -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 - TidalError(unsigned _status, W &&_what) noexcept + TidalError(unsigned _status, unsigned _sub_status, W &&_what) noexcept :std::runtime_error(std::forward(_what)), - status(_status) {} + status(_status), sub_status(_sub_status) {} unsigned GetStatus() const noexcept { return status; } + + unsigned GetSubStatus() const noexcept { + return sub_status; + } }; #endif diff --git a/src/input/plugins/TidalErrorParser.cxx b/src/input/plugins/TidalErrorParser.cxx index dce05745a..0608d619d 100644 --- a/src/input/plugins/TidalErrorParser.cxx +++ b/src/input/plugins/TidalErrorParser.cxx @@ -28,7 +28,7 @@ using Wrapper = Yajl::CallbacksWrapper; 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; diff --git a/src/input/plugins/TidalErrorParser.hxx b/src/input/plugins/TidalErrorParser.hxx index 457bd573e..9c59e5359 100644 --- a/src/input/plugins/TidalErrorParser.hxx +++ b/src/input/plugins/TidalErrorParser.hxx @@ -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;