From 0364024781ffa709a1a86ae9c6638f71df0317ca Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 28 Jan 2022 07:46:04 -0500 Subject: Use QPDFUsage exception for cli, json, and QPDFJob errors --- include/qpdf/JSONHandler.hh | 22 ++++++++-------------- include/qpdf/QPDFArgParser.hh | 14 ++------------ include/qpdf/QPDFExc.hh | 2 +- include/qpdf/QPDFJob.hh | 16 ++++++---------- include/qpdf/QPDFSystemError.hh | 2 +- include/qpdf/QPDFUsage.hh | 39 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 57 insertions(+), 38 deletions(-) create mode 100644 include/qpdf/QPDFUsage.hh (limited to 'include') diff --git a/include/qpdf/JSONHandler.hh b/include/qpdf/JSONHandler.hh index 0f75f946..73e8295f 100644 --- a/include/qpdf/JSONHandler.hh +++ b/include/qpdf/JSONHandler.hh @@ -28,7 +28,6 @@ #include #include #include -#include #include // This class allows a sax-like walk through a JSON object with @@ -39,15 +38,8 @@ class JSONHandler { public: - // Error exception is thrown if there are any errors validating - // the JSON object. - class QPDF_DLL_CLASS Error: public std::runtime_error - { - public: - QPDF_DLL - Error(std::string const&); - }; - + // A QPDFUsage exception is thrown if there are any errors + // validating the JSON object. QPDF_DLL JSONHandler(); @@ -55,10 +47,10 @@ class JSONHandler ~JSONHandler() = default; // Based on the type of handler, expect the object to be of a - // certain type. JSONHandler::Error is thrown otherwise. Multiple - // handlers may be registered, which allows the object to be of - // various types. If an anyHandler is added, no other handler will - // be called. There is no "final" handler -- if the top-level is a + // certain type. QPDFUsage is thrown otherwise. Multiple handlers + // may be registered, which allows the object to be of various + // types. If an anyHandler is added, no other handler will be + // called. There is no "final" handler -- if the top-level is a // dictionary or array, just use its end handler. typedef std::function #include #include -#include #include // This is not a general-purpose argument parser. It is tightly @@ -56,15 +55,6 @@ class QPDFArgParser { public: - // Usage exception is thrown if there are any errors parsing - // arguments - class QPDF_DLL_CLASS Usage: public std::runtime_error - { - public: - QPDF_DLL - Usage(std::string const&); - }; - // progname_env is used to override argv[0] when figuring out the // name of the executable for setting up completion. This may be // needed if the program is invoked by a wrapper. @@ -72,8 +62,8 @@ class QPDFArgParser QPDFArgParser(int argc, char* argv[], char const* progname_env); // Calls exit(0) if a help option is given or if in completion - // mode. If there are argument parsing errors, - // QPDFArgParser::Usage is thrown. + // mode. If there are argument parsing errors, QPDFUsage is + // thrown. QPDF_DLL void parseArgs(); diff --git a/include/qpdf/QPDFExc.hh b/include/qpdf/QPDFExc.hh index a156412b..faf5ddf6 100644 --- a/include/qpdf/QPDFExc.hh +++ b/include/qpdf/QPDFExc.hh @@ -39,7 +39,7 @@ class QPDF_DLL_CLASS QPDFExc: public std::runtime_error qpdf_offset_t offset, std::string const& message); QPDF_DLL - virtual ~QPDFExc() throw () + virtual ~QPDFExc() noexcept { } diff --git a/include/qpdf/QPDFJob.hh b/include/qpdf/QPDFJob.hh index 076501ff..e56626a7 100644 --- a/include/qpdf/QPDFJob.hh +++ b/include/qpdf/QPDFJob.hh @@ -43,15 +43,8 @@ class QPDFWriter; class QPDFJob { public: - // ConfigError exception is thrown if there are any usage-like - // errors when calling Config methods. - class QPDF_DLL_CLASS ConfigError: public std::runtime_error - { - public: - QPDF_DLL - ConfigError(std::string const&); - }; - + // QPDFUsage is thrown if there are any usage-like errors when + // calling Config methods. QPDF_DLL QPDFJob(); @@ -318,7 +311,9 @@ class QPDFJob friend class Config; // Return a top-level configuration item. See CONFIGURATION above - // for details. + // for details. If an invalid configuration is created (such as + // supplying contradictory options, omitting an input file, etc.), + // QPDFUsage is thrown. QPDF_DLL std::shared_ptr config(); @@ -404,6 +399,7 @@ class QPDFJob }; // Helper functions + static void usage(std::string const& msg); static JSON json_schema(std::set* keys = 0); static void parse_object_id( std::string const& objspec, bool& trailer, int& obj, int& gen); diff --git a/include/qpdf/QPDFSystemError.hh b/include/qpdf/QPDFSystemError.hh index 5d631b8b..16eb3583 100644 --- a/include/qpdf/QPDFSystemError.hh +++ b/include/qpdf/QPDFSystemError.hh @@ -36,7 +36,7 @@ class QPDF_DLL_CLASS QPDFSystemError: public std::runtime_error QPDFSystemError(std::string const& description, int system_errno); QPDF_DLL - virtual ~QPDFSystemError() throw (); + virtual ~QPDFSystemError() noexcept; // To get a complete error string, call what(), provided by // std::exception. The accessors below return the original values diff --git a/include/qpdf/QPDFUsage.hh b/include/qpdf/QPDFUsage.hh new file mode 100644 index 00000000..124bf188 --- /dev/null +++ b/include/qpdf/QPDFUsage.hh @@ -0,0 +1,39 @@ +// Copyright (c) 2005-2021 Jay Berkenbilt +// +// This file is part of qpdf. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Versions of qpdf prior to version 7 were released under the terms +// of version 2.0 of the Artistic License. At your option, you may +// continue to consider qpdf to be licensed under those terms. Please +// see the manual for additional information. + +#ifndef QPDFUSAGE_HH +#define QPDFUSAGE_HH + +#include + +#include +#include + +class QPDF_DLL_CLASS QPDFUsage: public std::runtime_error +{ + public: + QPDF_DLL + QPDFUsage(std::string const& msg); + QPDF_DLL + virtual ~QPDFUsage() noexcept = default; +}; + +#endif // QPDFUSAGE_HH -- cgit v1.2.3-54-g00ecf