From a31fb3c062c23a60fd945d2035b8271b0f0ca28d Mon Sep 17 00:00:00 2001 From: emeric Date: Thu, 26 Jul 2018 10:26:49 +0200 Subject: [PATCH] Always use our own exception tree --- src/av/AvInfo.cpp | 2 +- src/av/AvInfo.hpp | 8 +++++++- src/av/AvTranscoder.cpp | 8 ++++---- src/database/SqlQuery.cpp | 6 ++---- src/ui/LmsApplication.cpp | 4 ++-- src/ui/admin/InitWizardView.cpp | 3 ++- src/ui/admin/UserView.cpp | 5 +++-- src/ui/resource/ImageResource.cpp | 5 +++-- src/utils/Path.cpp | 6 +++--- 9 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/av/AvInfo.cpp b/src/av/AvInfo.cpp index 186d41f6..1b5f7c6d 100644 --- a/src/av/AvInfo.cpp +++ b/src/av/AvInfo.cpp @@ -36,7 +36,7 @@ static std::string averror_to_string(int error) } MediaFileException::MediaFileException(int avError) -: LmsException("MediaFileException: " + averror_to_string(avError)) +: AvException("MediaFileException: " + averror_to_string(avError)) { } diff --git a/src/av/AvInfo.hpp b/src/av/AvInfo.hpp index f3a4ee53..7e0e79ed 100644 --- a/src/av/AvInfo.hpp +++ b/src/av/AvInfo.hpp @@ -57,7 +57,13 @@ struct StreamInfo std::size_t bitrate; }; -class MediaFileException : public LmsException +class AvException : public LmsException +{ + public: + AvException(const std::string& msg) : LmsException(msg) {} +}; + +class MediaFileException : public AvException { public: MediaFileException(int avError); diff --git a/src/av/AvTranscoder.cpp b/src/av/AvTranscoder.cpp index 479d50f6..9dc28884 100644 --- a/src/av/AvTranscoder.cpp +++ b/src/av/AvTranscoder.cpp @@ -52,7 +52,7 @@ std::string encodingToMimetype(Encoding encoding) return encodingInfo.mimetype; } - throw std::logic_error("encoding_to_mimetype failed!"); + throw AvException("Invalid encoding"); } int encodingToInt(Encoding encoding) @@ -63,7 +63,7 @@ int encodingToInt(Encoding encoding) return encodingInfo.id; } - throw std::logic_error("encoding_to_int failed!"); + throw AvException("Invalid encoding"); } Encoding encodingFromInt(int encodingId) @@ -74,7 +74,7 @@ Encoding encodingFromInt(int encodingId) return encodingInfo.encoding; } - throw std::logic_error("encoding_from_int failed!"); + throw AvException("Invalid encodingId"); } // TODO, parametrize? @@ -104,7 +104,7 @@ Transcoder::init() if (!avConvPath.empty()) LMS_LOG(TRANSCODE, INFO) << "Using transcoder " << avConvPath.string(); else - throw std::runtime_error("Cannot find any transcoder binary!"); + throw AvException("Cannot find any transcoder binary!"); } Transcoder::Transcoder(boost::filesystem::path filePath, TranscodeParameters parameters) diff --git a/src/database/SqlQuery.cpp b/src/database/SqlQuery.cpp index 6956d180..c10eaee1 100644 --- a/src/database/SqlQuery.cpp +++ b/src/database/SqlQuery.cpp @@ -20,9 +20,8 @@ #include "SqlQuery.hpp" #include - +#include #include -#include WhereClause& WhereClause::And(const WhereClause& otherClause) @@ -70,8 +69,7 @@ WhereClause::get(void) const WhereClause& WhereClause::bind(const std::string& bindArg) { - if (_bindArgs.size() >= static_cast( std::count(_clause.begin(), _clause.end(), '?') )) - throw std::runtime_error("Too many bind args!"); + assert(_bindArgs.size() < static_cast(std::count(_clause.begin(), _clause.end(), '?'))); _bindArgs.push_back(bindArg); diff --git a/src/ui/LmsApplication.cpp b/src/ui/LmsApplication.cpp index 237b92df..cd8860bc 100644 --- a/src/ui/LmsApplication.cpp +++ b/src/ui/LmsApplication.cpp @@ -288,7 +288,7 @@ LmsApplication::handleAuthEvent() catch (std::exception& e) { LMS_LOG(UI, ERROR) << "Error while handling auth event: " << e.what(); - throw std::runtime_error("Internal error"); + throw LmsException("Internal error"); // Do not put details here at it appears on the user rendered html } } @@ -483,7 +483,7 @@ LmsApplication::notify(const Wt::WEvent& event) catch (std::exception& e) { LMS_LOG(UI, ERROR) << "Caught exception: " << e.what(); - throw std::runtime_error("Internal error"); + throw LmsException("Internal error"); // Do not put details here at it appears on the user rendered html } } diff --git a/src/ui/admin/InitWizardView.cpp b/src/ui/admin/InitWizardView.cpp index 93f8faaf..bbc97b0f 100644 --- a/src/ui/admin/InitWizardView.cpp +++ b/src/ui/admin/InitWizardView.cpp @@ -24,6 +24,7 @@ #include #include +#include "utils/Exception.hpp" #include "utils/Logger.hpp" #include "common/Validators.hpp" @@ -58,7 +59,7 @@ class InitWizardModel : public Wt::WFormModel // Check if a user already exist // If it's the case, just do nothing if (!Database::User::getAll(LmsApp->getDboSession()).empty()) - throw std::runtime_error("Admin user already created"); + throw LmsException("Admin user already created"); // Create user Wt::Auth::User authUser = LmsApp->getDb().getUserDatabase().registerNew(); diff --git a/src/ui/admin/UserView.cpp b/src/ui/admin/UserView.cpp index 0bf1de76..da5ba9bd 100644 --- a/src/ui/admin/UserView.cpp +++ b/src/ui/admin/UserView.cpp @@ -30,8 +30,9 @@ #include #include "common/Validators.hpp" -#include "utils/Utils.hpp" +#include "utils/Exception.hpp" #include "utils/Logger.hpp" +#include "utils/Utils.hpp" #include "LmsApplication.hpp" @@ -79,7 +80,7 @@ class UserModel : public Wt::WFormModel Database::User::pointer user = LmsApp->getDb().getUser(authUser); if (user == LmsApp->getUser()) - throw std::runtime_error("Cannot edit ourselves"); + throw LmsException("Cannot edit ourselves"); auto bitrate = getBitrateLimitRow(user->getMaxAudioBitrate()); if (bitrate) diff --git a/src/ui/resource/ImageResource.cpp b/src/ui/resource/ImageResource.cpp index d78c2b46..dc45b339 100644 --- a/src/ui/resource/ImageResource.cpp +++ b/src/ui/resource/ImageResource.cpp @@ -22,6 +22,7 @@ #include #include +#include "utils/Exception.hpp" #include "utils/Logger.hpp" #include "utils/Utils.hpp" @@ -58,7 +59,7 @@ ImageResource::getDefaultCover(std::size_t size) Image::Image image; if (!image.load( Wt::WApplication::instance()->docRoot() + unknownCoverPath )) - throw std::runtime_error("Cannot read default cover file"); + throw LmsException("Cannot read default cover file"); image.scale(size); @@ -80,7 +81,7 @@ ImageResource::getDefaultArtistImage(std::size_t size) Image::Image image; if (!image.load( Wt::WApplication::instance()->docRoot() + unknownArtistImagePath)) - throw std::runtime_error("Cannot read default artist image file"); + throw LmsException("Cannot read default artist image file"); image.scale(size); diff --git a/src/utils/Path.cpp b/src/utils/Path.cpp index b6511840..b1a87666 100644 --- a/src/utils/Path.cpp +++ b/src/utils/Path.cpp @@ -21,11 +21,11 @@ #include #include -#include #include // for boost::crc_32_type #include +#include "utils/Exception.hpp" #include "utils/Logger.hpp" boost::filesystem::path searchExecPath(std::string filename) @@ -34,7 +34,7 @@ boost::filesystem::path searchExecPath(std::string filename) path = ::getenv("PATH"); if (path.empty()) - throw std::runtime_error("Environment variable PATH not found"); + throw LmsException("Environment variable PATH not found"); std::string result; typedef boost::tokenizer > tokenizer; @@ -75,7 +75,7 @@ void computeCrc(const boost::filesystem::path& p, std::vector& cr else { LMS_LOG(DBUPDATER, ERROR) << "Failed to open file '" << p.string() << "'"; - throw std::runtime_error("Failed to open file '" + p.string() + "'" ); + throw LmsException("Failed to open file '" + p.string() + "'" ); } // Copy back result into the vector