input/tidal: add exception class TidalError

Allows catchers to inspect the HTTP status.
This commit is contained in:
Max Kellermann 2018-01-24 12:44:57 +01:00
parent c7c087a0d2
commit 2e1481f49d
4 changed files with 58 additions and 4 deletions

View File

@ -1372,6 +1372,7 @@ endif
if ENABLE_TIDAL
libinput_a_SOURCES += \
$(YAJL_SOURCES) \
src/input/plugins/TidalError.hxx \
src/input/plugins/TidalErrorParser.cxx src/input/plugins/TidalErrorParser.hxx \
src/input/plugins/TidalLoginRequest.cxx src/input/plugins/TidalLoginRequest.hxx \
src/input/plugins/TidalSessionManager.cxx src/input/plugins/TidalSessionManager.hxx \

View File

@ -0,0 +1,47 @@
/*
* 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

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "TidalErrorParser.hxx"
#include "TidalError.hxx"
#include "lib/yajl/Callbacks.hxx"
#include "util/ConstBuffer.hxx"
#include "util/RuntimeError.hxx"
@ -53,11 +54,15 @@ TidalErrorParser::OnEnd()
{
YajlResponseParser::OnEnd();
char what[1024];
if (!message.empty())
throw FormatRuntimeError("Error from Tidal: %s",
message.c_str());
snprintf(what, sizeof(what), "Error from Tidal: %s",
message.c_str());
else
throw FormatRuntimeError("Status %u from Tidal", status);
snprintf(what, sizeof(what), "Status %u from Tidal", status);
throw TidalError(status, what);
}
inline bool

View File

@ -30,7 +30,8 @@ template<typename T> struct ConstBuffer;
struct StringView;
/**
* Parse an error JSON response.
* Parse an error JSON response and throw a #TidalError upon
* completion.
*/
class TidalErrorParser final : public YajlResponseParser {
const unsigned status;