From 9169bbc879b11b55610c417cacb8008eba6cf844 Mon Sep 17 00:00:00 2001
From: Xavier Robin <xavalias-github@xavier.robin.name>
Date: Mon, 5 Feb 2024 10:15:23 +0100
Subject: [PATCH] refactor: make error messages more pythonic and consistent

---
 modules/io/src/mol/pdb_reader.cc  |  4 +++-
 modules/io/src/mol/pdb_writer.cc  |  4 +++-
 modules/io/src/mol/sdf_reader.cc  |  4 +++-
 modules/io/src/mol/sdf_writer.cc  | 30 +++++++++++++++++++++++++-----
 modules/io/src/mol/sdf_writer.hh  |  1 +
 modules/io/src/mol/star_parser.cc |  6 +++---
 modules/io/src/mol/star_writer.cc | 14 ++++++--------
 7 files changed, 44 insertions(+), 19 deletions(-)

diff --git a/modules/io/src/mol/pdb_reader.cc b/modules/io/src/mol/pdb_reader.cc
index 5b047c3aa..78b6bec4d 100644
--- a/modules/io/src/mol/pdb_reader.cc
+++ b/modules/io/src/mol/pdb_reader.cc
@@ -92,7 +92,9 @@ void PDBReader::Init(const boost::filesystem::path& loc)
     in_.push(boost::iostreams::gzip_decompressor());
   }
   in_.push(instream_);
-  if(!infile_) throw IOException("could not open "+loc.string());
+  if(!infile_) throw IOException("[Errno " + std::to_string(errno) + "] " +
+                     std::string(strerror(errno)) +
+                     ": '" + loc.string() + "'");
   line_num_=0;
   if(boost::iequals(boost::filesystem::extension(loc), ".pqr")) {
     is_pqr_=true;
diff --git a/modules/io/src/mol/pdb_writer.cc b/modules/io/src/mol/pdb_writer.cc
index d4461402c..265a8a024 100644
--- a/modules/io/src/mol/pdb_writer.cc
+++ b/modules/io/src/mol/pdb_writer.cc
@@ -407,7 +407,9 @@ PDBWriter::PDBWriter(const String& filename, const IOProfile& profile):
   is_pqr_(false), profile_(profile), filename_(filename)
 {
   if (!outfile_.is_open()) {
-    throw IOException("Failed to open: " + filename);
+    throw IOException("[Errno " + std::to_string(errno) + "] " +
+                      std::string(strerror(errno)) +
+                      ": '" + filename + "'");
   }
   if (boost::iequals(".pqr", boost::filesystem::extension(filename))) {
     is_pqr_=true;
diff --git a/modules/io/src/mol/sdf_reader.cc b/modules/io/src/mol/sdf_reader.cc
index 660a9dcb5..76c7091a2 100644
--- a/modules/io/src/mol/sdf_reader.cc
+++ b/modules/io/src/mol/sdf_reader.cc
@@ -124,7 +124,9 @@ void SDFReader::ClearState(const boost::filesystem::path& loc)
     in_.push(boost::iostreams::gzip_decompressor());
   }
   in_.push(instream_);
-  if(!infile_) throw IOException("could not open "+loc.string());
+  if(!infile_) throw IOException("[Errno " + std::to_string(errno) + "] " +
+                     std::string(strerror(errno)) +
+                     ": '" + loc.string() + "'");
   curr_chain_=mol::ChainHandle();
   curr_residue_=mol::ResidueHandle();
   chain_count_=0;
diff --git a/modules/io/src/mol/sdf_writer.cc b/modules/io/src/mol/sdf_writer.cc
index c0241211a..e9fd0ded4 100644
--- a/modules/io/src/mol/sdf_writer.cc
+++ b/modules/io/src/mol/sdf_writer.cc
@@ -171,28 +171,48 @@ namespace {
 }
 
 SDFWriter::SDFWriter(std::ostream& ostream)
-  : outfile_(), ostr_(ostream), counter_(0), atom_indices_() {
+  : outfile_(), ostr_(ostream), counter_(0), atom_indices_(), filename_("") {
 }
 
 SDFWriter::SDFWriter(const String& filename)
-  : outfile_(filename.c_str()), ostr_(outfile_), counter_(0), atom_indices_() {
+  : outfile_(filename.c_str()), ostr_(outfile_), counter_(0), atom_indices_(),
+   filename_(filename){
 }
 
 SDFWriter::SDFWriter(const boost::filesystem::path& filename): 
   outfile_(BFPathToString(filename).c_str()),
-  ostr_(outfile_), counter_(0), atom_indices_() {}
+  ostr_(outfile_), counter_(0), atom_indices_(), filename_("") {}
 
 void SDFWriter::Write(const mol::EntityView& ent) {
   if (!ostr_) {
-    throw IOException("Can't write SDF file. Bad output stream");
+    if (!filename_.empty()) {
+      throw IOException("[Errno " + std::to_string(errno) + "] " +
+                        std::string(strerror(errno)) +
+                        ": '" + filename_ + "'");
+    }
+    else {
+      throw IOException("[Errno " + std::to_string(errno) + "] " +
+                        std::string(strerror(errno)) +
+                        ": <stream>");
+    }
   }
+
   mol::EntityView non_const_view = ent;
   non_const_view.Apply(*this);
 }
 
 void SDFWriter::Write(const mol::EntityHandle& ent) {
   if (!ostr_) {
-    throw IOException("Can't write SDF file. Bad output stream");
+    if (!filename_.empty()) {
+      throw IOException("[Errno " + std::to_string(errno) + "] " +
+                        std::string(strerror(errno)) +
+                        ": '" + filename_ + "'");
+    }
+    else {
+      throw IOException("[Errno " + std::to_string(errno) + "] " +
+                        std::string(strerror(errno)) +
+                        ": <stream>");
+    }
   }
   mol::EntityView non_const_view = ent.CreateFullView();
   non_const_view.Apply(*this);
diff --git a/modules/io/src/mol/sdf_writer.hh b/modules/io/src/mol/sdf_writer.hh
index 8ca166d97..929be1d35 100644
--- a/modules/io/src/mol/sdf_writer.hh
+++ b/modules/io/src/mol/sdf_writer.hh
@@ -54,6 +54,7 @@ private:
   std::ostream&      ostr_;
   int                counter_;
   std::map<long,int> atom_indices_;
+  String             filename_;
 };
 
 }}
diff --git a/modules/io/src/mol/star_parser.cc b/modules/io/src/mol/star_parser.cc
index f22857a8f..c16bc2483 100644
--- a/modules/io/src/mol/star_parser.cc
+++ b/modules/io/src/mol/star_parser.cc
@@ -549,9 +549,9 @@ void StarParser::ParseGlobal()
 void StarParser::Parse()
 {
   if (!file_open_) {
-    throw IOException(this->FormatDiagnostic(STAR_DIAG_ERROR,
-                                             "Failed to open file '" +
-                                             filename_ + "'!"));
+    throw IOException("[Errno " + std::to_string(errno) + "] " +
+                     std::string(strerror(errno)) +
+                     ": '" + filename_ + "'");
   }
   StringRef line;
   std::stringstream ss;
diff --git a/modules/io/src/mol/star_writer.cc b/modules/io/src/mol/star_writer.cc
index 4b64bd370..73ff08f94 100644
--- a/modules/io/src/mol/star_writer.cc
+++ b/modules/io/src/mol/star_writer.cc
@@ -27,10 +27,9 @@ namespace ost{ namespace io{
 
 void StarWriter::Write(const String& data_name, std::ostream& stream) {
   if(!stream) {
-    std::stringstream ss;
-    ss << "Cannot open stream: [Errno " << errno << "] "
-       << strerror(errno) << std::endl;
-    throw IOException(ss.str());
+      throw IOException("[Errno " + std::to_string(errno) + "] " +
+                        std::string(strerror(errno)) +
+                        ": <stream>");
   }
   // write data header
   stream << "data_" << data_name << std::endl;
@@ -45,10 +44,9 @@ void StarWriter::Write(const String& data_name, std::ostream& stream) {
 void StarWriter::Write(const String& data_name, const String& filename) {
   std::ofstream fstream(filename.c_str());
   if (!fstream) {
-    std::stringstream ss;
-    ss << "Cannot open " << filename << ": [Errno " << errno << "] "
-       << strerror(errno) << std::endl;
-    throw IOException(ss.str());
+      throw IOException("[Errno " + std::to_string(errno) + "] " +
+                        std::string(strerror(errno)) +
+                        ": '" + filename + "'");
   }
   boost::iostreams::filtering_stream<boost::iostreams::output> stream;
   if (boost::iequals(".gz", boost::filesystem::extension(filename))) {
-- 
GitLab