Commit 2e1481f4 authored by Max Kellermann's avatar Max Kellermann

input/tidal: add exception class TidalError

Allows catchers to inspect the HTTP status.
parent c7c087a0
...@@ -1372,6 +1372,7 @@ endif ...@@ -1372,6 +1372,7 @@ endif
if ENABLE_TIDAL if ENABLE_TIDAL
libinput_a_SOURCES += \ libinput_a_SOURCES += \
$(YAJL_SOURCES) \ $(YAJL_SOURCES) \
src/input/plugins/TidalError.hxx \
src/input/plugins/TidalErrorParser.cxx src/input/plugins/TidalErrorParser.hxx \ src/input/plugins/TidalErrorParser.cxx src/input/plugins/TidalErrorParser.hxx \
src/input/plugins/TidalLoginRequest.cxx src/input/plugins/TidalLoginRequest.hxx \ src/input/plugins/TidalLoginRequest.cxx src/input/plugins/TidalLoginRequest.hxx \
src/input/plugins/TidalSessionManager.cxx src/input/plugins/TidalSessionManager.hxx \ src/input/plugins/TidalSessionManager.cxx src/input/plugins/TidalSessionManager.hxx \
......
/*
* Copyright 2003-2018 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef TIDAL_ERROR_HXX
#define TIDAL_ERROR_HXX
#include "check.h"
#include <stdexcept>
/**
* An error condition reported by the server.
*/
class TidalError : public std::runtime_error {
/**
* The HTTP status code.
*/
unsigned status;
public:
template<typename W>
TidalError(unsigned _status, W &&_what) noexcept
:std::runtime_error(std::forward<W>(_what)),
status(_status) {}
unsigned GetStatus() const noexcept {
return status;
}
};
#endif
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "TidalErrorParser.hxx" #include "TidalErrorParser.hxx"
#include "TidalError.hxx"
#include "lib/yajl/Callbacks.hxx" #include "lib/yajl/Callbacks.hxx"
#include "util/ConstBuffer.hxx" #include "util/ConstBuffer.hxx"
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
...@@ -53,11 +54,15 @@ TidalErrorParser::OnEnd() ...@@ -53,11 +54,15 @@ TidalErrorParser::OnEnd()
{ {
YajlResponseParser::OnEnd(); YajlResponseParser::OnEnd();
char what[1024];
if (!message.empty()) if (!message.empty())
throw FormatRuntimeError("Error from Tidal: %s", snprintf(what, sizeof(what), "Error from Tidal: %s",
message.c_str()); message.c_str());
else else
throw FormatRuntimeError("Status %u from Tidal", status); snprintf(what, sizeof(what), "Status %u from Tidal", status);
throw TidalError(status, what);
} }
inline bool inline bool
......
...@@ -30,7 +30,8 @@ template<typename T> struct ConstBuffer; ...@@ -30,7 +30,8 @@ template<typename T> struct ConstBuffer;
struct StringView; struct StringView;
/** /**
* Parse an error JSON response. * Parse an error JSON response and throw a #TidalError upon
* completion.
*/ */
class TidalErrorParser final : public YajlResponseParser { class TidalErrorParser final : public YajlResponseParser {
const unsigned status; const unsigned status;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment