From 2e1481f49db9149e5686693a78f8612699285ebe Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 24 Jan 2018 12:44:57 +0100 Subject: [PATCH] input/tidal: add exception class TidalError Allows catchers to inspect the HTTP status. --- Makefile.am | 1 + src/input/plugins/TidalError.hxx | 47 ++++++++++++++++++++++++++ src/input/plugins/TidalErrorParser.cxx | 11 ++++-- src/input/plugins/TidalErrorParser.hxx | 3 +- 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 src/input/plugins/TidalError.hxx diff --git a/Makefile.am b/Makefile.am index 59d3f2ebf..094fbf13e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/input/plugins/TidalError.hxx b/src/input/plugins/TidalError.hxx new file mode 100644 index 000000000..108899fea --- /dev/null +++ b/src/input/plugins/TidalError.hxx @@ -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 + +/** + * An error condition reported by the server. + */ +class TidalError : public std::runtime_error { + /** + * The HTTP status code. + */ + unsigned status; + +public: + template + TidalError(unsigned _status, W &&_what) noexcept + :std::runtime_error(std::forward(_what)), + status(_status) {} + + unsigned GetStatus() const noexcept { + return status; + } +}; + +#endif diff --git a/src/input/plugins/TidalErrorParser.cxx b/src/input/plugins/TidalErrorParser.cxx index 149037786..dce05745a 100644 --- a/src/input/plugins/TidalErrorParser.cxx +++ b/src/input/plugins/TidalErrorParser.cxx @@ -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 diff --git a/src/input/plugins/TidalErrorParser.hxx b/src/input/plugins/TidalErrorParser.hxx index 969ec0cf9..457bd573e 100644 --- a/src/input/plugins/TidalErrorParser.hxx +++ b/src/input/plugins/TidalErrorParser.hxx @@ -30,7 +30,8 @@ template 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;