Commit 9373881cca0c781c66b2b50b962bdbc26364abe5

Authored by Jay Berkenbilt
1 parent 0a354af0

Add QPDFJob::ConfigError exception

include/qpdf/QPDFJob.hh
@@ -36,12 +36,22 @@ @@ -36,12 +36,22 @@
36 #include <iostream> 36 #include <iostream>
37 #include <functional> 37 #include <functional>
38 #include <memory> 38 #include <memory>
  39 +#include <stdexcept>
39 40
40 class QPDFWriter; 41 class QPDFWriter;
41 42
42 class QPDFJob 43 class QPDFJob
43 { 44 {
44 public: 45 public:
  46 + // ConfigError exception is thrown if there are any usage-like
  47 + // errors when calling Config methods.
  48 + class QPDF_DLL_CLASS ConfigError: public std::runtime_error
  49 + {
  50 + public:
  51 + QPDF_DLL
  52 + ConfigError(std::string const&);
  53 + };
  54 +
45 QPDF_DLL 55 QPDF_DLL
46 QPDFJob(); 56 QPDFJob();
47 57
libqpdf/QPDFJob.cc
@@ -33,6 +33,11 @@ @@ -33,6 +33,11 @@
33 #include <qpdf/QPDFWriter.hh> 33 #include <qpdf/QPDFWriter.hh>
34 #include <qpdf/QIntC.hh> 34 #include <qpdf/QIntC.hh>
35 35
  36 +QPDFJob::ConfigError::ConfigError(std::string const& msg) :
  37 + std::runtime_error(msg)
  38 +{
  39 +}
  40 +
36 namespace 41 namespace
37 { 42 {
38 class ImageOptimizer: public QPDFObjectHandle::StreamDataProvider 43 class ImageOptimizer: public QPDFObjectHandle::StreamDataProvider
libqpdf/QPDFJob_config.cc
@@ -534,9 +534,7 @@ QPDFJob::CopyAttConfig::end() @@ -534,9 +534,7 @@ QPDFJob::CopyAttConfig::end()
534 { 534 {
535 if (this->caf.path.empty()) 535 if (this->caf.path.empty())
536 { 536 {
537 - // QXXXQ usage, json, and config exceptions need to be unified  
538 - // in some fashion.  
539 - throw std::runtime_error("copy attachments: no path specified"); 537 + throw QPDFJob::ConfigError("copy attachments: no path specified");
540 } 538 }
541 this->config.o.attachments_to_copy.push_back(this->caf); 539 this->config.o.attachments_to_copy.push_back(this->caf);
542 return this->config; 540 return this->config;
@@ -579,8 +577,7 @@ QPDFJob::AttConfig::creationdate(char const* parameter) @@ -579,8 +577,7 @@ QPDFJob::AttConfig::creationdate(char const* parameter)
579 { 577 {
580 if (! QUtil::pdf_time_to_qpdf_time(parameter)) 578 if (! QUtil::pdf_time_to_qpdf_time(parameter))
581 { 579 {
582 - // QXXXQ  
583 - throw std::runtime_error( 580 + throw QPDFJob::ConfigError(
584 std::string(parameter) + " is not a valid PDF timestamp"); 581 std::string(parameter) + " is not a valid PDF timestamp");
585 } 582 }
586 this->att.creationdate = parameter; 583 this->att.creationdate = parameter;
@@ -592,8 +589,7 @@ QPDFJob::AttConfig::moddate(char const* parameter) @@ -592,8 +589,7 @@ QPDFJob::AttConfig::moddate(char const* parameter)
592 { 589 {
593 if (! QUtil::pdf_time_to_qpdf_time(parameter)) 590 if (! QUtil::pdf_time_to_qpdf_time(parameter))
594 { 591 {
595 - // QXXXQ  
596 - throw std::runtime_error( 592 + throw QPDFJob::ConfigError(
597 std::string(parameter) + " is not a valid PDF timestamp"); 593 std::string(parameter) + " is not a valid PDF timestamp");
598 } 594 }
599 this->att.moddate = parameter; 595 this->att.moddate = parameter;
@@ -605,8 +601,7 @@ QPDFJob::AttConfig::mimetype(char const* parameter) @@ -605,8 +601,7 @@ QPDFJob::AttConfig::mimetype(char const* parameter)
605 { 601 {
606 if (strchr(parameter, '/') == nullptr) 602 if (strchr(parameter, '/') == nullptr)
607 { 603 {
608 - // QXXXQ  
609 - throw std::runtime_error( 604 + throw QPDFJob::ConfigError(
610 "mime type should be specified as type/subtype"); 605 "mime type should be specified as type/subtype");
611 } 606 }
612 this->att.mimetype = parameter; 607 this->att.mimetype = parameter;
@@ -630,18 +625,17 @@ QPDFJob::AttConfig::replace() @@ -630,18 +625,17 @@ QPDFJob::AttConfig::replace()
630 QPDFJob::Config& 625 QPDFJob::Config&
631 QPDFJob::AttConfig::end() 626 QPDFJob::AttConfig::end()
632 { 627 {
633 - // QXXXQ runtime_error  
634 -  
635 static std::string now = QUtil::qpdf_time_to_pdf_time( 628 static std::string now = QUtil::qpdf_time_to_pdf_time(
636 QUtil::get_current_qpdf_time()); 629 QUtil::get_current_qpdf_time());
637 if (this->att.path.empty()) 630 if (this->att.path.empty())
638 { 631 {
639 - throw std::runtime_error("add attachment: no path specified"); 632 + throw QPDFJob::ConfigError("add attachment: no path specified");
640 } 633 }
641 std::string last_element = QUtil::path_basename(this->att.path); 634 std::string last_element = QUtil::path_basename(this->att.path);
642 if (last_element.empty()) 635 if (last_element.empty())
643 { 636 {
644 - throw std::runtime_error("path for --add-attachment may not be empty"); 637 + throw QPDFJob::ConfigError(
  638 + "path for --add-attachment may not be empty");
645 } 639 }
646 if (this->att.filename.empty()) 640 if (this->att.filename.empty())
647 { 641 {
qpdf/qpdf.cc
@@ -52,6 +52,10 @@ int realmain(int argc, char* argv[]) @@ -52,6 +52,10 @@ int realmain(int argc, char* argv[])
52 { 52 {
53 usageExit(e.what()); 53 usageExit(e.what());
54 } 54 }
  55 + catch (QPDFJob::ConfigError& e)
  56 + {
  57 + usageExit(e.what());
  58 + }
55 catch (std::exception& e) 59 catch (std::exception& e)
56 { 60 {
57 std::cerr << whoami << ": " << e.what() << std::endl; 61 std::cerr << whoami << ": " << e.what() << std::endl;