From 4d7dd62fcf8d0f1feb96dc0749aa3003750ff7eb Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Sun, 7 Apr 2024 14:49:38 +0200 Subject: Add a common exception parent class --- src/backup.h | 8 +++----- src/exception.h | 39 +++++++++++++++++++++++++++++++++++++++ src/main.cpp | 5 +---- src/options.h | 9 ++++----- src/restore.h | 8 +++----- 5 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 src/exception.h diff --git a/src/backup.h b/src/backup.h index 289ee62..38a0b68 100644 --- a/src/backup.h +++ b/src/backup.h @@ -27,15 +27,13 @@ #ifndef BACKUP_H #define BACKUP_H +#include "exception.h" #include "options.h" -class BackupError : public std::runtime_error +class BackupError : public DiffddError { public: - explicit BackupError(const std::string &message) - : std::runtime_error(message) - { - } + explicit BackupError(const std::string &message) : DiffddError(message) {} }; void backup(const OptionsBackup &opts); diff --git a/src/exception.h b/src/exception.h new file mode 100644 index 0000000..99f8ee6 --- /dev/null +++ b/src/exception.h @@ -0,0 +1,39 @@ +/* Copyright 2024 Ján Sučan + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include +#include + +class DiffddError : public std::runtime_error +{ + public: + explicit DiffddError(const std::string &message) + : std::runtime_error(message) + { + } +}; diff --git a/src/main.cpp b/src/main.cpp index 01f3a69..0810f04 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,10 +48,7 @@ main(int argc, char **argv) OptionParser::printUsage(); std::cerr << "ERROR: " << e.what() << std::endl; exit(1); - } catch (const BackupError &e) { - std::cerr << "ERROR: " << e.what() << std::endl; - exit(1); - } catch (const RestoreError &e) { + } catch (const DiffddError &e) { std::cerr << "ERROR: " << e.what() << std::endl; exit(1); } diff --git a/src/options.h b/src/options.h index e0c16d1..2355082 100644 --- a/src/options.h +++ b/src/options.h @@ -27,16 +27,15 @@ #ifndef OPTIONS_H #define OPTIONS_H +#include "exception.h" + #include #include -class OptionError : public std::runtime_error +class OptionError : public DiffddError { public: - explicit OptionError(const std::string &message) - : std::runtime_error(message) - { - } + explicit OptionError(const std::string &message) : DiffddError(message) {} }; class Options diff --git a/src/restore.h b/src/restore.h index 0ea9549..4aee092 100644 --- a/src/restore.h +++ b/src/restore.h @@ -27,15 +27,13 @@ #ifndef RESTORE_H #define RESTORE_H +#include "exception.h" #include "options.h" -class RestoreError : public std::runtime_error +class RestoreError : public DiffddError { public: - explicit RestoreError(const std::string &message) - : std::runtime_error(message) - { - } + explicit RestoreError(const std::string &message) : DiffddError(message) {} }; void restore(const OptionsRestore &opts); -- cgit v1.2.3